CodeBlanks Logo Icon CodeBlanks

Three-valued logic

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

query.sql
-- fallback value
SELECT
FROM customers

Sample solution

query.sql
SELECT IFNULL(phone, 'yok') AS tel
FROM customers

Expected 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

Question
SELECT ____(phone, 'yok') FROM customers;
Answer
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.

All lessons in Beginner