Summary - GROUP BY
- SQL course
- Intermediate
- Lesson 29/115
- 5 min
Write a category report with multi-column GROUP BY.
Summary - GROUP BY
GROUP BY groups rows by a shared value and produces an aggregate per group. Sorting is done with ORDER BY.
SELECT category, COUNT(*)
FROM products
GROUP BY category;Summary grouping query.
category, COUNT(*) is selected from products. Rows are grouped by category; aggregate functions are computed per group.
Practice Summary - GROUP BY
Summary: group products by category and count.
Starter code
-- summary group
SELECT category, COUNT(*)
FROM productsSample solution
SELECT category, COUNT(*)
FROM products
GROUP BY category;Expected output
category count Elektronik 3 Kitap 5
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What sorts after grouping?
- DISTINCT
- WHERE
- HAVING
- ORDER BY Correct answer
Explanation: ORDER BY does the sorting.
2 Summary - GROUP BY: match the terms
| Term | Meaning |
|---|---|
GROUP BY |
group |
COUNT |
count |
ORDER BY |
sort |
SUM |
sum |