SQL LESSON 20 OF 32

CASE: labels from logic

CASE builds conditional values. WHEN tests a condition, THEN supplies its result, ELSE handles remaining rows. END closes the expression. This makes readable buckets without changing stored values.

हिन्दी में समझें

CASE शर्त के अनुसार मान बनाता है। WHEN शर्त, THEN परिणाम, ELSE बाकी पंक्तियाँ सँभालता है। END से बंद करें। संग्रहीत मान नहीं बदलते।

अभ्यास: name और tier दिखाएँ: price <= 500 पर Budget, नहीं तो Regular; id क्रम।

Hinglish mein samjhein

CASE condition ke hisaab se value banata hai. WHEN condition, THEN result, ELSE remaining rows. END expression close karta hai. Stored values same rehti hain.

Practice: name aur tier: price <= 500 to Budget warna Regular; id sorted.

A small SQL example

SELECT name, CASE WHEN price>=1000 THEN 'Premium' ELSE 'Standard' END AS tier FROM products ORDER BY id;

Examples use the fictional shop database bundled with the SQL playground: customers, products, orders, order_items and employees. The engine is SQLite; other SQL dialects may differ.

Try this example in the SQL playground →

Practise this idea

Show product name and tier: Budget when price <= 500, otherwise Regular; order by id.

Check answer & explanation
SELECT name,CASE WHEN price<=500 THEN 'Budget' ELSE 'Regular' END AS tier FROM products ORDER BY id;

CASE evaluates the price condition for each row. Prices at or below 500 receive Budget; all others receive Regular.

Stuck in the course? Choose Check answer for either the challenge or quiz. Read the explanation and continue to the next lesson at your own pace.

Check your understanding

CASE is used to…

  1. Create conditional values
  2. Delete a database
  3. Join only numeric columns
Check the quiz answer

Create conditional values

CASE builds conditional values. WHEN tests a condition, THEN supplies its result, ELSE handles remaining rows. END closes the expression. This makes readable buckets without changing stored values.

References & corrections

Use the official SQLite documentation for full language rules. Read how these lessons are made and checked. Found a mistake? Report it to Excel Keys with this lesson URL and a reproducible example.

FROM LEARNING TO BUSINESS RESULTS

Need a dashboard for your team?

Excel Keys designs custom Excel and HTML dashboards around your own KPIs and Excel files. They run offline in the browser, so your data stays on your computer.