Bird
Raised Fist0
SQLquery~10 mins

YEAR, MONTH, DAY extraction in SQL - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - YEAR, MONTH, DAY extraction
Start with a date value
↓
Apply YEAR() function
→Extract year part
↓
Apply MONTH() function
→Extract month part
↓
Apply DAY() function
→Extract day part
↓
Use extracted parts in query or output
Extract year, month, and day parts from a date using SQL functions YEAR(), MONTH(), and DAY() step-by-step.
Execution Sample
SQL
SELECT
  YEAR('2024-06-15') AS year_part,
  MONTH('2024-06-15') AS month_part,
  DAY('2024-06-15') AS day_part;
Extracts year, month, and day from the date '2024-06-15'.
Execution Table
StepFunction AppliedInput DateExtracted ValueOutput Column
1YEAR()'2024-06-15'2024year_part
2MONTH()'2024-06-15'6month_part
3DAY()'2024-06-15'15day_part
4Final Output-2024, 6, 15All columns
💡 All parts extracted; query returns one row with year=2024, month=6, day=15.
Variable Tracker
VariableStartAfter YEAR()After MONTH()After DAY()Final
year_partNULL2024202420242024
month_partNULLNULL666
day_partNULLNULLNULL1515
Key Moments - 3 Insights
Why does YEAR() return 2024 instead of the full date?
YEAR() extracts only the year part from the date string, ignoring month and day, as shown in execution_table step 1.
Can MONTH() return a value greater than 12?
No, MONTH() returns the month number (1-12) from the date, as shown in execution_table step 2 where it returns 6.
What happens if the input date is NULL?
All extraction functions return NULL if input is NULL, so no year, month, or day is extracted.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what value does DAY() extract at step 3?
A6
B15
C2024
DNULL
💡 Hint
Check the 'Extracted Value' column at step 3 in execution_table.
At which step does the query extract the month from the date?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for the row where 'Function Applied' is MONTH() in execution_table.
If the input date was '2023-12-31', what would YEAR() return?
A31
B12
C2023
DNULL
💡 Hint
YEAR() extracts the year part; see variable_tracker for how year_part changes.
Concept Snapshot
YEAR(date) extracts the year number from a date.
MONTH(date) extracts the month number (1-12).
DAY(date) extracts the day number (1-31).
Use these functions to break down dates into parts.
They return integers representing each part.
If input is NULL, output is NULL.
Full Transcript
This visual execution shows how SQL extracts year, month, and day from a date string. Starting with the date '2024-06-15', the YEAR() function extracts 2024, MONTH() extracts 6, and DAY() extracts 15. Each step applies one function and outputs the corresponding part. Variables year_part, month_part, and day_part update as each function runs. The process stops after all parts are extracted, returning a single row with these values. Common confusions include understanding that YEAR() returns only the year, MONTH() never exceeds 12, and all functions return NULL if input is NULL. The quiz tests understanding of these extraction steps and outputs.

Practice

(1/5)
1. What does the SQL function EXTRACT(YEAR FROM order_date) return when used on a date column?
easy
A. The full date in order_date
B. The year part of the date in order_date
C. The day part of the date in order_date
D. The month part of the date in order_date

Solution

  1. Step 1: Understand the EXTRACT function

    The EXTRACT function is used to get a specific part of a date, such as year, month, or day.
  2. Step 2: Identify the part requested

    Here, EXTRACT(YEAR FROM order_date) extracts the year part from the date in the order_date column.
  3. Final Answer:

    The year part of the date in order_date -> Option B
  4. Quick Check:

    EXTRACT(YEAR FROM date) = year number [OK]
Hint: EXTRACT(YEAR FROM date) gets the year number fast [OK]
Common Mistakes:
  • Confusing YEAR with MONTH or DAY
  • Expecting full date instead of part
  • Using wrong syntax like EXTRACT(order_date YEAR)
2. Which of the following is the correct syntax to extract the month from a date column named sale_date?
easy
A. EXTRACT(MONTH FROM sale_date)
B. EXTRACT(MONTH sale_date)
C. EXTRACT(FROM sale_date MONTH)
D. EXTRACT(sale_date, MONTH)

Solution

  1. Step 1: Recall the correct EXTRACT syntax

    The correct syntax is EXTRACT(part FROM source), where part is YEAR, MONTH, or DAY.
  2. Step 2: Match syntax with options

    EXTRACT(MONTH FROM sale_date) uses EXTRACT(MONTH FROM sale_date), which matches the correct syntax.
  3. Final Answer:

    EXTRACT(MONTH FROM sale_date) -> Option A
  4. Quick Check:

    EXTRACT(part FROM date) is correct syntax [OK]
