GROUP BY & HAVING

GROUP BY

The GROUP BY statement groups rows that have the same values into summary rows. This statement is often used with aggregate functions like COUNT(), MAX() , MIN() , SUM() , AVG() to group the result-set by one or more columns.

Syntax:

SELECT <column name(s)>
FROM table name
WHERE condition
GROUP BY <column name(s)>
ORDER BY <column name(s)>;

HAVING

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.

Syntax:

SELECT <column name(s)>
FROM table name
WHERE condition
GROUP BY column name(s)
HAVING condition
ORDER BY column name(s);

Leave a comment

Create a website or blog at WordPress.com

Up ↑

Design a site like this with WordPress.com
Get started