Aggregation intro
DraftAggregates reduce many rows into summaries. GROUP BY decides the level of detail: one row for the whole table, or one row per group.
Count and sum
Aggregate functions collapse many rows into one summary row.
Loaded Database:aggregation-intro.database-init
SELECT COUNT(*) AS orders, SUM(total_cents) AS revenue_cents
FROM orders;Loading plan...Group by city
GROUP BY returns one summary row for each distinct city.
Loaded Database:aggregation-intro.database-init
SELECT city, COUNT(*) AS orders, SUM(total_cents) AS revenue_cents
FROM orders
GROUP BY city
ORDER BY city;Loading plan...Filter groups
HAVING filters grouped rows after the aggregate has been computed.
Loaded Database:aggregation-intro.database-init
SELECT status, COUNT(*) AS orders
FROM orders
GROUP BY status
HAVING COUNT(*) > 1
ORDER BY status;Loading plan...