Challenge - 5 Problems
IF Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate1:00remaining
Output of IF function with numeric comparison
What is the output of this MySQL query?
SELECT IF(10 > 5, 'Yes', 'No') AS result;MySQL
SELECT IF(10 > 5, 'Yes', 'No') AS result;
Attempts:
2 left
💡 Hint
IF(condition, true_value, false_value) returns true_value if condition is true.
✗ Incorrect
Since 10 is greater than 5, the condition is true, so IF returns 'Yes'.
❓ query_result
intermediate1:00remaining
Using IF function with NULL value
What will this query return?
SELECT IF(NULL, 'True', 'False') AS output;MySQL
SELECT IF(NULL, 'True', 'False') AS output;
Attempts:
2 left
💡 Hint
In MySQL, NULL is treated as false in IF conditions.
✗ Incorrect
Since NULL is treated as false, IF returns the false_value 'False'.
📝 Syntax
advanced1:30remaining
Identify the syntax error in IF function usage
Which option contains a syntax error in using the IF function in MySQL?
Attempts:
2 left
💡 Hint
IF function requires three arguments: condition, true_value, false_value.
✗ Incorrect
Option D misses the third argument, causing a syntax error.
❓ query_result
advanced1:30remaining
Nested IF function output
What is the output of this query?
SELECT IF(score >= 90, 'A', IF(score >= 75, 'B', 'C')) AS grade FROM (SELECT 80 AS score) AS t;MySQL
SELECT IF(score >= 90, 'A', IF(score >= 75, 'B', 'C')) AS grade FROM (SELECT 80 AS score) AS t;
Attempts:
2 left
💡 Hint
Check the score against each condition in order.
✗ Incorrect
Score 80 is not >= 90, so first IF returns false and evaluates second IF. 80 >= 75 is true, so 'B' is returned.
🧠 Conceptual
expert2:00remaining
Behavior of IF function with non-boolean expressions
Consider this query:
What will be the output and why?
SELECT IF('abc', 'Yes', 'No') AS result;What will be the output and why?
MySQL
SELECT IF('abc', 'Yes', 'No') AS result;
Attempts:
2 left
💡 Hint
In MySQL, non-zero and non-empty values are TRUE in boolean context.
✗ Incorrect
MySQL treats any non-empty string as TRUE in IF condition, so it returns 'Yes'.