Kodewick
Start learning →
Dates and times7 / 23

SQL Dates and times

Lesson 7 of 23 · Part 1 · Querying data · about 20 minutes

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:

main.sql
1SELECT id, order_date2FROM orders;
Output
idorder_date
12025-09-02
22025-09-05
32025-09-18
42025-09-21
52025-10-01
62025-10-03
72025-10-10
82025-10-12
92025-10-15
102025-10-20
112025-10-22
122025-10-25
12 rows

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.

main.sql
1SELECT name, joined2FROM customers;
Output
namejoined
Ana Silva2025-01-12
Ben Okafor2025-02-03
Chloe Martin2025-02-20
David Kim2025-03-15
Emma Novak2025-04-01
Farah Khan2025-05-18
George Brown2025-06-09
Hana Sato2025-07-21
Ivan Petrov2025-08-30
Julia Costa2025-09-14
10 rows

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.

Choose one

You want the orders placed on October 1, 2025. What goes after WHERE order_date = …?

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":

main.sql
1SELECT id, order_date2FROM orders3WHERE order_date >= '2025-10-01' AND order_date < '2025-11-01';
Output
idorder_date
52025-10-01
62025-10-03
72025-10-10
82025-10-12
92025-10-15
102025-10-20
112025-10-22
122025-10-25
8 rows

BETWEEN works too, and it includes both ends. The orders from September 15 to October 5:

main.sql
1SELECT id, order_date2FROM orders3WHERE order_date BETWEEN '2025-09-15' AND '2025-10-05';
Output
idorder_date
32025-09-18
42025-09-21
52025-10-01
62025-10-03
4 rows
orders
idorder_date
22025-09-05
32025-09-18
42025-09-21
62025-10-03
72025-10-10
WHERE order_date BETWEEN '2025-09-15' AND '2025-10-05'
result
idorder_date
22025-09-05
32025-09-18
42025-09-21
62025-10-03
72025-10-10
  1. result · 1before September 15
  2. result · 5after October 5
BETWEEN two dates compares earlier and later: both end days count, and anything outside is crossed out
Choose oneA wrong guess is fine

How many rows does the result have?

1SELECT id, order_date2FROM orders3WHERE order_date BETWEEN '2025-10-10' AND '2025-10-20';
Fill inTap one to fill a blank; tap a blank to take it back

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';
Exercise 1

The September orders

Get all the orders from September 2025, showing the id and order_date columns.

It should print
idorder_date
12025-09-02
22025-09-05
32025-09-18
42025-09-21
4 rows
exercise1.sql
1SELECT id, order_date2FROM orders;
Output
Press Run or Ctrl+Enter

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:

main.sql
1SELECT id, order_date, YEAR(order_date), MONTH(order_date), DAY(order_date)2FROM orders;
Output
idorder_dateYEAR(order_date)MONTH(order_date)DAY(order_date)
12025-09-02202592
22025-09-05202595
32025-09-182025918
42025-09-212025921
52025-10-012025101
62025-10-032025103
72025-10-1020251010
82025-10-1220251012
92025-10-1520251015
102025-10-2020251020
112025-10-2220251022
122025-10-2520251025
12 rows
orders
idorder_date
12025-09-02
52025-10-01
122025-10-25
SELECT id, YEAR(…), MONTH(…), DAY(…)
result
idYEARMONTHDAY
1202592
52025101
1220251025
  1. result · 1the numbers 9 and 2, with no leading 0
Each row's date is split into three numbers, giving three new columns; the date in the table stays the same

They work in WHERE too. Here's another way to get the September orders:

main.sql
1SELECT id, order_date2FROM orders3WHERE MONTH(order_date) = 9;
Output
idorder_date
12025-09-02
22025-09-05
32025-09-18
42025-09-21
4 rows

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.

Choose oneA wrong guess is fine

How many rows does the result have?

1SELECT name, joined2FROM customers3WHERE MONTH(joined) = 2;
Fill inTap one to fill a blank; tap a blank to take it back

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;

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:

main.sql
1SELECT id, order_date, DATEDIFF('2025-10-31', order_date) AS days_ago2FROM orders;
Output
idorder_datedays_ago
12025-09-0259
22025-09-0556
32025-09-1843
42025-09-2140
52025-10-0130
62025-10-0328
72025-10-1021
82025-10-1219
92025-10-1516
102025-10-2011
112025-10-229
122025-10-256
12 rows
orders
idorder_date
82025-10-12
102025-10-20
122025-10-25
DATEDIFF('2025-10-31', order_date) AS days_ago
result
idorder_datedays_ago
82025-10-1219
102025-10-2011
122025-10-256
  1. result · 3the 25th to the 31st: 6 days
