CodeBlanks Logo Icon CodeBlanks

Summary - CASE

Write a conditional total report with CASE and SUM.

Summary - CASE

CASE produces conditional expressions. Forms include searched and simple, with ELSE defaults and aggregate use.

SELECT name,
  CASE WHEN price > 100 THEN 'pahalı' ELSE 'ucuz' END AS tier
FROM products;

Summary CASE query.

name, CASE WHEN price > 100 THEN 'pahalı' ELSE 'ucuz' END … is selected from products.

Practice Summary - CASE

Summary: tag each product with CASE WHEN price > 100 THEN 'pahalı' ELSE 'ucuz' END.

Starter code

query.sql
-- summary case
SELECT name,
  CASE WHEN price > 100 THEN 'pahalı' ELSE 'ucuz'

Sample solution

query.sql
SELECT name,
  CASE WHEN price > 100 THEN 'pahalı'
       ELSE 'ucuz' END AS tier
FROM products;

Expected output

name    tier
Laptop  pahalı
Kalem   ucuz

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What closes a CASE?

  • END Correct answer
  • ELSE
  • THEN
  • WHEN

Explanation: A CASE expression always ends with END.

2 Summary - CASE: match the terms

Term Meaning
CASE WHEN condition
THEN result
ELSE default
END close

All lessons in Intermediate