What if you could instantly pull out just the month or year from any date with a simple command?
Why DATE_FORMAT and EXTRACT in SQL? - Purpose & Use Cases
Imagine you have a big list of dates written in different styles on paper. You want to find all the birthdays in July or just see the year part of each date. Doing this by hand means flipping through pages and guessing the parts of each date.
Manually checking each date is slow and mistakes happen easily. You might miss some dates or mix up months and days. It's hard to quickly answer questions like "How many events happened in 2023?" or "Show me all dates in March."
Using DATE_FORMAT and EXTRACT in SQL lets you quickly pull out parts of a date like the year, month, or day. You can also change how dates look to match what you need. This makes sorting, filtering, and showing dates easy and error-free.
Look at each date and write down the month and year separately.
SELECT EXTRACT(MONTH FROM date_column) AS month, DATE_FORMAT(date_column, '%Y-%m-%d') AS formatted_date FROM table;You can instantly find, group, and display dates exactly how you want without confusion or delay.
A store wants to see sales only from December or find out how many orders came in each year. DATE_FORMAT and EXTRACT help them get these answers fast from their sales records.
Manually handling dates is slow and error-prone.
DATE_FORMAT and EXTRACT let you pick and show date parts easily.
This makes date-based questions quick and reliable to answer.
Practice
EXTRACT(MONTH FROM '2024-06-15') return?Solution
Step 1: Understand EXTRACT function
The EXTRACT function pulls out a specific part of a date, here the MONTH part.Step 2: Apply EXTRACT to the date
From the date '2024-06-15', the month is 6 (June).Final Answer:
6 -> Option DQuick Check:
EXTRACT(MONTH FROM date) = 6 [OK]
- Confusing day with month
- Expecting full date instead of part
- Using wrong syntax for EXTRACT
Solution
Step 1: Recall EXTRACT syntax
The correct syntax is EXTRACT(part FROM date), with parentheses and FROM keyword.Step 2: Check each option
EXTRACT(YEAR FROM '2024-06-15') matches the correct syntax: EXTRACT(YEAR FROM '2024-06-15'). Others miss parentheses or have wrong order.Final Answer:
EXTRACT(YEAR FROM '2024-06-15') -> Option AQuick Check:
Correct EXTRACT syntax uses parentheses and FROM [OK]
- Omitting parentheses
- Swapping arguments order
- Using quotes around part name
SELECT DATE_FORMAT('2024-06-15', '%Y-%m-%d'), EXTRACT(DAY FROM '2024-06-15');Solution
Step 1: Understand DATE_FORMAT with '%Y-%m-%d'
This format outputs year-month-day as '2024-06-15'.Step 2: Understand EXTRACT(DAY FROM date)
Extracting DAY from '2024-06-15' gives 15.Final Answer:
2024-06-15, 15 -> Option BQuick Check:
DATE_FORMAT + EXTRACT day = '2024-06-15', 15 [OK]
- Mixing month and day order
- Wrong format string in DATE_FORMAT
- Extracting wrong date part
SELECT DATE_FORMAT('2024-06-15', '%Y-%d-%m');Solution
Step 1: Analyze the format string '%Y-%d-%m'
This format prints year, then day, then month, which is unusual and likely a mistake.Step 2: Understand common date format order
Usually, month comes before day as '%Y-%m-%d'. Swapping day and month causes confusing output.Final Answer:
The format string swaps day and month incorrectly -> Option CQuick Check:
Correct order is year-month-day, not year-day-month [OK]
- Mixing day and month in format string
- Wrong number of arguments to DATE_FORMAT
- Confusing DATE_FORMAT with EXTRACT
orders showing only the year and month of the order date in format YYYY-MM. Which SQL query correctly does this?Solution
Step 1: Understand DATE_FORMAT for year and month
Using '%Y-%m' in DATE_FORMAT returns year and month as 'YYYY-MM'.Step 2: Check other options
SELECT EXTRACT(YEAR_MONTH FROM order_date) AS year_month FROM orders; is invalid syntax; EXTRACT does not support YEAR_MONTH. SELECT DATE_FORMAT(order_date, '%Y-%d') AS year_month FROM orders; uses day instead of month. SELECT EXTRACT(MONTH FROM order_date) AS year_month FROM orders; returns only month number, missing year.Final Answer:
SELECT DATE_FORMAT(order_date, '%Y-%m') AS year_month FROM orders; -> Option AQuick Check:
DATE_FORMAT with '%Y-%m' gives year-month [OK]
- Using EXTRACT with unsupported parts
- Mixing day and month in format
- Selecting only month without year
Ready to prove it?
Pass the test and this pattern moves unknown
