SQL Lesson 8: CASE — If-Then in SQL

📎 This article includes 2 downloadable practice files ↓

⏱ 2 min read

📘 SQL Beginner Course · Lesson 8 of 12

In this article
  1. Searched CASE
  2. Group by the label
  3. Conditional counts: SUM(CASE …)
  4. Simple CASE
  5. NULL labels
  6. CASE in ORDER BY
  7. Where beginners go wrong
  8. Practice

CASE is SQL’s IF: it returns different values depending on conditions. You’ll use it to label rows, bucket values and turn rows into columns.

Searched CASE

SELECT name, price,
       CASE WHEN price < 1000  THEN 'Budget'
            WHEN price < 10000 THEN 'Mid'
            ELSE 'Premium'
       END AS band
FROM products;

Conditions are checked top to bottom; the first true one wins, like a nested IF in Excel. Without ELSE, unmatched rows get NULL.

Group by the label

SELECT CASE WHEN qty <= 3 THEN 'Small' WHEN qty <= 7 THEN 'Medium' ELSE 'Large' END AS size,
       COUNT(*) AS orders
FROM orders
GROUP BY size;

SQLite and PostgreSQL allow GROUP BY the alias; in SQL Server repeat the CASE expression in GROUP BY.

Conditional counts: SUM(CASE …)

SELECT SUM(CASE WHEN status = 'Delivered' THEN 1 ELSE 0 END) AS delivered,
       SUM(CASE WHEN status <> 'Delivered' THEN 1 ELSE 0 END) AS not_delivered
FROM orders;

This is SQL’s COUNTIF. Put a value instead of 1 for SUMIF: SUM(CASE WHEN p.category='Computers' THEN o.qty*p.price ELSE 0 END).

💡 Several SUM(CASE…) columns grouped by region give a pivot table: regions down, categories across. The project lesson builds one.

Simple CASE

CASE status WHEN 'Delivered' THEN 'Done' WHEN 'Cancelled' THEN 'Void' ELSE 'Open' END

NULL labels

SELECT name, CASE WHEN gstin IS NULL THEN 'Unregistered' ELSE 'Registered' END AS gst_status
FROM customers;

COALESCE(gstin, 'Not given') is a shorter way to replace NULL with a value.

CASE in ORDER BY

ORDER BY CASE status WHEN 'Pending' THEN 1 WHEN 'Shipped' THEN 2 ELSE 3 END

A custom sort order, like Excel’s custom lists.

Where beginners go wrong

Mistake Effect
Wider condition first (price < 10000 before < 1000) Nothing is ever ‘Budget’
No ELSE Unexpected NULLs
CASE WHEN col = NULL Never true; use IS NULL
Forgetting END Syntax error

Practice

Download shop.db and this lesson’s exercise file below. Open the database in the free DB Browser for SQLite (or sqlite3 shop.db), paste the exercises into the Execute SQL tab and write a query for each task. The expected result is printed under every task; answers are at the bottom of the file.

📎 Practice files for this article

  • 📄
    Practice database (SQLite)shop.db: 12 customers, 10 products and 300 orders (April-September 2026).
    ⬇ DB · 28 KB
  • 🗃️
    Lesson 8 exercisesTasks with the expected result under each, and answers at the bottom.
    ⬇ SQL · 2 KB

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong