Lesson 8 of 9
GROUP BY & Aggregates
GROUP BY groups rows that have the same values. Use aggregate functions like COUNT(), SUM(), AVG(), MAX(), and MIN() to calculate values for each group.
SQL
-- Count users per country
SELECT country, COUNT(*) as total_users
FROM users
GROUP BY country;
-- Average price per category
SELECT category, AVG(price) as avg_price
FROM products
GROUP BY category;
-- Having filters groups (like WHERE for groups)
SELECT country, COUNT(*) as total
FROM users
GROUP BY country
HAVING total > 10;