We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A medium sql problem, graded against 4 test cases (2 of them hidden).
Query-writing problems - joins, aggregation, window functions, and query tuning.
Reach for it when you see: A schema and a question about the data rather than a function signature.
More SQLproblems →```sql
SELECT c.name
FROM Customers c
WHERE EXISTS (
SELECT 1 FROM Orders o
WHERE o.customer_id = c.id
AND o.total > 500
)
ORDER BY c.name;
```
EXISTS vs IN vs JOIN-DISTINCT:
| pattern | NULL-safe | short-circuits | duplicates |
|---|---|---|---|
| `WHERE EXISTS (...)` | yes | yes (stops at first match) | no |
| `WHERE id IN (subq)` | no - if subquery has NULL, NOT IN breaks | varies | no |
| `JOIN ... DISTINCT` | yes | no | needs DISTINCT to dedupe |
`EXISTS` is usually the safest choice: NULL-safe, semi-join-friendly to the optimizer, no dedupe needed. Modern optimizers often rewrite all three to the same plan, but `NOT IN` with a NULL-containing subquery is a classic correctness bug.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.