Challenge - 5 Problems
DATE_FORMAT Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
Remember %W gives weekday name and %r gives 12-hour time with AM/PM.
✗ Incorrect
The date '2024-06-15' is a Saturday. %W returns full weekday name, %M full month name, %d day with leading zero, %Y full year, and %r 12-hour time with seconds and AM/PM.
🧠 Conceptual
intermediate1:30remaining
Understanding DATE_FORMAT specifiers
Which DATE_FORMAT specifier returns the abbreviated month name (e.g., 'Jun')?
Attempts:
2 left
💡 Hint
Look for the specifier that returns a short month name.
✗ Incorrect
%b returns abbreviated month name like 'Jan', 'Feb', 'Jun'. %M returns full month name, %m returns month number with leading zero, %W returns full weekday name.
📝 Syntax
advanced2: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;
Attempts:
2 left
💡 Hint
Check for missing quotes or parentheses.
✗ Incorrect
Option D is missing the closing quote for the format string, causing a syntax error. All others are syntactically correct.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
Consider readability and function overhead.
✗ Incorrect
Using DATE_FORMAT with '%Y-%m' directly extracts year and month in one step, which is clear and efficient. Options A, B, and D add extra function calls or string manipulation, which is less efficient.
🔧 Debug
expert3:00remaining
Debugging unexpected output from DATE_FORMAT
A developer runs:
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'?
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'?
Attempts:
2 left
💡 Hint
%h is 12-hour format, %H is 24-hour format.
✗ Incorrect
The specifier '%h' returns hour in 12-hour format (01..12). '%H' returns hour in 24-hour format (00..23). To get '23:59 PM' (though PM is redundant with 24-hour), changing '%h' to '%H' will show '23:59'. The '%p' will still show AM/PM but is not typical with 24-hour format.