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.
Compute the cutoff first, then compare. Don't wrap the column in a function.
Filed under Querying
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.
"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.
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.
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.
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.
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.
SELECT * FROM orders WHERE created_at >= SYSDATE - 30;
DATE arithmetic counts days. For TIMESTAMP columns, SYSTIMESTAMP - INTERVAL '30' DAY keeps fractional seconds.
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.
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.
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.
BETWEEN with a date-only upper bound keeps midnight and drops the rest of the day.
YEAR() or EXTRACT() on the column hides it from its index. Rewrite as a range.
Four engines convert silently, one refuses, and the conversions differ.
SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.