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 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 id, total_spend,
NTILE(4) OVER (ORDER BY total_spend ASC, id ASC) AS quartile
FROM Customers
ORDER BY quartile, total_spend, id;
```
NTILE bucketing rule: with `n` rows split into `k` buckets, the first `n mod k` buckets get `ceil(n/k)` rows; the remaining buckets get `floor(n/k)` rows. Example: 10 rows / 4 buckets -> sizes 3, 3, 2, 2.
When to prefer percentiles instead: `NTILE` is positional, not value-based. Two customers with identical spend can land in different buckets. If you need value-based bucketing, use `PERCENT_RANK` or `CUME_DIST` with explicit thresholds.
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.