Last 3 Months Where Statement
Querying Data for the Last 3 Months in SQL

Learn how to construct SQL WHERE clauses to retrieve data from the last three months, covering various database systems and common pitfalls.
Retrieving data for a specific rolling period, such as the 'last 3 months', is a common requirement in data analysis and reporting. This article explores various SQL techniques to achieve this, focusing on different database systems like SQL Server, MySQL, PostgreSQL, and Oracle. We'll cover how to construct effective WHERE
clauses using date functions, ensuring your queries are both accurate and performant.
Understanding 'Last 3 Months'
The definition of 'last 3 months' can sometimes be ambiguous. Does it mean the last 90 days? Or the current month and the two preceding full months? Or the period starting exactly three months ago from today's date? Most commonly, it refers to a rolling window from the current date. For example, if today is October 26, 2023, 'last 3 months' would typically mean data from July 26, 2023, to October 26, 2023. However, some interpretations might include all of July, August, September, and October up to the current day. We will focus on the rolling window approach, which is generally more flexible and widely applicable.
flowchart TD A[Current Date] --> B{"Subtract 3 Months"} B --> C[Start Date of Period] C --> D[Filter Data] D --> E[Result: Data within last 3 months]
Conceptual flow for filtering data within the last 3 months.
SQL Server: DATEADD and GETDATE()
SQL Server provides robust date and time functions, making it straightforward to calculate rolling periods. The DATEADD
function is key here, allowing you to add or subtract specified time intervals from a date. GETDATE()
returns the current date and time.
SELECT *
FROM YourTable
WHERE YourDateColumn >= DATEADD(month, -3, GETDATE());
SQL Server query to get data from the last 3 months.
YourDateColumn
is indexed. This allows the database to quickly locate the relevant rows without scanning the entire table.MySQL: INTERVAL and CURDATE()
MySQL uses the INTERVAL
keyword with CURDATE()
(or NOW()
) to perform date arithmetic. CURDATE()
returns the current date without the time component, while NOW()
includes the time.
SELECT *
FROM YourTable
WHERE YourDateColumn >= CURDATE() - INTERVAL 3 MONTH;
MySQL query to get data from the last 3 months.
PostgreSQL: INTERVAL and NOW()
PostgreSQL also uses the INTERVAL
keyword, often combined with NOW()
(or CURRENT_DATE
) for date calculations. PostgreSQL's date arithmetic is very flexible.
SELECT *
FROM YourTable
WHERE YourDateColumn >= NOW() - INTERVAL '3 months';
PostgreSQL query to get data from the last 3 months.
Oracle: ADD_MONTHS and SYSDATE
Oracle Database provides the ADD_MONTHS
function to add or subtract months from a date, and SYSDATE
to get the current date and time.
SELECT *
FROM YourTable
WHERE YourDateColumn >= ADD_MONTHS(SYSDATE, -3);
Oracle query to get data from the last 3 months.
YourDateColumn
includes time, and you only compare against a date (e.g., CURDATE()
in MySQL), you might exclude data from the start of the 'start date' of your period. To include all data from the start date, ensure your comparison includes the time component or truncates the date.Handling Date-Only Columns vs. DateTime Columns
When your date column stores only dates (e.g., YYYY-MM-DD
) versus full timestamps (e.g., YYYY-MM-DD HH:MI:SS
), your WHERE
clause might need slight adjustments to ensure inclusivity. If YourDateColumn
is a DATE
type, the examples above work perfectly. If it's a DATETIME
or TIMESTAMP
type, and you want to include all records from the start of the 'start date', you might need to use TRUNC()
(Oracle), DATE()
(MySQL), or CAST(AS DATE)
(SQL Server/PostgreSQL) on the current date, or ensure your comparison includes the time component.
-- SQL Server (for DATETIME columns, to include full start day)
SELECT *
FROM YourTable
WHERE YourDateColumn >= DATEADD(month, -3, CAST(GETDATE() AS DATE));
-- MySQL (for DATETIME columns, to include full start day)
SELECT *
FROM YourTable
WHERE YourDateColumn >= DATE(NOW() - INTERVAL 3 MONTH);
-- PostgreSQL (for TIMESTAMP columns, to include full start day)
SELECT *
FROM YourTable
WHERE YourDateColumn >= (NOW() - INTERVAL '3 months')::date;
-- Oracle (for TIMESTAMP columns, to include full start day)
SELECT *
FROM YourTable
WHERE YourDateColumn >= TRUNC(ADD_MONTHS(SYSDATE, -3));
Adjusted queries for DATETIME/TIMESTAMP columns to include the full start day.