Challenge - 5 Problems
Date Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this DATE_FORMAT query?
Given a table events with a column
event_date of type DATE, what is the output of the following query?SELECT DATE_FORMAT(event_date, '%Y-%m-%d') AS formatted_date FROM events WHERE event_id = 1;
SQL
SELECT DATE_FORMAT(event_date, '%Y-%m-%d') AS formatted_date FROM events WHERE event_id = 1;
Attempts:
2 left
💡 Hint
The format string '%Y-%m-%d' means year-month-day with dashes.
✗ Incorrect
DATE_FORMAT formats the date according to the pattern. '%Y' is 4-digit year, '%m' is 2-digit month, '%d' is 2-digit day, separated by dashes.
❓ query_result
intermediate2:00remaining
What does EXTRACT return for this date?
Consider the query:
What is the value of
SELECT EXTRACT(MONTH FROM '2024-02-29'::DATE) AS month_value;
What is the value of
month_value?SQL
SELECT EXTRACT(MONTH FROM '2024-02-29'::DATE) AS month_value;
Attempts:
2 left
💡 Hint
EXTRACT(MONTH FROM date) returns the month number from the date.
✗ Incorrect
The date is February 29, 2024. EXTRACT(MONTH FROM date) returns 2 because February is the second month.
📝 Syntax
advanced2:00remaining
Which query correctly extracts the year from a timestamp?
You want to extract the year from a timestamp column named
created_at. Which of the following queries is syntactically correct and returns the year as an integer?Attempts:
2 left
💡 Hint
EXTRACT uses the syntax EXTRACT(field FROM source). The field is not a string.
✗ Incorrect
Option B uses the correct syntax: EXTRACT(YEAR FROM created_at). Options A, C, and D are invalid syntax.
❓ optimization
advanced2:00remaining
Optimizing date filtering with EXTRACT vs DATE_FORMAT
You want to select all rows from
sales where the sale happened in March 2023. Which query is more efficient and why?Attempts:
2 left
💡 Hint
Filtering by range on the raw date column can use indexes better than functions on the column.
✗ Incorrect
Option C uses a range condition on the raw date column, which can use indexes efficiently. Options B and C apply functions on the column, preventing index use. Option C uses LIKE which is less efficient and may not use indexes.
🔧 Debug
expert2:00remaining
Why does this EXTRACT query raise an error?
Given the query:
It raises an error. What is the cause?
SELECT EXTRACT(QUARTER FROM '2023-05-10');
It raises an error. What is the cause?
SQL
SELECT EXTRACT(QUARTER FROM '2023-05-10');
Attempts:
2 left
💡 Hint
Check if the string literal is recognized as a date type by the database.
✗ Incorrect
The string '2023-05-10' is a plain string literal. EXTRACT expects a date or timestamp type. Without casting, it causes a type error.