GROUP BY and HAVING
GROUP BY
GROUP BY
clause divide the rows returned fromSELECT
statement into groupsFor each group, you can apply aggregate functions like
COUNT
,SUM
,MIN
,MAX
etc.
Syntax
Group BY
Group the data in column and pass it to aggregate function
Group BY with count
Group BY with SUM
Group BY with MIN, MAX
HAVING
We use
HAVING
clause to specify a search condition for a group or an aggregateThe
HAVING
clause is often used with theGROUP BY
clause to filter rows based on filter conditioncannot use column alias with having clause because it is evaluated before the
SELECT
statement
HAVING AGGREGATE_FUNCTION(column2) = value
HAVING AGGREGATE_FUNCTION(column2) >= value
HAVING vs WHERE
HAVING
works on result groupWHERE
works onSELECT
columns and not on the result group
Last updated