Beyond SUM and AVG, SQL has aggregation tools most analysts never learn — and they're the ones that solve real problems. This chapter covers conditional aggregation, FILTER, and the GROUPING family.
COUNT, COUNT DISTINCT, COUNT(*)
| Form | Counts |
|---|---|
| COUNT(*) | all rows, including those with NULLs |
| COUNT(col) | rows where col IS NOT NULL |
| COUNT(DISTINCT col) | unique non-NULL values of col |
| COUNT(*) FILTER (...) | all rows matching the filter |
-- All orders, including NULL customersSELECT COUNT(*) FROM orders;-- Orders with a known customerSELECT COUNT(customer_id) FROM orders;-- Distinct customers who ever orderedSELECT COUNT(DISTINCT customer_id) FROM orders;
Conditional aggregation
The single most-asked SQL technique in 2026 analyst interviews. Use a CASE inside an aggregate to count or sum only specific rows.
-- Pivot order status counts on one row per regionSELECTregion,COUNT(*) AS total_orders,SUM(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) AS paid,SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) AS pending,SUM(CASE WHEN status = 'cancelled' THEN 1 ELSE 0 END) AS cancelled,SUM(CASE WHEN status = 'paid' THEN amount ELSE 0 END) AS paid_revenueFROM ordersGROUP BY region;
FILTER (WHERE …) — the modern syntax
Postgres, SQL Server, BigQuery and others support FILTER (WHERE …), which is much cleaner than CASE-inside-SUM:
SELECTregion,COUNT(*) AS total_orders,COUNT(*) FILTER (WHERE status = 'paid') AS paid,COUNT(*) FILTER (WHERE status = 'pending') AS pending,SUM(amount) FILTER (WHERE status = 'paid') AS paid_revenueFROM ordersGROUP BY region;
GROUPING SETS, ROLLUP, CUBE
Compute multiple GROUP BY combinations in one pass:
-- Subtotals by region, plus a grand total row at the bottomSELECTregion,SUM(amount) AS revenueFROM ordersGROUP BY ROLLUP(region);-- region | revenue-- ----------+----------- North | 12000-- South | 18000-- West | 15000-- NULL | 45000 ← grand total-- Subtotals at every combination of region × monthSELECT region, month, SUM(amount)FROM ordersGROUP BY CUBE(region, month);-- Custom combinations — only the ones you wantSELECT region, status, month, SUM(amount)FROM ordersGROUP BY GROUPING SETS ((region),(status),(region, status),() -- the grand total);
Real-world patterns
1. Funnel analysis
SELECTCOUNT(DISTINCT user_id) AS visited,COUNT(DISTINCT user_id) FILTER (WHERE event = 'add_to_cart') AS added_to_cart,COUNT(DISTINCT user_id) FILTER (WHERE event = 'checkout') AS reached_checkout,COUNT(DISTINCT user_id) FILTER (WHERE event = 'purchase') AS purchasedFROM eventsWHERE event_date = CURRENT_DATE;
2. Cohort retention shell
WITH first_seen AS (SELECT user_id, DATE_TRUNC('month', MIN(event_at)) AS cohortFROM events GROUP BY user_id)SELECTcohort,COUNT(DISTINCT f.user_id) AS cohort_size,COUNT(DISTINCT e.user_id) FILTER (WHERE e.event_at < f.cohort + INTERVAL '7 days') AS active_w1,COUNT(DISTINCT e.user_id) FILTER (WHERE e.event_at < f.cohort + INTERVAL '30 days') AS active_m1FROM first_seen fLEFT JOIN events e ON e.user_id = f.user_idGROUP BY cohort;
Key takeaways
- 01COUNT(*) counts rows; COUNT(col) skips NULLs; COUNT(DISTINCT col) deduplicates.
- 02Conditional aggregation (CASE-inside-SUM or FILTER WHERE) is the workhorse of analytics SQL.
- 03FILTER (WHERE …) reads better than CASE — use it where supported.
- 04ROLLUP gives subtotals + grand total; CUBE gives every combination; GROUPING SETS lets you pick.
- 05Funnels, cohorts, and pivot tables all reduce to conditional aggregation patterns.
