Grouping and Aggregates

Chapter 5, “Sorting Data and Other Selection Techniques,” discussed some SELECT features: the DISTINCT keyword and the ORDER BY clause. It also introduced the aggregate functions. There, aggregates were used only with the table as a whole—but that's just a small part of the aggregate story. In real life, aggregate functions are most frequently used in combination with the GROUP BY clause.

This chapter focuses on GROUP BY, which returns groups of rows, and on the HAVING clause, which puts conditions on GROUP BY results much as WHERE qualifies individual rows. The chapter concludes with a recapitulation of everything you need to know about null values.

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.