Candidate telemetry diagnostic, error autopsy, and step-by-step query construction walkthrough.
Live aggregated metrics across candidate sandbox attempts
26 solved
First attempt fail
Evaluated submissions
Median time to solve
Unlocked answer
For genres present in every calendar year of available data, compute year-over-year revenue growth.
| Column | Type |
|---|---|
| InvoiceLineId | INTEGER (PK) |
| InvoiceId | INTEGER (FK) |
| TrackId | INTEGER (FK) |
| UnitPrice | NUMERIC(10,2) NOT NULL |
| Quantity | INTEGER NOT NULL |
| Column | Type |
|---|---|
| TrackId | INTEGER (PK) |
| Name | TEXT NOT NULL |
| AlbumId | INTEGER (FK → Album) |
| MediaTypeId | INTEGER (FK → MediaType) |
| GenreId | INTEGER (FK → Genre) |
| Composer | TEXT |
| Milliseconds | INTEGER NOT NULL |
| Bytes | INTEGER |
| UnitPrice | NUMERIC(10,2) NOT NULL |
| Column | Type |
|---|---|
| GenreId | INTEGER (PK) |
| Name | TEXT |
| Column | Type |
|---|---|
| InvoiceId | INTEGER (PK) |
| CustomerId | INTEGER (FK) |
| InvoiceDate | TIMESTAMP NOT NULL |
| BillingCountry | TEXT |
| Total | NUMERIC(10,2) NOT NULL |
LAG() partitioned by genre to get the prior year revenueGenre, Year, Revenue, PrevYearRevenue, YoY_Growth_Pct (rounded to 2; NULL when no prior year)Your query should return 60 rows with 5 columns: | genre | year | revenue | prevyearrevenue | yoy_growth_pct | |--------------------|------|---------|-----------------|----------------| | Alternative & Punk | 2009 | 62.37 | NULL | NULL | | Alternative & Punk | 2010 | 39.6 | 62.37 | -36.51 | | Alternative & Punk | 2011 | 45.54 | 39.6 | 15.0 | | Alternative & Punk | 2012 | 38.61 | 45.54 | -15.22 | | Alternative & Punk | 2013 | 55.44 | 38.61 | 43.59 | | ... | ... | ... | ... | ... |
Attempting to filter window function output directly inside the WHERE clause (e.g., WHERE ROW_NUMBER() OVER (...) <= 3). Because SQL executes WHERE before evaluating window functions, this raises a syntax error or produces invalid groupings. The calculation must be staged in a CTE or subquery first.
Interviewers use this question to verify whether you understand the exact SQL execution order (FROM -> WHERE -> GROUP BY -> HAVING -> WINDOW -> SELECT -> ORDER BY), how to choose correctly between ROW_NUMBER, RANK, and DENSE_RANK when handling ties, and how to partition datasets without collapsing rows.
Construct the solution logically from first principles to avoid typical edge case pitfalls.
Determine whether the ranking or running total resets per customer, department, or genre (PARTITION BY), or spans the entire table.
OVER (PARTITION BY <group_col> ORDER BY <order_col> DESC)
Write a WITH clause to calculate the window metric alongside the base columns, ensuring all join and filter conditions are applied.
WITH RankedData AS (
WITH yearly AS (
SELECT g.GenreId, g.Name AS Genre,
EXTRACT(YEAR FROM i.InvoiceDate)::int AS Year,
R...
)Select from the CTE and apply the outer predicate (e.g., WHERE rnk = 1 or WHERE rnk <= N) to extract the final result set.
SELECT <columns> FROM RankedData WHERE rnk = 1 ORDER BY <columns>;
WITH yearly AS (
SELECT g.GenreId, g.Name AS Genre,
EXTRACT(YEAR FROM i.InvoiceDate)::int AS Year,
ROUND(SUM(il.UnitPrice * il.Quantity), 2) AS Revenue
FROM InvoiceLine il
JOIN Track t ON il.TrackId = t.TrackId
JOIN Genre g ON t.GenreId = g.GenreId
JOIN Invoice i ON il.InvoiceId = i.InvoiceId
GROUP BY g.GenreId, g.Name, EXTRACT(YEAR FROM i.InvoiceDate)
),
total_years AS (
SELECT COUNT(DISTINCT EXTRACT(YEAR FROM InvoiceDate)) AS n_years
FROM Invoice
),
stable_genres AS (
SELECT GenreId
FROM yearly
GROUP BY GenreId
HAVING COUNT(*) = (SELECT n_years FROM total_years)
)
SELECT y.Genre,
y.Year,
y.Revenue,
LAG(y.Revenue) OVER (PARTITION BY y.Genre ORDER BY y.Year) AS PrevYearRevenue,
CASE
WHEN LAG(y.Revenue) OVER (PARTITION BY y.Genre ORDER BY y.Year) IS NULL THEN NULL
WHEN LAG(y.Revenue) OVER (PARTITION BY y.Genre ORDER BY y.Year) = 0 THEN NULL
ELSE ROUND((y.Revenue - LAG(y.Revenue) OVER (PARTITION BY y.Genre ORDER BY y.Year)) * 100.0
/ LAG(y.Revenue) OVER (PARTITION BY y.Genre ORDER BY y.Year), 2)
END AS YoY_Growth_Pct
FROM yearly y
JOIN stable_genres s ON y.GenreId = s.GenreId
ORDER BY y.Genre, y.Year;Real code patterns candidates submit that fail the grading suite.
SELECT * FROM table_name WHERE ROW_NUMBER() OVER (ORDER BY amount DESC) <= 5;
SELECT department_id, employee_id, salary,
RANK() OVER (ORDER BY salary DESC) as rank
FROM employees;Three recurring syntax and semantic traps relevant to this problem domain.
Window functions cannot appear in WHERE or HAVING clauses. Filtering on a rank or running total requires wrapping the query in a CTE or subquery.
SELECT *, RANK() OVER (ORDER BY points DESC) as rnk FROM candidates WHERE RANK() OVER (ORDER BY points DESC) <= 5; -- ❌ Syntax Error
WITH Ranked AS ( SELECT *, RANK() OVER (ORDER BY points DESC) as rnk FROM candidates ) SELECT * FROM Ranked WHERE rnk <= 5; -- ✅ Correct
Using RANK() skips rank positions on ties (1, 2, 2, 4), whereas DENSE_RANK() retains consecutive integers (1, 2, 2, 3). Using ROW_NUMBER() arbitrarily breaks ties.
SELECT name, RANK() OVER (ORDER BY score DESC) as rnk ... -- ❌ Might miss 3rd rank if 2nd ties
SELECT name, DENSE_RANK() OVER (ORDER BY score DESC) as rnk ... -- ✅ Guaranteed continuous ranks
Forgetting the PARTITION BY clause causes ranking or rolling metrics to compute across the entire dataset rather than resetting per group/customer.
ROW_NUMBER() OVER (ORDER BY sale_date DESC) -- ❌ Global row number
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY sale_date DESC) -- ✅ Per-customer rank
Launch our in-browser coding environment. Run queries, view execution plans, and get instant comparative diff grading with no setup.