Candidate telemetry diagnostic, error autopsy, and step-by-step query construction walkthrough.
Live aggregated metrics across candidate sandbox attempts
14 solved
First attempt fail
Evaluated submissions
Median time to solve
Unlocked answer
The marketing team wants to identify above-average spenders within each country to target premium campaigns. Compare each customer's total spending against their country's average.
| 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) |
CustomerName (FirstName + LastName), Country, TotalSpent, CountryAvg, DifferenceYour result should have 13 rows with 5 columns: | CustomerName | Country | TotalSpent | CountryAvg | Difference | |--------------------|----------------|------------|------------|------------| | Richard Cunningham | USA | 47.62 | 40.24 | 7.38 | | Helena Holý | Czech Republic | 49.62 | 45.12 | 4.5 | | Fynn Zimmermann | Germany | 43.62 | 39.12 | 4.5 | | Frank Ralston | USA | 43.62 | 40.24 | 3.38 | | Julia Barnett | USA | 43.62 | 40.24 | 3.38 | | ... | ... | ... | ... | ... |
Double-counting metrics by using COUNT(*) after joining parent and child tables. When joining an invoice table with an invoice lines table, a single invoice multiplies across all its line items, causing COUNT(invoice_id) to return line counts instead of unique invoice counts.
Interviewers check whether you notice 1-to-many cardinality multiplication and use COUNT(DISTINCT col) or pre-aggregate child records before joining.
Construct the solution logically from first principles to avoid typical edge case pitfalls.
Identify the base table and apply preliminary WHERE filters.
FROM TableName WHERE is_active = true
Group by primary business keys and compute aggregate expressions.
SELECT category, COUNT(DISTINCT item_id) AS total_items, SUM(amount) AS revenue GROUP BY category
Order by specified metrics descending and apply limit clauses.
ORDER BY revenue DESC LIMIT 10;
WITH CustomerTotals AS (
SELECT c.CustomerId,
c.FirstName || ' ' || c.LastName AS CustomerName,
c.Country,
ROUND(SUM(i.Total), 2) AS TotalSpent
FROM Customer c
JOIN Invoice i ON c.CustomerId = i.CustomerId
GROUP BY c.CustomerId
),
CountryAvg AS (
SELECT Country, ROUND(AVG(TotalSpent), 2) AS AvgSpent
FROM CustomerTotals
GROUP BY Country
)
SELECT ct.CustomerName,
ct.Country,
ct.TotalSpent,
ca.AvgSpent AS CountryAvg,
ROUND(ct.TotalSpent - ca.AvgSpent, 2) AS Difference
FROM CustomerTotals ct
JOIN CountryAvg ca ON ct.Country = ca.Country
WHERE ct.TotalSpent > ca.AvgSpent
ORDER BY Difference DESC
LIMIT 15;Real code patterns candidates submit that fail the grading suite.
SELECT a.Name, COUNT(t.TrackId) FROM Artist a JOIN Album al ON a.ArtistId = al.ArtistId JOIN Track t ON al.AlbumId = t.AlbumId GROUP BY a.Name;
Three recurring syntax and semantic traps relevant to this problem domain.
Joining a fact table with child lines multiplies fact table rows, distorting sums and counts.
SELECT c.id, SUM(i.total) FROM customer c JOIN invoice i ON c.id = i.customer_id JOIN invoice_line il ON i.id = il.invoice_id -- ❌ Inflated SUM
SELECT c.id, SUM(i.total) FROM customer c JOIN invoice i ON c.id = i.customer_id GROUP BY c.id; -- ✅ Avoids line multiplication
Using COUNT(*) when duplicate rows exist due to joins counts duplicate records.
SELECT artist_id, COUNT(album_id) ... -- ❌ Counts duplicate occurrences
SELECT artist_id, COUNT(DISTINCT album_id) ... -- ✅ Distinct unique entities
In SQL, dividing integers like 5 / 10 results in 0. Cast at least one operand to FLOAT or NUMERIC.
SELECT solved_count / total_count AS rate ... -- ❌ Returns 0
SELECT CAST(solved_count AS FLOAT) / total_count AS rate ... -- ✅ Returns 0.5
Launch our in-browser coding environment. Run queries, view execution plans, and get instant comparative diff grading with no setup.