Skip to main content
Albero Academy

SQL Aggregations Deep Dive

COUNT vs COUNT DISTINCT, conditional aggregation, GROUPING SETS, ROLLUP, CUBE, and the patterns that show up in real analytics work.

Chapter 5SQL14 min readBeginner friendly
  • #SQL
  • #Aggregations
  • #GROUP BY

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(*)

FormCounts
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
SQL
-- All orders, including NULL customers
SELECT COUNT(*) FROM orders;
-- Orders with a known customer
SELECT COUNT(customer_id) FROM orders;
-- Distinct customers who ever ordered
SELECT 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.

SQL
-- Pivot order status counts on one row per region
SELECT
region,
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_revenue
FROM orders
GROUP BY region;

FILTER (WHERE …) — the modern syntax

Postgres, SQL Server, BigQuery and others support FILTER (WHERE …), which is much cleaner than CASE-inside-SUM:

SQL
SELECT
region,
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_revenue
FROM orders
GROUP BY region;

GROUPING SETS, ROLLUP, CUBE

Compute multiple GROUP BY combinations in one pass:

SQL
-- Subtotals by region, plus a grand total row at the bottom
SELECT
region,
SUM(amount) AS revenue
FROM orders
GROUP BY ROLLUP(region);
-- region | revenue
-- ----------+---------
-- North | 12000
-- South | 18000
-- West | 15000
-- NULL | 45000 ← grand total
-- Subtotals at every combination of region × month
SELECT region, month, SUM(amount)
FROM orders
GROUP BY CUBE(region, month);
-- Custom combinations — only the ones you want
SELECT region, status, month, SUM(amount)
FROM orders
GROUP BY GROUPING SETS (
(region),
(status),
(region, status),
() -- the grand total
);

Real-world patterns

1. Funnel analysis

SQL
SELECT
COUNT(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 purchased
FROM events
WHERE event_date = CURRENT_DATE;

2. Cohort retention shell

SQL
WITH first_seen AS (
SELECT user_id, DATE_TRUNC('month', MIN(event_at)) AS cohort
FROM events GROUP BY user_id
)
SELECT
cohort,
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_m1
FROM first_seen f
LEFT JOIN events e ON e.user_id = f.user_id
GROUP BY cohort;

Key takeaways

  1. 01COUNT(*) counts rows; COUNT(col) skips NULLs; COUNT(DISTINCT col) deduplicates.
  2. 02Conditional aggregation (CASE-inside-SUM or FILTER WHERE) is the workhorse of analytics SQL.
  3. 03FILTER (WHERE …) reads better than CASE — use it where supported.
  4. 04ROLLUP gives subtotals + grand total; CUBE gives every combination; GROUPING SETS lets you pick.
  5. 05Funnels, cohorts, and pivot tables all reduce to conditional aggregation patterns.