Month-over-Month Revenue & Top Customer
Produce a monthly report combining MoM revenue growth with the top-spending customer in that month. Schema: Customer...
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 genres present in every calendar year of available data, compute year-over-year revenue growth. Schema: InvoiceLine...
For each genre, find the top 3 tracks by units sold. Schema: InvoiceLine ColumnType InvoiceLineIdINTEGER (PK)...
For each customer, compute their shortest gap (in days) between any two consecutive purchases. This surfaces the most...
Apply the 80/20 rule: identify the smallest set of customers that together generate at least 80% of total revenue....
For each manager, compute the total sales their entire organizational subtree generated (the manager's direct reports,...
Classic RFM segmentation: score every customer on Recency (R), Frequency (F), and Monetary (M), then assign a segment...
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...
HR wants each employee's complete reporting path from themselves up to the top of the org, plus how many levels above...
Using a CTE, compute each album's track count, then return only albums whose track count exceeds the average track...
HR wants a headcount view: for every employee, how many people report to them in total, counting both direct reports...
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.
Name a step with WITH and use it like a table in the rest of the query.
A common table expression is a named result set defined with WITH at the top of a query and referenced like a table further down. CTEs let a long query be written as a sequence of readable steps instead of nested subqueries, and a recursive CTE can walk a hierarchy or generate a series of dates. In interviews they are usually the difference between an answer a reviewer can follow and one they have to unpick from the inside out.
FAQ
Short answers to what people get stuck on most. Tap a question to expand it.
A CTE (common table expression) is a named result defined with WITH that the rest of the query can use like a table.
WITH paid AS (
SELECT * FROM orders WHERE status = 'paid'
),
by_customer AS (
SELECT customer_id, SUM(amount) AS total
FROM paid
GROUP BY customer_id
)
SELECT * FROM by_customer WHERE total > 500;Not by itself. A CTE mainly makes the query easier to read.
Use WITH RECURSIVE when rows point to other rows in the same table, like managers and their reports.
WITH RECURSIVE chain AS (
SELECT id, manager_id, 1 AS depth
FROM employees WHERE id = 7
UNION ALL
SELECT e.id, e.manager_id, c.depth + 1
FROM employees e
JOIN chain c ON e.id = c.manager_id
)
SELECT * FROM chain;