Rewrite a year filter so the index actually fires
The original query expresses the year filter as «extract the year from created_at and compare with 2024». The trouble: the column is wrapped in a function, the planner can't use the created_at index, and falls back to a full table scan.
Rewrite the filter as a date range so the planner can spot the b-tree index and pick it up (this form of condition is called «sargable»). Return the id, total, and creation date of orders placed in 2024, sort by id.
Expected output sample
This is what a correct answer looks like — its row count is its own, it doesn't have to match the schema tables.
| id | total | created_at |
|---|---|---|
| 1 | 100 | 2024-01-05T00:00:00+00:00 |
| 2 | 150 | 2024-01-12T00:00:00+00:00 |
| 3 | 200 | 2024-01-20T00:00:00+00:00 |
Sign in to see submission history
Sign inSign in to use AI Mentor
Sign in