Three-valued logic
- SQL course
- Beginner
- Lesson 63/115
- 4 min
Learn how NULL behaves with AND, OR and comparisons.
IFNULL and NVL
IFNULL (MySQL) and NVL (Oracle) take two values; if the first is NULL they return the second. They are a short form of COALESCE.
SELECT IFNULL(phone, 'yok') AS tel FROM customers;This query writes 'yok' when phone is null.
IFNULL(phone, 'yok') AS tel is selected from customers.
IFNULL and NVL — details
IFNULL takes only two values; for three or more fallbacks use COALESCE. PostgreSQL does not have IFNULL.
Practice IFNULL and NVL
Select phone with 'none' fallback using IFNULL from customers.
Starter code
-- fallback value
SELECT
FROM customersSample solution
SELECT IFNULL(phone, 'yok') AS tel
FROM customersExpected output
tel 555-1234 yok
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Fallback
SELECT ____(phone, 'yok') FROM customers;
SELECT IFNULL(phone, 'yok') FROM customers;
Explanation: IFNULL(phone, 'yok') shows 'yok' when the phone is NULL.
2 Three-valued logic: match the terms
| Term | Meaning |
|---|---|
IFNULL |
two fallbacks |
COALESCE |
many fallbacks |
NULLIF |
null if equal |
IS NULL |
check |
3 How many values does IFNULL take?
- Unlimited
- Two Correct answer
- Three
- One
Explanation: IFNULL takes two values.