Simple CASE
- SQL course
- Intermediate
- Lesson 67/115
- 4 min
Learn the simple CASE form.
Simple CASE
Simple CASE compares one expression for equality. It is a shorter form of searched CASE.
SELECT name,
CASE category WHEN 'Kitap' THEN 'okuma'
WHEN 'Elektronik' THEN 'teknik'
ELSE 'diğer' END AS type
FROM products;This query converts the category name to a type.
name, CASE category WHEN 'Kitap' THEN 'okuma' WHEN … is selected from products.
Simple CASE — details
In simple CASE the CASE expression is written once; each WHEN checks equality with a value.
For example, CASE status WHEN 'A' THEN 'Aktif' WHEN 'P' THEN 'Pasif' END turns codes into readable text.
Practice Simple CASE
Write CASE category WHEN 'Kitap' THEN 'okuma' ... END to convert category to type.
Starter code
-- simple case
SELECT name,
CASESample solution
SELECT name,
CASE category WHEN 'Kitap' THEN 'okuma'
WHEN 'Elektronik' THEN 'teknik'
ELSE 'diğer' END AS type
FROM products;Expected output
name type Suç ve Ceza okuma Laptop teknik
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Simple CASE: match the terms
| Term | Meaning |
|---|---|
simple |
equality |
searched |
condition |
WHEN |
value |
THEN |
result |
2 Add category
CASE ____ WHEN 'Kitap' THEN 'okuma'
CASE category WHEN 'Kitap' THEN 'okuma'
Explanation: category is the right answer.
3 What does simple CASE check?
- A range
- Equality Correct answer
- Size
- NULL
Explanation: Simple CASE checks equality.