HAVING¶
The HAVING
clause filters groups of rows created by the GROUP BY
clause. Unlike the WHERE clause, which filters rows before grouping, HAVING
filters groups after the aggregation has been performed.
Syntax¶
SELECT <column1>, [ aggregate_function <column2> ]
FROM <table_name>
[ WHERE <condition1> ]
GROUP BY <column1>
HAVING <condition2>
Arguments¶
Parameter |
Description |
---|---|
|
The columns to retrieve |
|
The table from which to retrieve the data |
|
Filters the rows before grouping them |
|
Groups the rows by the values in column1 |
|
Filters the groups based on the condition |
Examples¶
SELECT
department_id,
COUNT(*)
FROM
employees
GROUP BY
department_id
HAVING
COUNT(*) > 5;