// dialect comparison

Date difference filters in WHERE

Compute the cutoff first, then compare. Don't wrap the column in a function.

Filed under Querying

The verdict

All five use the same shape, column >= computed constant, because that is the form an index on the column can serve. The DATEDIFF and age() versions read more naturally but evaluate a function per row. Put the arithmetic on the constant side.

Where they differ

"Created in the last 30 days" is a range predicate: created_at >= cutoff. Every engine has its own cutoff arithmetic (DATEADD, intervals, SYSDATE - 30) and its own date-difference function, which belongs in the SELECT list rather than the WHERE clause.

How each engine writes it

SQL Server

SELECT * FROM orders WHERE created_at >= DATEADD(DAY, -30, SYSDATETIME());

DATEDIFF(DAY, created_at, SYSDATETIME()) <= 30 can't seek an index on created_at; the bare-column range can.

PostgreSQL

SELECT * FROM orders WHERE created_at >= now() - INTERVAL '30 days';

Interval subtraction keeps the column bare; age(now(), created_at) <= INTERVAL '30 days' does not.

MySQL

SELECT * FROM orders WHERE created_at >= NOW() - INTERVAL 30 DAY;

DATEDIFF(NOW(), created_at) <= 30 can't use an index on created_at. DATE_SUB(NOW(), INTERVAL 30 DAY) is the same cutoff written as a function.

SQLite

SELECT * FROM orders WHERE created_at >= datetime('now', '-30 days');

datetime('now') is UTC text in 'YYYY-MM-DD HH:MM:SS' form, so stored values must use the same zone and layout. julianday(created_at) in the predicate defeats an index.

Oracle

SELECT * FROM orders WHERE created_at >= SYSDATE - 30;

DATE arithmetic counts days. For TIMESTAMP columns, SYSTIMESTAMP - INTERVAL '30' DAY keeps fractional seconds.

Questions that come up

Why is DATEDIFF(DAY, col, now()) <= 30 slow?

The function wraps the column, so the optimizer can't use an index on it and has to compute the function for every row. Rearranged as col >= cutoff, it becomes a range seek with the same result.

How do date-difference functions differ across engines?

SQL Server's DATEDIFF counts boundaries crossed, so Dec 31 23:59 to Jan 1 00:01 is 1 day. MySQL's DATEDIFF subtracts the date parts. Oracle DATE subtraction returns fractional days, and PostgreSQL date subtraction returns an integer while timestamp subtraction returns an interval. Don't port these expressions without checking the numbers.

Should I filter on 'last 30 days' or 'this month'?

They are different predicates: >= now() - 30 days is rolling, >= the first of the month is calendar-based. Compute the boundary from the current time with DATETRUNC(month, SYSDATETIME()) in SQL Server 2022+, date_trunc('month', now()) in PostgreSQL, DATE_FORMAT(NOW(), '%Y-%m-01') in MySQL, date('now', 'start of month') in SQLite or TRUNC(SYSDATE, 'MM') in Oracle.

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.