SQL TutorialLesson 34Free preview
SQL Group By
This lesson is part of SQL · All levels
Summary
GROUP BY groups rows that have the same value on one or more columns so that the aggregate function is calculated per group. For example, average prices per category or total spending per customer. The column used by GROUP BY must appear in SELECT if the aggregate function is not wrapped.
Key points
- GROUP BY groups the rows according to specific columns.
- The aggregate function is calculated separately for each group.
- Non-aggregate columns in SELECT are required to participate in GROUP BY.
- You can group multiple columns at once with a comma.
- To filter the group results, use HAVING, not WHERE.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.