GROUP BY Single and Multiple Columns
Group rows by one or more columns to produce subtotals and category breakdowns, and read GROUP BY query plans.
What GROUP BY Does
GROUP BY partitions rows into groups by one or more column values, then applies aggregates per group.
Single-Column Group
Total orders per user:
SELECT user_id, COUNT(*) AS order_count, SUM(total) AS revenue
FROM orders
GROUP BY user_id
ORDER BY revenue DESC;All lessons in this course
- COUNT, SUM, AVG, MIN, MAX
- GROUP BY Single and Multiple Columns
- HAVING vs WHERE
- Common Pitfalls: NULLs in Aggregates