CodeBlanks Logo Icon CodeBlanks

DATEDIFF

Find the difference between two dates.

DATEDIFF

DATEDIFF returns the number of days between two dates. The order of arguments sets the sign.

SELECT DATEDIFF('2026-01-15', '2026-01-01') AS fark;

The 14-day gap between January 15 and January 1.

The expression DATEDIFF('2026-01-15', '2026-01-01') AS fark is evaluated and returned.

DATEDIFF — details

In PostgreSQL subtracting two dates gives the day difference. MySQL uses the DATEDIFF function.

Practice DATEDIFF

Select the days from each order_date to today aliased as gun from orders.

Starter code

query.sql
-- day difference
SELECT
FROM orders

Sample solution

query.sql
SELECT DATEDIFF(CURRENT_DATE, order_date) AS gun
FROM orders

Expected output

gun
15
45

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Date difference

Question
SELECT ____('2026-01-15', '2026-01-01');
Answer
SELECT DATEDIFF('2026-01-15', '2026-01-01');

Explanation: DATEDIFF is the right answer.

2 What does DATEDIFF return?

  • The difference in years
  • A date
  • The difference in days Correct answer
  • An hour

Explanation: DATEDIFF gives days between two dates.

3 DATEDIFF: match the terms

Term Meaning
DATEDIFF day diff
EXTRACT extract
INTERVAL interval
CURRENT_DATE today

All lessons in Beginner