Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
SQL Having | Filtering Statements
SQL Tutorial for Beginners
course content

Зміст курсу

SQL Tutorial for Beginners

SQL Tutorial for Beginners

1. Database & Introduction to Syntax
2. SQL Aggregate Functions
3. Filtering Statements

bookSQL Having

HAVING is a filter keyword that applies to the already grouped records. Unlike WHERE, which is used for conditions to filter the records, HAVING is used with a condition presented as an aggregate function and applied to the result obtained with the GROUP BY. Thus, the HAVING operator is only used with GROUP BY and only after this operator.

The syntax is next:

1234567
--do not run this query SELECT col1, col2, ... FROM table WHERE conditions GROUP BY col1, col2, ... HAVING agg_conditions ORDER BY col1, col2, ...;
copy

For example, you want to get info about singers and their songs: singers' names and the price of their most expensive song. To do that, you can use GROUP BY the singer and display the price using the function MAX(). But let it be the limit: you need only singers such that the maximum price is less than 1000. The next query solves this problem:

1234
SELECT singer, MAX(price) FROM songs GROUP BY singer HAVING MAX(price) < 1000
copy

This way, you can add a condition to the aggregate function MAX(). You cannot do that in WHERE statement, that's why we need to use HAVING.

Завдання

Retrieve info about the singers and their number of songs. Select only singers with at least 2 songs. Order records by the number of songs in groups starting with the biggest one.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 11
toggle bottom row

bookSQL Having

HAVING is a filter keyword that applies to the already grouped records. Unlike WHERE, which is used for conditions to filter the records, HAVING is used with a condition presented as an aggregate function and applied to the result obtained with the GROUP BY. Thus, the HAVING operator is only used with GROUP BY and only after this operator.

The syntax is next:

1234567
--do not run this query SELECT col1, col2, ... FROM table WHERE conditions GROUP BY col1, col2, ... HAVING agg_conditions ORDER BY col1, col2, ...;
copy

For example, you want to get info about singers and their songs: singers' names and the price of their most expensive song. To do that, you can use GROUP BY the singer and display the price using the function MAX(). But let it be the limit: you need only singers such that the maximum price is less than 1000. The next query solves this problem:

1234
SELECT singer, MAX(price) FROM songs GROUP BY singer HAVING MAX(price) < 1000
copy

This way, you can add a condition to the aggregate function MAX(). You cannot do that in WHERE statement, that's why we need to use HAVING.

Завдання

Retrieve info about the singers and their number of songs. Select only singers with at least 2 songs. Order records by the number of songs in groups starting with the biggest one.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 11
toggle bottom row

bookSQL Having

HAVING is a filter keyword that applies to the already grouped records. Unlike WHERE, which is used for conditions to filter the records, HAVING is used with a condition presented as an aggregate function and applied to the result obtained with the GROUP BY. Thus, the HAVING operator is only used with GROUP BY and only after this operator.

The syntax is next:

1234567
--do not run this query SELECT col1, col2, ... FROM table WHERE conditions GROUP BY col1, col2, ... HAVING agg_conditions ORDER BY col1, col2, ...;
copy

For example, you want to get info about singers and their songs: singers' names and the price of their most expensive song. To do that, you can use GROUP BY the singer and display the price using the function MAX(). But let it be the limit: you need only singers such that the maximum price is less than 1000. The next query solves this problem:

1234
SELECT singer, MAX(price) FROM songs GROUP BY singer HAVING MAX(price) < 1000
copy

This way, you can add a condition to the aggregate function MAX(). You cannot do that in WHERE statement, that's why we need to use HAVING.

Завдання

Retrieve info about the singers and their number of songs. Select only singers with at least 2 songs. Order records by the number of songs in groups starting with the biggest one.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

HAVING is a filter keyword that applies to the already grouped records. Unlike WHERE, which is used for conditions to filter the records, HAVING is used with a condition presented as an aggregate function and applied to the result obtained with the GROUP BY. Thus, the HAVING operator is only used with GROUP BY and only after this operator.

The syntax is next:

1234567
--do not run this query SELECT col1, col2, ... FROM table WHERE conditions GROUP BY col1, col2, ... HAVING agg_conditions ORDER BY col1, col2, ...;
copy

For example, you want to get info about singers and their songs: singers' names and the price of their most expensive song. To do that, you can use GROUP BY the singer and display the price using the function MAX(). But let it be the limit: you need only singers such that the maximum price is less than 1000. The next query solves this problem:

1234
SELECT singer, MAX(price) FROM songs GROUP BY singer HAVING MAX(price) < 1000
copy

This way, you can add a condition to the aggregate function MAX(). You cannot do that in WHERE statement, that's why we need to use HAVING.

Завдання

Retrieve info about the singers and their number of songs. Select only singers with at least 2 songs. Order records by the number of songs in groups starting with the biggest one.

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 3. Розділ 11
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
some-alt