Hint: Remember EXTRACT(part FROM date) syntax [OK]
Common Mistakes:
  • Omitting FROM keyword
  • Swapping order of arguments
  • Using commas inside EXTRACT
3. Given a table events with a column event_date storing dates, what will this query return?

SELECT EXTRACT(DAY FROM event_date) FROM events WHERE EXTRACT(MONTH FROM event_date) = 12;
medium
A. All month numbers of events that happened in December
B. Syntax error due to wrong EXTRACT usage
C. All full dates of events in December
D. All day numbers of events that happened in December

Solution

  1. Step 1: Analyze the WHERE clause

    The WHERE clause filters rows where the month part of event_date is 12 (December).
  2. Step 2: Analyze the SELECT clause

    The SELECT clause extracts the day part from event_date for those filtered rows.
  3. Final Answer:

    All day numbers of events that happened in December -> Option D
  4. Quick Check:

    EXTRACT filters month=12, selects day numbers [OK]
Hint: Filter by month, select day with EXTRACT [OK]
Common Mistakes:
  • Confusing day with month in SELECT
  • Expecting full date instead of day number
  • Thinking query has syntax error
4. Identify the error in this SQL query:

SELECT EXTRACT(YEAR sale_date) FROM sales;
medium
A. Missing FROM keyword inside EXTRACT
B. sale_date should be in quotes
C. EXTRACT cannot be used on date columns
D. No error, query is correct

Solution

  1. Step 1: Check EXTRACT syntax

    EXTRACT requires the syntax EXTRACT(part FROM source). The keyword FROM is missing here.
  2. Step 2: Confirm correct usage

    The correct query should be SELECT EXTRACT(YEAR FROM sale_date) FROM sales;
  3. Final Answer:

    Missing FROM keyword inside EXTRACT -> Option A
  4. Quick Check:

    EXTRACT(part FROM date) needs FROM [OK]
Hint: Always include FROM in EXTRACT syntax [OK]
Common Mistakes:
  • Omitting FROM keyword
  • Putting column name in quotes
  • Thinking EXTRACT can't use dates
5. You want to find all orders placed on the 15th day of any month in 2023 from the orders table with a order_date column. Which query correctly filters these orders?
hard
A. SELECT * FROM orders WHERE EXTRACT(MONTH FROM order_date) = 15 AND EXTRACT(YEAR FROM order_date) = 2023;
B. SELECT * FROM orders WHERE DAY(order_date) = 15 AND YEAR(order_date) = 2023;
C. SELECT * FROM orders WHERE EXTRACT(DAY FROM order_date) = 15 AND EXTRACT(YEAR FROM order_date) = 2023;
D. SELECT * FROM orders WHERE EXTRACT(DAY FROM order_date) = 2023 AND EXTRACT(YEAR FROM order_date) = 15;

Solution

  1. Step 1: Identify correct EXTRACT usage for day and year

    We want day = 15 and year = 2023, so EXTRACT(DAY FROM order_date) = 15 and EXTRACT(YEAR FROM order_date) = 2023.
  2. Step 2: Check each option

    SELECT * FROM orders WHERE EXTRACT(DAY FROM order_date) = 15 AND EXTRACT(YEAR FROM order_date) = 2023; matches the correct conditions. SELECT * FROM orders WHERE DAY(order_date) = 15 AND YEAR(order_date) = 2023; uses non-standard functions that may not work in all SQL dialects. SELECT * FROM orders WHERE EXTRACT(MONTH FROM order_date) = 15 AND EXTRACT(YEAR FROM order_date) = 2023; incorrectly uses MONTH = 15. SELECT * FROM orders WHERE EXTRACT(DAY FROM order_date) = 2023 AND EXTRACT(YEAR FROM order_date) = 15; swaps day and year values incorrectly.
  3. Final Answer:

    SELECT * FROM orders WHERE EXTRACT(DAY FROM order_date) = 15 AND EXTRACT(YEAR FROM order_date) = 2023; -> Option C
  4. Quick Check:

    Filter day=15 and year=2023 with EXTRACT [OK]
Hint: Use EXTRACT(DAY FROM date) and EXTRACT(YEAR FROM date) together [OK]
Common Mistakes:
  • Mixing up day and month
  • Using non-standard functions without checking dialect
  • Swapping day and year values
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard