0
0
MySQLquery~5 mins

IF function in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the IF function do in MySQL?
The IF function checks a condition and returns one value if the condition is true, and another value if it is false.
Click to reveal answer
beginner
Write the syntax of the IF function in MySQL.
IF(condition, value_if_true, value_if_false)
Click to reveal answer
intermediate
How does the IF function differ from the CASE statement in MySQL?
IF is used for simple two-way decisions (true/false), while CASE can handle multiple conditions and outcomes.
Click to reveal answer
beginner
Example: What will this query return? SELECT IF(5 > 3, 'Yes', 'No');
It will return 'Yes' because 5 is greater than 3, so the condition is true.
Click to reveal answer
intermediate
Can the IF function be nested inside another IF function in MySQL?
Yes, you can nest IF functions to check multiple conditions step-by-step.
Click to reveal answer
What will this MySQL query return? SELECT IF(10 = 10, 'Equal', 'Not Equal');
ANULL
B'Not Equal'
C'Equal'
DError
Which part of IF(condition, value_if_true, value_if_false) is evaluated first?
AAll at once
Bvalue_if_true
Cvalue_if_false
Dcondition
Can IF function return NULL as a value_if_true or value_if_false?
AYes
BNo
COnly value_if_true can be NULL
DOnly value_if_false can be NULL
Which of these is a valid use of IF in MySQL?
ASELECT IF age >= 18, 'Adult', 'Minor' FROM users;
BSELECT IF(age >= 18, 'Adult', 'Minor') FROM users;
CSELECT IF(age >= 18 THEN 'Adult' ELSE 'Minor') FROM users;
DSELECT IF(age >= 18 'Adult', 'Minor') FROM users;
What happens if the condition in IF is NULL?
AIF returns value_if_false
BIF returns value_if_true
CIF returns NULL
DIF throws an error
Explain how the IF function works in MySQL and give a simple example.
Think about a question with two possible answers.
You got /4 concepts.
    Describe a situation where nesting IF functions would be useful in a database query.
    Imagine checking age groups or multiple categories.
    You got /3 concepts.