EXTRACT
- SQL course
- Beginner
- Lesson 54/115
- 4 min
Pull year, month, day out of a date.
EXTRACT
With EXTRACT you pull a part like year, month or day from a date. You use YEAR, MONTH, DAY.
SELECT EXTRACT(YEAR FROM order_date) AS yil FROM orders;This query returns the year of each order.
EXTRACT(YEAR FROM order_date) AS yil is selected from orders.
EXTRACT — details
Some databases offer a YEAR(date) function. EXTRACT is ANSI standard and works across all major databases.
Practice EXTRACT
Select the year of order_date aliased as yil from orders.
Starter code
-- extract year
SELECT
FROM ordersSample solution
SELECT EXTRACT(YEAR FROM order_date) AS yil
FROM ordersExpected output
yil 2025 2026
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Extract year
SELECT ____(YEAR FROM order_date) FROM orders;
SELECT EXTRACT(YEAR FROM order_date) FROM orders;
Explanation: EXTRACT is the right answer.
2 What does EXTRACT do?
- Gets part of a date Correct answer
- Adds to a date
- Deletes a date
- Formats a date
Explanation: EXTRACT pulls a part from a date.
3 EXTRACT: match the terms
| Term | Meaning |
|---|---|
EXTRACT |
extract |
YEAR |
year |
MONTH |
month |
DAY |
day |