GROUP BY / HAVING
command
GROUP BY groups rows by column values; HAVING filters the grouped results (like WHERE for aggregates).
Syntax
GROUP BY column HAVING aggregate_condition
Example
sql
-- Count users per role
SELECT role, COUNT(*) AS total
FROM users
GROUP BY role;
-- Revenue per category
SELECT category_id, SUM(price) AS revenue
FROM orders
GROUP BY category_id
HAVING revenue > 1000
ORDER BY revenue DESC;