0
0
MySQLquery~20 mins

IF function in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
IF Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
1: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;
ANULL
BNo
CYes
DSyntax Error
Attempts:
2 left
💡 Hint
IF(condition, true_value, false_value) returns true_value if condition is true.
query_result
intermediate
1: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;
AFalse
BTrue
CNULL
DError
Attempts:
2 left
💡 Hint
In MySQL, NULL is treated as false in IF conditions.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in IF function usage
Which option contains a syntax error in using the IF function in MySQL?
ASELECT IF(score >= 60, 'Pass', 'Fail') FROM exams;
BSELECT IF(score >= 60, 'Pass', 'Fail') result FROM exams;
CSELECT IF(score >= 60, 'Pass', 'Fail') AS result FROM exams;
DSELECT IF(score >= 60, 'Pass') FROM exams;
Attempts:
2 left
💡 Hint
IF function requires three arguments: condition, true_value, false_value.
query_result
advanced
1: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;
AA
BB
CC
DNULL
Attempts:
2 left
💡 Hint
Check the score against each condition in order.
🧠 Conceptual
expert
2:00remaining
Behavior of IF function with non-boolean expressions
Consider this query:

SELECT IF('abc', 'Yes', 'No') AS result;

What will be the output and why?
MySQL
SELECT IF('abc', 'Yes', 'No') AS result;
AYes, because non-empty strings are treated as TRUE in IF condition
BNo, because IF only accepts boolean TRUE or FALSE
CNULL, because 'abc' is not a boolean
DSyntax Error, because string literals cannot be used as condition
Attempts:
2 left
💡 Hint
In MySQL, non-zero and non-empty values are TRUE in boolean context.