Playlist Genre Coverage
Identify playlists with the broadest genre diversity for the "Discover" carousel. Schema: Playlist ColumnType...
Identify playlists with the broadest genre diversity for the "Discover" carousel. Schema: Playlist ColumnType...
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...
For genres present in every calendar year of available data, compute year-over-year revenue growth. Schema: InvoiceLine...
For each customer, compute their shortest gap (in days) between any two consecutive purchases. This surfaces the most...
For each manager, compute the total sales their entire organizational subtree generated (the manager's direct reports,...
Pivot revenue per billing country across days of the week — useful for spotting weekend-heavy markets. Schema: Invoice...
Using a CTE, first compute each customer's total spending (sum of their invoice totals). Then, from that CTE, return...
Pricing wants a per-genre breakdown of how many tracks are sold at the standard $0.99 rate versus the premium rate...
Using a CTE, compute each album's track count, then return only albums whose track count exceeds the average track...
Compute each artist's total track count (across all their albums) and rank artists by that count, highest first, using...
Compute each customer's total number of invoices, then rank customers by that count (most invoices first) using...
Ops wants a Pareto-style view of genre revenue: compute each genre's total revenue, then -- ordered from...
The catalog team wants a per-genre breakdown of tracks across the 3 most common media types, using conditional...
Pre-aggregated mart: total MRR per plan per month of start_date. Return month, plan, total_mrr. Order by month, plan.
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.
Collapse many rows into one summary row per group.
Aggregation collapses many rows into one summary row per group: GROUP BY names the grouping columns, and SUM, COUNT, AVG, MIN and MAX describe each group. HAVING then filters the groups themselves, which is why it can reference an aggregate while WHERE cannot. This is the largest topic in the question bank because almost every analyst task, from revenue by month to active users per plan, is an aggregation with the right grouping key.
FAQ
Short answers to what people get stuck on most. Tap a question to expand it.
WHERE filters rows before grouping. HAVING filters groups after aggregation.
SELECT region, SUM(amount) AS total
FROM orders
WHERE status = 'paid' -- row filter, before grouping
GROUP BY region
HAVING SUM(amount) > 1000; -- group filter, after groupingThey count different things: all rows, non-NULL values, or unique non-NULL values.
Every selected column must be in GROUP BY or inside an aggregate, because the database cannot pick one value for the whole group.
SELECT COUNT(*) AS order_rows,
COUNT(coupon_code) AS orders_with_coupon,
COUNT(DISTINCT user_id) AS unique_customers
FROM orders;-- error: name is neither grouped nor aggregated
SELECT customer_id, name, SUM(amount) FROM sales GROUP BY customer_id;
-- fix: group by it as well
SELECT customer_id, name, SUM(amount) FROM sales GROUP BY customer_id, name;