SQL Quest › SQL Interview Questions › Subqueries & CTEs
UNION ALL Dedup: Cross-Dataset Search
Combine employee and customer data into a single contact list. Using UNION ALL, create a result with name, source ('employee' or 'customer'), and detail (department for employees, membership for customers). Then wrap it in a CTE and count how many contacts exist per source. The final output has TWO columns — source and contact_count — with three rows: one per source, plus a summary ROW labeled 'total' (source = 'total', contact_count = sum of all contacts). Place the 'total' row last; order the other rows alphabetically by source. This tests UNION ALL vs UNION semantics, which matters when datasets may overlap.
Solve it in the browser editor →
Runs on SQLite in your browser, graded against the expected result, no signup. A wrong answer gets a diagnosis, not just "incorrect".
Schema
customers
| customer_id | name | signup_date | membership | total_orders | |
|---|---|---|---|---|---|
| 1 | John Smith | john.smith@email.com | 2023-01-15 | Gold | 15 |
| 2 | Emma Wilson | emma.wilson@email.com | 2023-03-20 | Silver | 8 |
| 3 | Michael Brown | michael.brown@email.com | 2023-02-10 | Gold | 12 |
Expected output: employee: 50, customer: 16, total: 66 (or less if UNION deduplicates)
Hint
Concepts
SELECT UNION ALL UNION CTE CASE GROUP BY UNION / Set Operations
Practise the topic: SQL practice questions · CTE practice · CASE WHEN practice · GROUP BY exercises
In these company practice sets
Databricks · Plaid · Snowflake
A SQL Quest challenge matched to patterns reported for these companies — not a question any of them has published.
Related questions
Where would this cost you points in an interview?
Ten questions, no signup: a Skillmap across nine SQL skills and the one to fix first.
Take the readiness test