We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A hard sql problem, graded against 3 test cases (1 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 region,
SUM(CASE WHEN quarter = 'Q1' THEN amount ELSE 0 END) AS q1,
SUM(CASE WHEN quarter = 'Q2' THEN amount ELSE 0 END) AS q2,
SUM(CASE WHEN quarter = 'Q3' THEN amount ELSE 0 END) AS q3,
SUM(CASE WHEN quarter = 'Q4' THEN amount ELSE 0 END) AS q4
FROM Sales
GROUP BY region
ORDER BY region;
```
Engine-specific alternatives:
- SQL Server: `PIVOT (SUM(amount) FOR quarter IN ([Q1],[Q2],[Q3],[Q4])) p`
- Postgres: `crosstab` from the `tablefunc` extension.
- Oracle: native `PIVOT` clause.
The conditional-aggregation form works everywhere and reads clearly. Use engine-native pivot only when the column set is dynamic and large.
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.