Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
HAVING | Grouping
course content

Зміст курсу

Розширений рівень SQL

HAVINGHAVING

До цього моменту ми зрозуміли, як працювати з групами значень у таблиці, а не з кожним значенням окремо. Фільтрація часто використовується, коли потрібно витягти певні дані з таблиць, а не отримати всі доступні дані.

Раніше ми використовували для цієї ситуації оператор WHERE в SQL. Але це тільки тоді, коли ми працюємо із записами з таблиці, а не з групами. Для груп вираз WHERE не працює. Тому для фільтрації груп існує інша умова - HAVING. Давайте подивимося, як вона працює.

Якщо ви виконаєте цей запит, то отримаєте наступний результат:

We received one department in response using the HAVING clause, where we set a condition for the column by which we grouped the data.

Note

Note that to use data aggregation within the HAVING clause, we need to have data grouping in our query. As in the query above, we grouped the data by the department column.

Let's look at the more generalized syntax of the HAVING clause and when it's best to use it:

You can see that you get only the names of those subway lines that have more than 5 stations. If we did not apply the filtering condition, we would get the result shown below:

Let's also briefly understand the main difference between WHERE and HAVING clauses and when to use each of them:

  1. The WHERE clause is used before data aggregation, while the HAVING clause is used after data aggregation;
  2. The WHERE clause is written before GROUP BY, while the HAVING clause is written after GROUP BY.

These are the two main differences you need to remember for successful use of the HAVING clause. Now, let's return to the task given to us by the school:

id student_surname class_letter subject_name grade
1 Smith A Mathematics 85
2 Johnson B Physics 90
3 Williams C Chemistry 78
4 Brown A Biology 92
... ... ... ... ...
100 Gonzales A Biology 89

Завдання

Вам потрібно отримати для школи прізвища учнів, які мають більше однієї оцінки.

Вам потрібно отримати лише прізвища учнів; немає потреби включати кількість їхніх оцінок у відповідь. Використовуйте клазу HAVING та агрегатну функцію COUNT() для виконання цього завдання.

Примітка:

У результаті має бути лише один стовпчик із прізвищами.

Все було зрозуміло?

Секція 1. Розділ 6
toggle bottom row
course content

Зміст курсу

Розширений рівень SQL

HAVINGHAVING

До цього моменту ми зрозуміли, як працювати з групами значень у таблиці, а не з кожним значенням окремо. Фільтрація часто використовується, коли потрібно витягти певні дані з таблиць, а не отримати всі доступні дані.

Раніше ми використовували для цієї ситуації оператор WHERE в SQL. Але це тільки тоді, коли ми працюємо із записами з таблиці, а не з групами. Для груп вираз WHERE не працює. Тому для фільтрації груп існує інша умова - HAVING. Давайте подивимося, як вона працює.

Якщо ви виконаєте цей запит, то отримаєте наступний результат:

We received one department in response using the HAVING clause, where we set a condition for the column by which we grouped the data.

Note

Note that to use data aggregation within the HAVING clause, we need to have data grouping in our query. As in the query above, we grouped the data by the department column.

Let's look at the more generalized syntax of the HAVING clause and when it's best to use it:

You can see that you get only the names of those subway lines that have more than 5 stations. If we did not apply the filtering condition, we would get the result shown below:

Let's also briefly understand the main difference between WHERE and HAVING clauses and when to use each of them:

  1. The WHERE clause is used before data aggregation, while the HAVING clause is used after data aggregation;
  2. The WHERE clause is written before GROUP BY, while the HAVING clause is written after GROUP BY.

These are the two main differences you need to remember for successful use of the HAVING clause. Now, let's return to the task given to us by the school:

id student_surname class_letter subject_name grade
1 Smith A Mathematics 85
2 Johnson B Physics 90
3 Williams C Chemistry 78
4 Brown A Biology 92
... ... ... ... ...
100 Gonzales A Biology 89

Завдання

Вам потрібно отримати для школи прізвища учнів, які мають більше однієї оцінки.

Вам потрібно отримати лише прізвища учнів; немає потреби включати кількість їхніх оцінок у відповідь. Використовуйте клазу HAVING та агрегатну функцію COUNT() для виконання цього завдання.

Примітка:

У результаті має бути лише один стовпчик із прізвищами.

Все було зрозуміло?

Секція 1. Розділ 6
toggle bottom row
some-alt