Module 5 · Advanced queries

Calculate and query dates

By the end you will be able to obtain a part of a date or calculate a later date.

About 12 min

0 of 52 lessons

Before you start, you need

Transform text with functions

Extract parts or shift a date

A date can tell you the year someone joined or the day of their next check-up. First decide whether you need part of the date, or a completely new date.

In the course data, dates are written as year-month-day. For example, 2025-06-10 means 10 June 2025. This format is also called YYYY-MM-DD.

STRFTIME('%Y', joined_at) gets the year; '%Y' says which part we want to extract. DATE(joined_at, '+7 days') calculates seven days later; '+7 days' expresses the shift.

Both calculations can be shown in SELECT with an alias. The original date is kept and DATE takes the length of each month into account. We use SQLite syntax that works in the lab; other engines may write these functions differently.

Minimal syntax
SELECT STRFTIME('%Y', joined_at) AS year,
       DATE(joined_at, '+7 days') AS review
FROM employees;

See it in action

We want to schedule a check-up seven days after each employee's start date.

SQL
SELECT name,
       joined_at,
       DATE(joined_at, '+7 days') AS review
FROM employees;
Example result: 8 of 10 rows
namejoined_atreview
Claire Smith2021-02-152021-02-22
Alex Reed2022-05-102022-05-17
Beth Morgan2024-01-122024-01-19
Charles Page2021-09-012021-09-08
Diana Lane2023-03-202023-03-27
Ernest Cole2024-06-022024-06-09
Faith Rivers2022-11-182022-11-25
Graham Newton2023-08-072023-08-14
  1. joined_at shows the starting point. DATE returns the date seven days later.
  2. The result lets you compare both dates next to each employee. The calculation handles changes of month: it does not just add 7 to the day in the text.
  3. If you only need the year, STRFTIME('%Y', joined_at) extracts it as text. That is the first practice task.
  4. Do not use a day/month/year format in these arguments. The course data already uses YYYY-MM-DD.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Extract the year

Not started

Show name and STRFTIME('%Y', joined_at) as joined_year from employees.

Expected result: 10 rows with the columns name, joined_year, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Shift a date

Not started

Show name and DATE(joined_at, '+7 days') with the alias review_date from employees.

Expected result: 10 rows with the columns name, review_date, in that order. Row order does not matter.

View tables and data

Open this panel to see the full data.

Key takeaways

  • Course dates use YYYY-MM-DD.
  • STRFTIME extracts a part.
  • DATE calculates a new date.
  • The saved date is preserved.

Next: Practice: combine UNION, CASE and functions

Go deeper

Going further: calculate the seven-day deadline and the month for three orders.

SQL
SELECT id, date, DATE(date, '+7 days') AS deadline, STRFTIME('%Y-%m', date) AS month FROM orders WHERE id IN (101, 102, 103) ORDER BY id;
Example result: 3 rows
iddatedeadlinemonth
1012025-05-022025-05-092025-05
1022025-05-042025-05-112025-05
1032025-05-102025-05-172025-05
  1. The deadline is calculated from each original date. The month is extracted as year-month text; the order and its date stay unchanged.

What date is seven days after 28 February in a leap year?

SQL
SELECT DATE('2024-02-28', '+7 days') AS review;
Example result: 1 rows
review
2024-03-06
  1. The result is 6 March. DATE takes the length of the month into account, including 29 February.
Going further with SQLite: download and run it

Dates are YYYY-MM-DD. Compare the extracted year with the shifted date. DATE handles changes of month and leap years; the file also checks a deadline and a month extracted from orders.

Download the script with the database and ready-made queries. Open an empty SQLite database and import the script. In sqlite3 you can use .read functions-date-sqlite.sql. To run it again, import the file into another empty database.