GROUP BY Syntax

Here's what GROUP BY looks like in the context of a SELECT statement:

SELECT select_list
FROM table_list
[ WHERE conditions ]
[ GROUP BY group_by_list ]
[ ORDER BY order_by_list ]

In most SQL dialects, every item in the GROUP BY list must appear in the SELECT list—you can make groups only out of things you select. Systems vary in what they allow in a GROUP BY list, in addition to column names: More complex expressions, display labels, and SELECT list position numbers are all possibilities. Following is an example with a single column in the GROUP BY clause. The query figures how many books each publisher has.

SQL
select pub_id, count(title_id)
from titles
group by pub_id pub_id count(titles.title_id) ====== ===================== ...

Get Practical SQL Handbook, The: Using SQL Variants, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.