DATEDIFF works row by row: October 31 minus that row's order date gives the number of 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.

Choose oneA wrong guess is fine

What's the result?

1SELECT DATEDIFF('2025-10-01', '2025-09-02');
Exercise 2

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.

It should print
namejoineddays
Hana Sato2025-07-21163
Ivan Petrov2025-08-30123
Julia Costa2025-09-14108
3 rows
exercise2.sql
1SELECT name, joined2FROM customers3WHERE joined >= '2025-07-01';
Output
Press Run or Ctrl+Enter

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:

main.sql
1SELECT CURDATE(), NOW();
Output
CURDATE()NOW()
2026-10-282026-10-28 12:00:00
1 row

They're often used with DATEDIFF, for example to see how many days each customer has been with us up to today:

main.sql
1SELECT name, joined, DATEDIFF(CURDATE(), joined) AS days2FROM customers;
Output
namejoineddays
Ana Silva2025-01-12654
Ben Okafor2025-02-03632
Chloe Martin2025-02-20615
David Kim2025-03-15592
Emma Novak2025-04-01575
Farah Khan2025-05-18528
George Brown2025-06-09506
Hana Sato2025-07-21464
Ivan Petrov2025-08-30424
Julia Costa2025-09-14409
10 rows

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.

Choose one

In a real database: you run SELECT CURDATE(); today, and again tomorrow. What happens?

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:

CodeMeaningFor 2025-09-02
%Y4-digit year2025
%m2-digit month09
%d2-digit day02
%eday without a leading 02

Everything else in the format is kept as it is. Day/month/year:

main.sql
1SELECT id, order_date, DATE_FORMAT(order_date, '%d/%m/%Y') AS shown2FROM orders;
Output
idorder_dateshown
12025-09-0202/09/2025
22025-09-0505/09/2025
32025-09-1818/09/2025
42025-09-2121/09/2025
52025-10-0101/10/2025
62025-10-0303/10/2025
72025-10-1010/10/2025
82025-10-1212/10/2025
92025-10-1515/10/2025
102025-10-2020/10/2025
112025-10-2222/10/2025
122025-10-2525/10/2025
12 rows

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.

Choose oneA wrong guess is fine

What's the result?

1SELECT DATE_FORMAT('2025-10-03', '%m/%d/%Y');

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?

main.sql
1SELECT id, order_date, DATE_ADD(order_date, INTERVAL 7 DAY) AS deadline2FROM orders;
Output
idorder_datedeadline
12025-09-022025-09-09
22025-09-052025-09-12
32025-09-182025-09-25
42025-09-212025-09-28
52025-10-012025-10-08
62025-10-032025-10-10
72025-10-102025-10-17
82025-10-122025-10-19
92025-10-152025-10-22
102025-10-202025-10-27
112025-10-222025-10-29
122025-10-252025-11-01
12 rows

The end of a month rolls over by itself: October 25 plus 7 days is November 1. To go back, use DATE_SUB:

main.sql
1SELECT DATE_SUB('2025-10-01', INTERVAL 1 DAY), DATE_ADD('2025-10-01', INTERVAL 1 MONTH);
Output
DATE_SUB('2025-10-01', INTERVAL 1 DAY)DATE_ADD('2025-10-01', INTERVAL 1 MONTH)
2025-09-302025-11-01
1 row

One day before October 1 is September 30, and one month after is November 1.

Change it

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;
Should print
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
Exercise 3

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.

It should print
idorder_datedeadline
102025-10-202025-10-27
112025-10-222025-10-29
122025-10-252025-11-01
3 rows
exercise3.sql
1SELECT id, order_date2FROM orders3WHERE order_date >= '2025-10-20';
Output
Press Run or Ctrl+Enter

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: >=, < and BETWEEN all pick a range of days, and BETWEEN includes both ends.
  • YEAR, MONTH and DAY pull out the year, month and day as numbers, and work in WHERE too.
  • DATEDIFF(later, earlier) counts the days between; the wrong order gives a negative number.
  • CURDATE() is today and NOW() 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, and DATE_SUB moves it back.
Finished lesson 7?Lesson 7 of 23 · progress stays in this browser
Next · lesson 8 · 15 minAggregate functionsTurn a whole table into one number with COUNT, SUM, AVG, MIN and MAX: how many rows, the total, the average, the largest, and with WHERE, only some rows.Next lesson →
The button marks this lesson done and opens the next.