Candidate telemetry diagnostic, error autopsy, and step-by-step query construction walkthrough.
Live aggregated metrics across candidate sandbox attempts
20 solved
First attempt fail
Evaluated submissions
Median time to solve
Unlocked answer
Apply the Pareto principle (80/20 rule) to customer revenue. Find the top 20% of customers and show what percentage of total revenue they account for using cumulative sums.
| Column | Type |
|---|---|
| CustomerId | INTEGER (Primary Key) |
| FirstName | TEXT |
| LastName | TEXT |
| Company | TEXT |
| Address | TEXT |
| City | TEXT |
| State | TEXT |
| Country | TEXT |
| PostalCode | TEXT |
| Phone | TEXT |
| Fax | TEXT |
| TEXT | |
| SupportRepId | INTEGER (Foreign Key → Employee.EmployeeId) |
| Column | Type |
|---|---|
| InvoiceId | INTEGER (Primary Key) |
| CustomerId | INTEGER (Foreign Key → Customer.CustomerId) |
| InvoiceDate | TIMESTAMP |
| BillingAddress | TEXT |
| BillingCity | TEXT |
| BillingState | TEXT |
| BillingCountry | TEXT |
| BillingPostalCode | TEXT |
| Total | NUMERIC(10,2) |
ROW_NUMBER() and COUNT(*) OVER () to find the top 20%CumulativeRevenue and CumulativePercent using window SUMcustomername | totalspent | spendrank | cumulativerevenue | cumulativepercent ----------------------------------------------------------------------------- Helena Holý | 49.62 | 1 | 49.62 | 9.3 Richard Cunningham | 47.62 | 2 | 97.24 | 18.23 Luis Rojas | 46.62 | 3 | 143.86 | 26.97 Ladislav Kovács | 45.62 | 4 | 189.48 | 35.52 Hugh O'Reilly | 45.62 | 5 | 235.1 | 44.07 Julia Barnett | 43.62 | 6 | 278.72 | 52.25 Fynn Zimmermann | 43.62 | 7 | 322.34 | 60.43 Frank Ralston | 43.62 | 8 | 365.96 | 68.6 ... (12 rows total)
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 CustomerRevenue AS (
SELECT c.CustomerId,
c.FirstName || ' ' || c.LastName AS CustomerName,
...
)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 CustomerRevenue AS (
SELECT c.CustomerId,
c.FirstName || ' ' || c.LastName AS CustomerName,
ROUND(SUM(i.Total), 2) AS TotalSpent,
ROW_NUMBER() OVER (ORDER BY SUM(i.Total) DESC) AS SpendRank,
COUNT(*) OVER () AS TotalCustomers
FROM Customer c
JOIN Invoice i ON c.CustomerId = i.CustomerId
GROUP BY c.CustomerId
)
SELECT CustomerName,
TotalSpent,
SpendRank,
ROUND(SUM(TotalSpent) OVER (ORDER BY SpendRank ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW), 2) AS CumulativeRevenue,
ROUND(SUM(TotalSpent) OVER (ORDER BY SpendRank ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) * 100.0 / SUM(TotalSpent) OVER (), 2) AS CumulativePercent
FROM CustomerRevenue
WHERE SpendRank <= CAST(TotalCustomers * 0.2 AS INTEGER)
ORDER BY SpendRank;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.