0
0
MySQLquery~20 mins

DATE_FORMAT function in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DATE_FORMAT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of DATE_FORMAT with mixed specifiers
What is the output of the following query?
SELECT DATE_FORMAT('2024-06-15 14:35:20', '%W, %M %d, %Y %r') AS formatted_date;
MySQL
SELECT DATE_FORMAT('2024-06-15 14:35:20', '%W, %M %d, %Y %r') AS formatted_date;
ASaturday, 06 15, 2024 02:35:20 PM
BSunday, June 15, 2024 14:35:20
CSaturday, June 15, 2024 02:35:20 PM
DSaturday, June 15, 2024 14:35:20
Attempts:
2 left
💡 Hint
Remember %W gives weekday name and %r gives 12-hour time with AM/PM.
🧠 Conceptual
intermediate
1:30remaining
Understanding DATE_FORMAT specifiers
Which DATE_FORMAT specifier returns the abbreviated month name (e.g., 'Jun')?
A%b
B%M
C%W
D%m
Attempts:
2 left
💡 Hint
Look for the specifier that returns a short month name.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in DATE_FORMAT usage
Which option contains a syntax error in using DATE_FORMAT function?
MySQL
SELECT DATE_FORMAT('2024-06-15', '%Y-%m-%d') AS date1;
ASELECT DATE_FORMAT('2024-06-15', '%Y/%m/%d') AS date2;
BSELECT DATE_FORMAT('2024-06-15', '%d-%m-%Y') AS date3;
CSELECT DATE_FORMAT('2024-06-15', '%Y-%m-%d') AS date1;
DSELECT DATE_FORMAT('2024-06-15', '%Y-%m-%d);
Attempts:
2 left
💡 Hint
Check for missing quotes or parentheses.
optimization
advanced
2:30remaining
Optimizing date formatting for performance
You want to display only the year and month from a large table's datetime column using DATE_FORMAT. Which query is the most efficient?
ASELECT DATE_FORMAT(datetime_col, '%Y-%m') FROM big_table;
BSELECT CONCAT(YEAR(datetime_col), '-', MONTH(datetime_col)) FROM big_table;
CSELECT LEFT(DATE_FORMAT(datetime_col, '%Y-%m-%d'), 7) FROM big_table;
DSELECT SUBSTRING(DATE_FORMAT(datetime_col, '%Y-%m-%d'), 1, 7) FROM big_table;
Attempts:
2 left
💡 Hint
Consider readability and function overhead.
🔧 Debug
expert
3:00remaining
Debugging unexpected output from DATE_FORMAT
A developer runs:
SELECT DATE_FORMAT('2024-06-15 23:59:59', '%h:%i %p') AS time_12h;

But the output is '11:59 PM' as expected. They want to show '23:59 PM'. Which change will cause the output to show '23:59 PM'?
AChange '%i' to '%I' in the format string.
BChange '%h' to '%H' in the format string.
CRemove '%p' from the format string.
DChange '%p' to '%P' in the format string.
Attempts:
2 left
💡 Hint
%h is 12-hour format, %H is 24-hour format.