Explore Library
Code Quiz

Non-Sargable WHERE Kills the Index

A join query has an index that the optimizer refuses to use because of how the WHERE clause is written.

Codesql
CREATE INDEX idx_orders_created ON orders(created_at);

SELECT o.id, c.name, o.total
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE YEAR(o.created_at) = 2024
ORDER BY o.total DESC;

The idx_orders_created index exists but the query still does a full scan on orders. What is the bug?