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 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
CREATE INDEX idx_articles_tenant_created
ON articles (tenant_id, created_at) -- key columns: filter + sort
INCLUDE (id, title); -- payload columns to avoid heap fetch
```
SQL Server has the same `INCLUDE` syntax. MySQL doesn't have INCLUDE; you bake all columns into the key, paying for a wider index.
What "covering" buys you: a query like `SELECT id, title FROM articles WHERE tenant_id = ? ORDER BY created_at` runs as an index-only scan. No heap I/O. On a 100M-row table this can be 10-100x faster than a non-covering index that requires a heap fetch per row.
Trade-offs:
- Wider indexes use more disk and RAM (buffer pool pressure).
- Every INSERT / UPDATE / DELETE updates each index, so write amplification grows with index count and width.
- INCLUDE columns don't participate in ordering, so they don't widen the seek key - they only widen the leaf pages.
Postgres-specific: EXPLAIN shows `Index Only Scan` when covering works. If it shows `Index Scan` instead, the visibility map may be stale - run `VACUUM`. INCLUDE-style covering requires Postgres 11+.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
These apply to the pattern as a whole, not just this problem.