SQL Dates and times
Dates are written like '2025-10-01' and can be compared directly or used with BETWEEN; YEAR/MONTH/DAY pull out the parts, DATEDIFF(later, earlier) counts the days between, and DATE_ADD(date, INTERVAL 7 DAY) moves a date forward.
Lots of questions about a shop are about dates. How many orders came in October? How long has a customer been with us? What's the latest day an order can ship? This lesson is about working with dates in SQL. First, here are the dates in the orders table:
1SELECT id, order_date2FROM orders;
| id | order_date |
|---|---|
| 1 | 2025-09-02 |
| 2 | 2025-09-05 |
| 3 | 2025-09-18 |
| 4 | 2025-09-21 |
| 5 | 2025-10-01 |
| 6 | 2025-10-03 |
| 7 | 2025-10-10 |
| 8 | 2025-10-12 |
| 9 | 2025-10-15 |
| 10 | 2025-10-20 |
| 11 | 2025-10-22 |
| 12 | 2025-10-25 |
What a date looks like
This stage is about the dates stored in the tables. In orders, order_date is the day the order was placed; in customers, joined is the day the customer signed up. Both are of type DATE, and they always look like year-month-day: a 4-digit year, a 2-digit month and a 2-digit day, separated by dashes, like 2025-09-02.
1SELECT name, joined2FROM customers;
| name | joined |
|---|---|
| Ana Silva | 2025-01-12 |
| Ben Okafor | 2025-02-03 |
| Chloe Martin | 2025-02-20 |
| David Kim | 2025-03-15 |
| Emma Novak | 2025-04-01 |
| Farah Khan | 2025-05-18 |
| George Brown | 2025-06-09 |
| Hana Sato | 2025-07-21 |
| Ivan Petrov | 2025-08-30 |
| Julia Costa | 2025-09-14 |
When you write a date in a query, put it in single quotes: '2025-09-02'. The database knows the column holds dates, so it can compare them, count the days between them and add days to them. You'll do all of that in this lesson.
Tip: Always use two digits for the month and day. Some places accept '2025-9-2', but mixing styles is asking for trouble; '2025-09-02' is always safe.
You want the orders placed on October 1, 2025. What goes after WHERE order_date = …?
2025-10-01 is read as a subtraction: 2025 minus 10 minus 1.Filtering by date
This stage is about picking rows by date. Comparing dates means comparing earlier and later, and the later date counts as "bigger", so the comparisons from lesson 2 and the AND and BETWEEN from lesson 3 all work. To get the October orders, ask for "October 1 or later, and before November 1":
1SELECT id, order_date2FROM orders3WHERE order_date >= '2025-10-01' AND order_date < '2025-11-01';
| id | order_date |
|---|---|
| 5 | 2025-10-01 |
| 6 | 2025-10-03 |
| 7 | 2025-10-10 |
| 8 | 2025-10-12 |
| 9 | 2025-10-15 |
| 10 | 2025-10-20 |
| 11 | 2025-10-22 |
| 12 | 2025-10-25 |
BETWEEN works too, and it includes both ends. The orders from September 15 to October 5:
1SELECT id, order_date2FROM orders3WHERE order_date BETWEEN '2025-09-15' AND '2025-10-05';
| id | order_date |
|---|---|
| 3 | 2025-09-18 |
| 4 | 2025-09-21 |
| 5 | 2025-10-01 |
| 6 | 2025-10-03 |
| id | order_date |
|---|---|
| 2 | 2025-09-05 |
| 3 | 2025-09-18 |
| 4 | 2025-09-21 |
| 6 | 2025-10-03 |
| 7 | 2025-10-10 |
WHERE order_date BETWEEN '2025-09-15' AND '2025-10-05'| id | order_date |
|---|---|
| 2 | 2025-09-05 |
| 3 | 2025-09-18 |
| 4 | 2025-09-21 |
| 6 | 2025-10-03 |
| 7 | 2025-10-10 |
result · 1before September 15result · 5after October 5
How many rows does the result have?
1SELECT id, order_date2FROM orders3WHERE order_date BETWEEN '2025-10-10' AND '2025-10-20';
BETWEEN includes both ends, so the ones on the 10th and the 20th count too.Fill in one keyword to get the customers who joined between April 1 and June 30.
1SELECT name, joined2FROM customers3WHERE joined '2025-04-01' AND '2025-06-30';
BETWEEN early AND late picks a range of days and includes both ends, so Emma Novak, who joined on April 1, is in.The September orders
✓Get all the orders from September 2025, showing the id and order_date columns.
| id | order_date |
|---|---|
| 1 | 2025-09-02 |
| 2 | 2025-09-05 |
| 3 | 2025-09-18 |
| 4 | 2025-09-21 |
1SELECT id, order_date2FROM orders;
Year, month and day: YEAR, MONTH, DAY
This stage is about taking one part out of a date. YEAR(date) gives the year, MONTH(date) the month and DAY(date) the day, all as numbers:
1SELECT id, order_date, YEAR(order_date), MONTH(order_date), DAY(order_date)2FROM orders;
| id | order_date | YEAR(order_date) | MONTH(order_date) | DAY(order_date) |
|---|---|---|---|---|
| 1 | 2025-09-02 | 2025 | 9 | 2 |
| 2 | 2025-09-05 | 2025 | 9 | 5 |
| 3 | 2025-09-18 | 2025 | 9 | 18 |
| 4 | 2025-09-21 | 2025 | 9 | 21 |
| 5 | 2025-10-01 | 2025 | 10 | 1 |
| 6 | 2025-10-03 | 2025 | 10 | 3 |
| 7 | 2025-10-10 | 2025 | 10 | 10 |
| 8 | 2025-10-12 | 2025 | 10 | 12 |
| 9 | 2025-10-15 | 2025 | 10 | 15 |
| 10 | 2025-10-20 | 2025 | 10 | 20 |
| 11 | 2025-10-22 | 2025 | 10 | 22 |
| 12 | 2025-10-25 | 2025 | 10 | 25 |
| id | order_date |
|---|---|
| 1 | 2025-09-02 |
| 5 | 2025-10-01 |
| 12 | 2025-10-25 |
SELECT id, YEAR(…), MONTH(…), DAY(…)| id | YEAR | MONTH | DAY |
|---|---|---|---|
| 1 | 2025 | 9 | 2 |
| 5 | 2025 | 10 | 1 |
| 12 | 2025 | 10 | 25 |
result · 1the numbers 9 and 2, with no leading 0
They work in WHERE too. Here's another way to get the September orders:
1SELECT id, order_date2FROM orders3WHERE MONTH(order_date) = 9;
| id | order_date |
|---|---|
| 1 | 2025-09-02 |
| 2 | 2025-09-05 |
| 3 | 2025-09-18 |
| 4 | 2025-09-21 |
Coming up: once you've learned grouping (GROUP BY) in lesson 9, you can group by MONTH(order_date) and count the orders of every month in one go.
How many rows does the result have?
1SELECT name, joined2FROM customers3WHERE MONTH(joined) = 2;
MONTH(joined) gives the number 2, which equals 2.Fill in a function to get the orders placed before the 10th of the month (the 1st to the 9th).
1SELECT id, order_date2FROM orders3WHERE (order_date) < 10;
DAY. With MONTH you'd compare the month with 10 and get all the September orders instead.Days between: DATEDIFF
This stage is about counting the days between two dates. DATEDIFF(later date, earlier date) gives the number of days between them. How many days had passed by October 31 since each order:
1SELECT id, order_date, DATEDIFF('2025-10-31', order_date) AS days_ago2FROM orders;
| id | order_date | days_ago |
|---|---|---|
| 1 | 2025-09-02 | 59 |
| 2 | 2025-09-05 | 56 |
| 3 | 2025-09-18 | 43 |
| 4 | 2025-09-21 | 40 |
| 5 | 2025-10-01 | 30 |
| 6 | 2025-10-03 | 28 |
| 7 | 2025-10-10 | 21 |
| 8 | 2025-10-12 | 19 |
| 9 | 2025-10-15 | 16 |
| 10 | 2025-10-20 | 11 |
| 11 | 2025-10-22 | 9 |
| 12 | 2025-10-25 | 6 |
| id | order_date |
|---|---|
| 8 | 2025-10-12 |
| 10 | 2025-10-20 |
| 12 | 2025-10-25 |
DATEDIFF('2025-10-31', order_date) AS days_ago| id | order_date | days_ago |
|---|---|---|
| 8 | 2025-10-12 | 19 |
| 10 | 2025-10-20 | 11 |
| 12 | 2025-10-25 | 6 |
result · 3the 25th to the 31st: 6 days
Remember the order: the later date first, the earlier one second. The other way round, you get a negative number: DATEDIFF('2025-09-02', '2025-10-01') is -29.
What's the result?
1SELECT DATEDIFF('2025-10-01', '2025-09-02');How long they've been with us
✓Get the customers who joined on or after July 1, showing name and joined plus a column days: the number of days from the day they joined to December 31, 2025.
| name | joined | days |
|---|---|---|
| Hana Sato | 2025-07-21 | 163 |
| Ivan Petrov | 2025-08-30 | 123 |
| Julia Costa | 2025-09-14 | 108 |
1SELECT name, joined2FROM customers3WHERE joined >= '2025-07-01';
What day is it: CURDATE and NOW
This stage is about getting "now". CURDATE() gives today's date, and NOW() gives the date and time right now, down to the second. Nothing goes in the brackets:
1SELECT CURDATE(), NOW();| CURDATE() | NOW() |
|---|---|
| 2026-10-28 | 2026-10-28 12:00:00 |
They're often used with DATEDIFF, for example to see how many days each customer has been with us up to today:
1SELECT name, joined, DATEDIFF(CURDATE(), joined) AS days2FROM customers;
| name | joined | days |
|---|---|---|
| Ana Silva | 2025-01-12 | 654 |
| Ben Okafor | 2025-02-03 | 632 |
| Chloe Martin | 2025-02-20 | 615 |
| David Kim | 2025-03-15 | 592 |
| Emma Novak | 2025-04-01 | 575 |
| Farah Khan | 2025-05-18 | 528 |
| George Brown | 2025-06-09 | 506 |
| Hana Sato | 2025-07-21 | 464 |
| Ivan Petrov | 2025-08-30 | 424 |
| Julia Costa | 2025-09-14 | 409 |
Watch out: a query that uses CURDATE() or NOW() gives a different result every day. Run it today and again tomorrow, and the days are 1 apart. If you want the same result every time, write the date yourself, as in DATEDIFF('2025-12-31', joined). In this course the sample database keeps "today" fixed at noon on 28 October 2026, so here everyone sees the same result every day; in a real database it is the day and time you run it.
In a real database: you run SELECT CURDATE(); today, and again tomorrow. What happens?
CURDATE() checks what day it is every time it runs, so its result changes with the days. The dates stored in the table don't change; only the value CURDATE() gives does.A different look: DATE_FORMAT
This stage is about showing a date in another style. DATE_FORMAT(date, 'format') writes the date as text following the format, where % plus a letter stands for one part of the date:
| Code | Meaning | For 2025-09-02 |
|---|---|---|
%Y | 4-digit year | 2025 |
%m | 2-digit month | 09 |
%d | 2-digit day | 02 |
%e | day without a leading 0 | 2 |
Everything else in the format is kept as it is. Day/month/year:
1SELECT id, order_date, DATE_FORMAT(order_date, '%d/%m/%Y') AS shown2FROM orders;
| id | order_date | shown |
|---|---|---|
| 1 | 2025-09-02 | 02/09/2025 |
| 2 | 2025-09-05 | 05/09/2025 |
| 3 | 2025-09-18 | 18/09/2025 |
| 4 | 2025-09-21 | 21/09/2025 |
| 5 | 2025-10-01 | 01/10/2025 |
| 6 | 2025-10-03 | 03/10/2025 |
| 7 | 2025-10-10 | 10/10/2025 |
| 8 | 2025-10-12 | 12/10/2025 |
| 9 | 2025-10-15 | 15/10/2025 |
| 10 | 2025-10-20 | 20/10/2025 |
| 11 | 2025-10-22 | 22/10/2025 |
| 12 | 2025-10-25 | 25/10/2025 |
There's also %M for the month's name: DATE_FORMAT(order_date, '%M %e, %Y') gives September 2, 2025.
What DATE_FORMAT gives you is text, just for show. To compare or sort dates, keep using the original date column.
What's the result?
1SELECT DATE_FORMAT('2025-10-03', '%m/%d/%Y');%m is 10, %d is 03 and %Y is 2025, and the / in between stays as it is.Moving a date: DATE_ADD and INTERVAL
This stage is about adding to and subtracting from dates. DATE_ADD(date, INTERVAL number unit) moves a date forward; the usual units are DAY, MONTH and YEAR. The shop promises to ship within 7 days of the order, so what's the latest day for each order?
1SELECT id, order_date, DATE_ADD(order_date, INTERVAL 7 DAY) AS deadline2FROM orders;
| id | order_date | deadline |
|---|---|---|
| 1 | 2025-09-02 | 2025-09-09 |
| 2 | 2025-09-05 | 2025-09-12 |
| 3 | 2025-09-18 | 2025-09-25 |
| 4 | 2025-09-21 | 2025-09-28 |
| 5 | 2025-10-01 | 2025-10-08 |
| 6 | 2025-10-03 | 2025-10-10 |
| 7 | 2025-10-10 | 2025-10-17 |
| 8 | 2025-10-12 | 2025-10-19 |
| 9 | 2025-10-15 | 2025-10-22 |
| 10 | 2025-10-20 | 2025-10-27 |
| 11 | 2025-10-22 | 2025-10-29 |
| 12 | 2025-10-25 | 2025-11-01 |
The end of a month rolls over by itself: October 25 plus 7 days is November 1. To go back, use DATE_SUB:
1SELECT DATE_SUB('2025-10-01', INTERVAL 1 DAY), DATE_ADD('2025-10-01', INTERVAL 1 MONTH);| DATE_SUB('2025-10-01', INTERVAL 1 DAY) | DATE_ADD('2025-10-01', INTERVAL 1 MONTH) |
|---|---|
| 2025-09-30 | 2025-11-01 |
One day before October 1 is September 30, and one month after is November 1.
Change only the number in the green box so that each order's deadline is 3 days after it was placed.
1SELECT id, order_date, DATE_ADD(order_date, INTERVAL DAY) AS deadline2FROM orders3WHERE id <= 3;
id | order_date | deadline 1 | 2025-09-02 | 2025-09-05 2 | 2025-09-05 | 2025-09-08 3 | 2025-09-18 | 2025-09-21
INTERVAL 3 DAY moves the date 3 days forward: September 2 becomes September 5.Latest shipping day
✓Get the orders from October 20 on, showing id and order_date plus a column deadline: the order date moved 7 days forward.
| id | order_date | deadline |
|---|---|---|
| 10 | 2025-10-20 | 2025-10-27 |
| 11 | 2025-10-22 | 2025-10-29 |
| 12 | 2025-10-25 | 2025-11-01 |
1SELECT id, order_date2FROM orders3WHERE order_date >= '2025-10-20';
What you learned
- A date is written
'year-month-day', in quotes, with two digits for the month and day. - Comparing dates means comparing earlier and later:
>=,<andBETWEENall pick a range of days, andBETWEENincludes both ends. YEAR,MONTHandDAYpull out the year, month and day as numbers, and work inWHEREtoo.DATEDIFF(later, earlier)counts the days between; the wrong order gives a negative number.CURDATE()is today andNOW()is right now; their results change every day.DATE_FORMAT(date, '%d/%m/%Y')changes how a date looks, and gives text.DATE_ADD(date, INTERVAL 7 DAY)moves a date forward, andDATE_SUBmoves it back.