// dialect comparison

Filtering by year or month without losing the index

YEAR() or EXTRACT() on the column hides it from its index. Rewrite as a range.

Filed under Querying

The verdict

The half-open range wins on every engine: it can use the plain index on the column, it ports with only the literal syntax changing, and it needs nothing extra. Use function-form predicates only where a matching expression index already exists, and spell the expression exactly as it was indexed.

Where they differ

Filtering on a date part tempts you into YEAR(col) = 2026 or EXTRACT(YEAR FROM col) = 2026. Both wrap the column and defeat its index. The sargable form is a half-open range on the column itself; expression indexes are the fallback when the function form must stay.

How each engine writes it

SQL Server

SELECT * FROM orders WHERE created_at >= '2026-01-01' AND created_at < '2027-01-01';

YEAR(created_at) = 2026 can't seek an index on created_at. A computed column (ALTER TABLE orders ADD order_year AS YEAR(created_at)) with its own index makes the function form seekable.

PostgreSQL

SELECT * FROM orders WHERE created_at >= DATE '2026-01-01' AND created_at < DATE '2027-01-01';

EXTRACT(YEAR FROM created_at) = 2026 needs an expression index: CREATE INDEX ON orders ((EXTRACT(YEAR FROM created_at))). That index is rejected on timestamptz, where EXTRACT depends on the session time zone.

MySQL

SELECT * FROM orders WHERE created_at >= '2026-01-01' AND created_at < '2027-01-01';

YEAR(created_at) = 2026 can't use an index on created_at; an 8.0.13+ functional index on ((YEAR(created_at))) can serve it.

SQLite

SELECT * FROM orders WHERE created_at >= '2026-01-01' AND created_at < '2027-01-01';

Works when created_at holds ISO-8601 text, which sorts chronologically. strftime('%Y', created_at) = '2026' can't use an index on the column.

Oracle

SELECT * FROM orders WHERE created_at >= DATE '2026-01-01' AND created_at < DATE '2027-01-01';

EXTRACT(YEAR FROM created_at) = 2026 needs a function-based index on exactly that expression.

Questions that come up

What does 'sargable' mean?

Search-argument-able: a predicate the optimizer can answer with an index seek because the indexed column stands bare on one side of the comparison. YEAR(col) = 2026 hides the column inside a function, so the engine evaluates it row by row.

How do I filter 'this month' sargably?

Compute the boundaries first, as in created_at >= '2026-11-01' AND created_at < '2026-12-01'. Functions that build the boundary (DATEFROMPARTS or DATETRUNC in T-SQL, date_trunc in PostgreSQL, TRUNC(SYSDATE, 'MM') in Oracle) go on the constant side and wrap the current date, never the column.

When is an expression index the better fix?

When you can't change the query (third-party or ORM-generated SQL) or many queries use the function form. The index adds write cost and only matches the exact expression, so treat it as the fallback.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.