Full answer
There are two ways to write CASE. The simple form compares one expression against several values:
text
CASE x WHEN 1 THEN 'one' WHEN 2 THEN 'two' ENDEach WHEN is checked as x = value. When the value is NULL, the check is x = NULL, which is never true:
sql
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);sql
SELECT name,
CASE city WHEN NULL THEN 'missing' ELSE 'has city' END AS label
FROM students
ORDER BY name;| name | label |
|---|---|
| Asha | has city |
| Ben | has city |
| Chen | has city |
| Dara | has city |
| Eli | has city |
Dara's city is NULL, yet the label fell through to the ELSE branch. The searched form takes a full condition in each WHEN, so it can use IS NULL:
sql
SELECT name,
CASE WHEN city IS NULL THEN 'missing'
WHEN city = 'Pune' THEN 'local'
ELSE 'outstation'
END AS label
FROM students
ORDER BY name;| name | label |
|---|---|
| Asha | local |
| Ben | outstation |
| Chen | local |
| Dara | missing |
| Eli | outstation |
Order matters in the searched form: the first true condition wins. Putting the NULL check first is a good habit, so later conditions can assume a value exists. For a plain "show this instead of NULL", COALESCE is shorter: COALESCE(city, 'missing').
sql
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
city TEXT,
year INTEGER
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
credits INTEGER
);
CREATE TABLE enrolments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
marks INTEGER,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO students VALUES (1,'Asha','Pune',1),(2,'Ben','Leeds',2),(3,'Chen','Pune',1),(4,'Dara',NULL,3),(5,'Eli','Leeds',2);
INSERT INTO courses VALUES (10,'Databases',4),(20,'Algorithms',4),(30,'Statistics',3),(40,'Ethics',2);
INSERT INTO enrolments VALUES
(1,10,88),(1,20,92),(2,10,75),(2,30,NULL),
(3,10,64),(3,20,71),(3,30,80),(5,20,55);
SELECT name,
CASE city WHEN NULL THEN 'missing' ELSE 'has city' END AS label
FROM students
ORDER BY name;
SELECT name,
CASE WHEN city IS NULL THEN 'missing'
WHEN city = 'Pune' THEN 'local'
ELSE 'outstation'
END AS label
FROM students
ORDER BY name;Checked on SQLite 3.46.1, run twice with identical output.
Key points
- Simple CASE uses = for each WHEN
- WHEN NULL can never match
- Use the searched form with IS NULL
- The first true WHEN wins, so check NULL first
Common mistakes
- Writing CASE col WHEN NULL and getting the ELSE value
- Forgetting an ELSE and getting NULL for unmatched rows
Follow-ups
- What does CASE return when no WHEN matches and there is no ELSE?
- Rewrite the query with COALESCE.
Tips
- Name the two forms of CASE before explaining the bug