DATE_FORMAT and EXTRACT in SQL - Time & Space Complexity
We want to understand how the time it takes to run date functions changes as we work with more data.
How does using DATE_FORMAT or EXTRACT on many rows affect the work done?
Analyze the time complexity of the following code snippet.
SELECT
DATE_FORMAT(order_date, '%Y-%m-%d') AS formatted_date,
EXTRACT(YEAR FROM order_date) AS order_year
FROM orders;
This query formats the date and extracts the year from each order's date in the orders table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Applying DATE_FORMAT and EXTRACT functions to each row's date.
- How many times: Once for every row in the orders table.
As the number of rows grows, the work grows in the same way because each row needs its date processed.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 date operations |
| 100 | 200 date operations |
| 1000 | 2000 date operations |
Pattern observation: The number of operations grows directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the number of rows increases.
[X] Wrong: "Using DATE_FORMAT or EXTRACT is slow because they do complex calculations that multiply with data size."
[OK] Correct: These functions run once per row and are simple operations, so the time grows linearly, not exponentially.
Understanding how simple functions like DATE_FORMAT and EXTRACT scale helps you explain query performance clearly and confidently.
What if we added a JOIN that doubles the number of rows? How would the time complexity change?
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
