Candidate telemetry diagnostic, error autopsy, and step-by-step query construction walkthrough.
Live aggregated metrics across candidate sandbox attempts
22 solved
First attempt fail
Evaluated submissions
Median time to solve
Unlocked answer
For each manager, compute the total sales their entire organizational subtree generated (the manager's direct reports, their reports, and so on).
| Column | Type |
|---|---|
| EmployeeId | INTEGER (PK) |
| LastName | TEXT NOT NULL |
| FirstName | TEXT NOT NULL |
| Title | TEXT |
| ReportsTo | INTEGER (FK → Employee) |
| BirthDate | TIMESTAMP |
| HireDate | TIMESTAMP |
| Country | TEXT |
| Column | Type |
|---|---|
| CustomerId | INTEGER (PK) |
| FirstName | TEXT NOT NULL |
| LastName | TEXT NOT NULL |
| Company | TEXT |
| Country | TEXT |
| TEXT NOT NULL | |
| SupportRepId | INTEGER (FK → Employee) |
| Column | Type |
|---|---|
| InvoiceId | INTEGER (PK) |
| CustomerId | INTEGER (FK) |
| InvoiceDate | TIMESTAMP NOT NULL |
| BillingCountry | TEXT |
| Total | NUMERIC(10,2) NOT NULL |
WITH RECURSIVE CTE to build the manager → subordinate transitive closureCustomer.SupportRepIdManagerName, ManagerTitle, SubtreeSales (rounded to 2 decimals)Your query should return 2 rows with 3 columns: | managername | managertitle | subtreesales | |---------------|-----------------|--------------| | Andrew Adams | General Manager | 2328.6 | | Nancy Edwards | Sales Manager | 2328.6 |
Using INNER JOIN instead of LEFT JOIN when joining primary records to optional child tables. If an entity has zero associated transactions or invoices, an INNER JOIN silently purges that row from the report, resulting in understated counts and skewed analytical aggregates.
Interviewers test whether you can recognize cardinality relationships (1:1, 1:N, N:M), understand table key constraints, and avoid unintentional data loss or cartesian explosion.
Construct the solution logically from first principles to avoid typical edge case pitfalls.
Establish which table defines the primary grain of the query (e.g. Customers, Invoices, or Artists).
FROM PrimaryTable pt
Attach related tables using clear join conditions on primary and foreign key pairs.
LEFT JOIN ForeignTable ft ON pt.id = ft.foreign_key
Filter required subsets and qualify column names with unambiguous table aliases.
WHERE pt.is_active = true ORDER BY pt.name ASC;
WITH RECURSIVE subtree AS (
-- Anchor: managers (have direct reports)
SELECT EmployeeId AS root_manager, EmployeeId AS member_id
FROM Employee
WHERE EmployeeId IN (SELECT DISTINCT ReportsTo FROM Employee WHERE ReportsTo IS NOT NULL)
UNION ALL
-- Recurse: include all descendants
SELECT s.root_manager, e.EmployeeId
FROM subtree s
JOIN Employee e ON e.ReportsTo = s.member_id
)
SELECT m.FirstName || ' ' || m.LastName AS ManagerName,
m.Title AS ManagerTitle,
ROUND(SUM(i.Total), 2) AS SubtreeSales
FROM subtree s
JOIN Employee m ON s.root_manager = m.EmployeeId
JOIN Customer c ON c.SupportRepId = s.member_id
JOIN Invoice i ON i.CustomerId = c.CustomerId
GROUP BY m.EmployeeId, m.FirstName, m.LastName, m.Title
ORDER BY SubtreeSales DESC;Real code patterns candidates submit that fail the grading suite.
SELECT Name, InvoiceId, Total FROM Customer JOIN Invoice ON CustomerId = CustomerId;
Three recurring syntax and semantic traps relevant to this problem domain.
INNER JOIN drops rows from the left table if there is no matching foreign key in the right table. For inclusive reports, use LEFT JOIN.
SELECT c.name, COUNT(o.id) FROM customers c JOIN orders o ON c.id = o.customer_id GROUP BY c.name; -- ❌ Drops customers with 0 orders
SELECT c.name, COUNT(o.id) FROM customers c LEFT JOIN orders o ON c.id = o.customer_id GROUP BY c.name; -- ✅ Includes all customers
Adding a WHERE filter on a column from the right table of a LEFT JOIN converts it into an INNER JOIN because NULL rows fail the WHERE predicate.
FROM customers c LEFT JOIN orders o ON c.id = o.customer_id WHERE o.status = 'active'; -- ❌ Discards NULLs, acting like INNER JOIN
FROM customers c LEFT JOIN orders o ON c.id = o.customer_id AND o.status = 'active'; -- ✅ Preserves all customers
Joining two tables on non-unique keys without sufficient composite constraints multiplies rows exponentially, inflating SUM and COUNT results.
FROM users u JOIN user_tags t ON u.id = t.user_id JOIN user_roles r ON u.id = r.user_id -- ❌ M*N row explosion
Aggregate child tables in separate CTEs before joining to the parent entity.
Launch our in-browser coding environment. Run queries, view execution plans, and get instant comparative diff grading with no setup.