DATEDIFF
- SQL course
- Beginner
- Lesson 56/115
- 4 min
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
-- day difference
SELECT
FROM ordersSample solution
SELECT DATEDIFF(CURRENT_DATE, order_date) AS gun
FROM ordersExpected 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
SELECT ____('2026-01-15', '2026-01-01');
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 |