0
0
MySQLquery~20 mins

EXTRACT and YEAR/MONTH/DAY in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
EXTRACT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Extract Year from Date
Given a table orders with a column order_date of type DATE, what is the output of this query?
SELECT EXTRACT(YEAR FROM order_date) AS order_year FROM orders WHERE order_id = 101;
MySQL
CREATE TABLE orders (order_id INT, order_date DATE);
INSERT INTO orders VALUES (101, '2023-07-15');
A07
B2023
C15
D2023-07-15
Attempts:
2 left
💡 Hint
EXTRACT(YEAR FROM date) returns the year part as a number.
query_result
intermediate
2:00remaining
Extract Month from Date
What will this query return for the order_date '2022-12-05' in the orders table?
SELECT EXTRACT(MONTH FROM order_date) AS order_month FROM orders WHERE order_id = 202;
MySQL
CREATE TABLE orders (order_id INT, order_date DATE);
INSERT INTO orders VALUES (202, '2022-12-05');
A12
B2022
C2022-12-05
D5
Attempts:
2 left
💡 Hint
EXTRACT(MONTH FROM date) returns the month number.
📝 Syntax
advanced
2:00remaining
Identify the Syntax Error in EXTRACT Usage
Which option contains a syntax error when trying to extract the day from a date column birth_date in table users?
MySQL
SELECT EXTRACT(DAY FROM birth_date) AS birth_day FROM users;
ASELECT EXTRACT(DAY OF birth_date) AS birth_day FROM users;
BSELECT EXTRACT(DAY FROM birth_date) AS birth_day FROM users;
CSELECT EXTRACT(DAY, birth_date) AS birth_day FROM users;
DSELECT EXTRACT(DAY FROM birth_date) birth_day FROM users;
Attempts:
2 left
💡 Hint
EXTRACT syntax requires the keyword FROM between the part and the date.
optimization
advanced
2:00remaining
Optimizing Date Filtering Using EXTRACT
You want to find all orders placed in March 2023 from the orders table with a order_date column. Which query is more efficient and why?
ASELECT * FROM orders WHERE EXTRACT(YEAR FROM order_date) = 2023 AND EXTRACT(MONTH FROM order_date) = 3;
BSELECT * FROM orders WHERE MONTH(order_date) = 3 AND YEAR(order_date) = 2023;
CSELECT * FROM orders WHERE DATE_FORMAT(order_date, '%Y-%m') = '2023-03';
DSELECT * FROM orders WHERE order_date >= '2023-03-01' AND order_date < '2023-04-01';
Attempts:
2 left
💡 Hint
Using range conditions on the date column can use indexes better than functions on the column.
🧠 Conceptual
expert
2:00remaining
Understanding EXTRACT Behavior with NULL Dates
What is the result of this query when order_date is NULL in the orders table?
SELECT EXTRACT(YEAR FROM order_date) AS order_year FROM orders WHERE order_id = 303;
MySQL
CREATE TABLE orders (order_id INT, order_date DATE);
INSERT INTO orders VALUES (303, NULL);
ANULL
B0
CError: Invalid date
DEmpty result set
Attempts:
2 left
💡 Hint
EXTRACT returns NULL when the date value is NULL.