← All posts
postMay 16, 2026
SQL GROUP BY — aggregating like a Data Engineer
#sql#group-by#aggregations
sql
SELECT
country,
COUNT(*) AS total_users,
AVG(age) AS avg_age
FROM users
WHERE created_at >= '2026-01-01'
GROUP BY country
ORDER BY total_users DESC;GROUP BY collapses many rows into one row per group. You pair it with aggregate functions like COUNT, SUM, AVG, MIN, and MAX to compute summaries.
Almost every business question becomes a GROUP BY query: how many sales per month, how many users per country, average response time per endpoint. If you understand GROUP BY plus the five aggregates, you can answer most business questions in SQL.