Challenge - 5 Problems
Date Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Extracting the Year from a Date
Given the table Events with a column
event_date of type DATE, what is the output of this query?SELECT YEAR(event_date) AS year_extracted FROM Events WHERE event_date = '2023-06-15';
SQL
CREATE TABLE Events (event_date DATE); INSERT INTO Events VALUES ('2023-06-15');
Attempts:
2 left
💡 Hint
The YEAR() function extracts the year part from a date.
✗ Incorrect
The YEAR() function returns the year component as an integer from a date value. Here, it extracts 2023 from '2023-06-15'.
❓ query_result
intermediate2:00remaining
Extracting Month and Day from a Date
What will be the output of this query on the table Orders with a column
order_date containing '2022-12-05' ?SELECT MONTH(order_date) AS month_extracted, DAY(order_date) AS day_extracted FROM Orders WHERE order_date = '2022-12-05';
SQL
CREATE TABLE Orders (order_date DATE); INSERT INTO Orders VALUES ('2022-12-05');
Attempts:
2 left
💡 Hint
MONTH() extracts the month number, DAY() extracts the day number.
✗ Incorrect
MONTH('2022-12-05') returns 12 and DAY('2022-12-05') returns 5 as integers.
📝 Syntax
advanced2:00remaining
Identify the Syntax Error in Date Extraction
Which option contains a syntax error when trying to extract the year from a date column
birth_date in table Users?SQL
SELECT YEAR(birth_date) FROM Users;
Attempts:
2 left
💡 Hint
Square brackets are not used for function arguments in SQL.
✗ Incorrect
Option A uses square brackets which is invalid syntax for function calls in SQL. Parentheses must be used.
❓ query_result
advanced2:00remaining
Using EXTRACT to Get Date Parts
What is the output of this query on table
Appointments with appt_date = '2024-01-09' ?SELECT EXTRACT(MONTH FROM appt_date) AS month_part, EXTRACT(DAY FROM appt_date) AS day_part FROM Appointments WHERE appt_date = '2024-01-09';
SQL
CREATE TABLE Appointments (appt_date DATE); INSERT INTO Appointments VALUES ('2024-01-09');
Attempts:
2 left
💡 Hint
EXTRACT returns integer parts of the date.
✗ Incorrect
EXTRACT(MONTH FROM '2024-01-09') returns 1 and EXTRACT(DAY FROM '2024-01-09') returns 9 as integers.
🧠 Conceptual
expert2:00remaining
Understanding Date Extraction Functions Across SQL Dialects
Which statement is correct about extracting year, month, and day from a date column in different SQL dialects?
Attempts:
2 left
💡 Hint
Check which functions are supported by each SQL dialect.
✗ Incorrect
MySQL supports both YEAR() and EXTRACT() functions for date parts. PostgreSQL supports EXTRACT but not YEAR(). SQL Server uses DATEPART() instead of EXTRACT().