Summary

Now you know all about grouping, a very useful element in SQL.

  • Divide data into groups with the GROUP BY clause. Like ORDER BY, GROUP BY allows nesting—that is, you can group within groups. However, GROUP BY is more restrictive than ORDER BY in the kinds of expressions it allows in the GROUP BY list. GROUP BY does some sorting as it builds groups, but don't make assumptions—the ordering tends to vary a great deal from system to system and even from release to release.

  • Limit the group results with the HAVING clause. Don't think of HAVING as a later form of WHERE. The two are different in terms of when they are processed. Use WHERE to remove row results before grouping and HAVING to remove group results after grouping. WHERE does not allow ...

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.