Chapter 5 · Lesson 1 of 3
Filtering by Date Ranges
Filter rows by date ranges with comparison operators and DATE literals, and return only the orders placed in one specific month.
Dates are easier to work with when you treat them as dates, not strings. SQL lets you compare them directly.
To find February orders, you can write:
SELECT order_id, order_date
FROM orders
WHERE order_date >= DATE '2024-02-01'
AND order_date < DATE '2024-03-01'This pattern is often safer than BETWEEN for month ranges, because the upper bound stays exclusive.
The difference matters most with timestamp values. If an order happened at 2024-02-29 18:30:00, an inclusive upper bound like BETWEEN DATE '2024-02-01' AND DATE '2024-02-29' can miss it depending on how the database casts the date.
You can use the same idea for weeks, quarters, or any other date window.
Sample data
This lesson loads the orders table (7 rows) into DuckDB in your browser with one click, so you can run every example without uploading anything.
Setup SQL for orders
CREATE OR REPLACE TABLE orders (
order_id INTEGER,
customer_name VARCHAR,
order_date DATE,
ship_date DATE,
amount DECIMAL(10,2)
);
INSERT INTO orders VALUES
(1, 'Alice', '2024-01-03', '2024-01-05', 1200),
(2, 'Bob', '2024-01-15', '2024-01-20', 850),
(3, 'Charlie', '2024-02-01', '2024-02-04', 640),
(4, 'Diana', '2024-02-18', '2024-02-25', 430),
(5, 'Eve', '2024-03-02', '2024-03-03', 1500),
(6, 'Frank', '2024-03-28', '2024-04-02', 920),
(7, 'Grace', '2024-04-10', '2024-04-15', 760);Practice challenge
Return the February 2024 orders with order_id and order_date, ordered by order_date.
Hint: Filter order_date from 2024-02-01 up to but not including 2024-03-01.
Run your query in the editor and the lesson checks the result automatically.
Lessons in Working with Dates
Filter by date ranges, extract parts of dates, and calculate the days between two dates in DuckDB SQL.
Related guides
SQL Date Queries: Date Ranges, EXTRACT, and DATE_DIFF Explained
Date queries become much easier when you filter with clear ranges, extract useful parts, and calculate durations directly in SQL.
SQL Cheat Sheet for Data Analysis: Filter, Group, Join, Dates, and Ranking
One pattern per common analysis question, from previewing a new table to ranking rows inside groups, each linked to a lesson where you can practice it.