GROUP BY Multiple Columns
Group data by multiple columns to get more granular insights. Calculate the number of invoices and total revenue by...
Answer "by month", "last 30 days" and "days between" questions correctly.
Almost every reporting question is a date question: revenue by month, signups in the last 30 days, days between an order and its delivery. Getting it right means truncating timestamps to the right grain, extracting parts with EXTRACT, formatting with TO_CHAR, and doing arithmetic with intervals rather than subtracting raw strings. These questions work with real timestamp columns so the edge cases, month boundaries and time zones behave the way they do in production.
Group data by multiple columns to get more granular insights. Calculate the number of invoices and total revenue by...
Finance needs a revenue growth analysis showing monthly revenue alongside the previous month and the percentage growth...
The finance team needs a cumulative revenue growth chart for key markets. Show monthly revenue alongside the running...
The retention team wants to understand purchase frequency patterns. Calculate the average, minimum, and maximum gap (in...
The analytics team wants to track how genre popularity changes over time. Calculate the year-over-year revenue change...
Report the month-by-month invoice activity for fiscal year 2012. Schema: Invoice ColumnType InvoiceIdINTEGER (PK)...
Find customers who have purchased across multiple calendar years — a proxy for loyalty. Schema: Customer ColumnType...
Compute each employee's tenure (years between HireDate and the fixed reference date 2026-06-12) and bucket them by...
Show quarter-by-quarter revenue per genre across calendar years 2012 and 2013 for the four largest genres. Schema:...
Show how many customers made their first-ever purchase each month — the new-customer cohort series. Schema: Invoice...
Compute cohort retention: for each customer signup-month (first invoice), how many of that cohort were active in month...
Produce a monthly report combining MoM revenue growth with the top-spending customer in that month. Schema: Customer...
Segment customers into quartiles by lifetime value and compute days since their most recent purchase. Schema: Customer...
For each customer, compute their shortest gap (in days) between any two consecutive purchases. This surfaces the most...
Pivot revenue per billing country across days of the week — useful for spotting weekend-heavy markets. Schema: Invoice...
FAQ
Short answers to what people get stuck on most. Tap a question to expand it.
Truncate the timestamp to the month, then group by the result.
SELECT DATE_TRUNC('month', created_at) AS month,
SUM(amount) AS revenue
FROM orders
GROUP BY 1
ORDER BY 1;Subtract dates for a day count, and compare against CURRENT_DATE minus an interval.
SELECT order_id,
delivered_at::date - ordered_at::date AS days_to_deliver
FROM orders
WHERE ordered_at >= CURRENT_DATE - INTERVAL '30 days';Timestamps are stored in UTC and shown in your session time zone, so day and month boundaries can shift.
SELECT DATE_TRUNC('day',
created_at AT TIME ZONE 'Asia/Kolkata') AS day,
COUNT(*) AS orders
FROM orders
WHERE created_at >= '2026-01-01'
AND created_at < '2026-02-01'
GROUP BY 1;Track your streak & earn XP
Sign up free to unlock your progress dashboard, daily streaks, and leaderboard ranking.
10 role-based tests for Data Analysts, SQL Developers & Data Engineers with instant scorecards.