Intermediate Queries · 8 min read · Beginner
CASE WHEN & Handling NULL with COALESCE
Create computed categories with CASE WHEN ... THEN ... ELSE ... END and replace missing NULL values safely using COALESCE().
What you'll learn
- Bucket numeric values into readable labels using CASE WHEN
- Replace NULL values cleanly with COALESCE(col, fallback)
- Test for NULL properly using IS NULL / IS NOT NULL (never = NULL)
In SQL, NULL represents an unknown or missing value. Because unknown never equals unknown, col = NULL is never true. Always write col IS NULL or wrap nullable columns in COALESCE(col, 'Fallback').
Fill-in-the-Blank Exercise
Handle NULL & Classify Scores
Replace NULL bios with 'No bio yet' and finish the CASE expression.
SELECT
(bio, 'No bio yet') AS bio,
CASE WHEN score >= 80 THEN 'Pass' ELSE 'Retry' AS status
FROM learners;Try It Yourself
SQLCASE WHEN Buckets + COALESCE
Query Result (In-Browser SQLite)
Click Run SQL to execute this query against a live in-browser SQLite database.
Check Your Understanding
Score: 0 / 1
1. How should you check if a column named deleted_at is NULL in SQL?
Study Workspace
Finished this lesson?
Your progress is saved locally in your browser, no account needed.