← Back to MySQL Course | Chapter 8: Operators & Conditional Logic | Lesson 4 of 5

CASE Expression

CASE एक sorting machine पर if-then signs के एक set जैसा है: अगर score ज़्यादा है तो यह A कहता है, अगर medium है तो B, नहीं तो C।
Syntax
sql
CASE
  WHEN condition1 THEN result1
  WHEN condition2 THEN result2
  ELSE default_result
END

CASE column_name
  WHEN value1 THEN result1
  ELSE default_result
END

Simple CASE Expression

एक simple CASE expression एक value को options की एक list के against compare करता है, एक switch statement जैसा। यह programming में एक switch statement जैसे act करता है, expression को बारी-बारी हर WHEN value के against match करते हुए जब तक एक succeed न हो जाए।

उदाहरण: Simple CASE Expression

sql
CREATE TABLE orders (id INT, status TEXT);
INSERT INTO orders VALUES (1, 'P'), (2, 'S'), (3, 'C');
SELECT id, CASE status
  WHEN 'P' THEN 'Pending'
  WHEN 'S' THEN 'Shipped'
  WHEN 'C' THEN 'Cancelled'
  ELSE 'Unknown'
END AS status_label
FROM orders;

Searched CASE Expression

एक searched CASE expression एक single value compare करने के बजाय हर option के लिए complex logical conditions evaluate करता है। यह range checks और कई operators की अनुमति देता है, जैसे numeric ranges के आधार पर scores को letter grades में grade करना।

उदाहरण: Searched CASE Expression

sql
CREATE TABLE scores (id INT, score INT);
INSERT INTO scores VALUES (1, 95), (2, 72), (3, 50);
SELECT id, CASE
  WHEN score >= 90 THEN 'A'
  WHEN score >= 70 THEN 'B'
  ELSE 'F'
END AS grade
FROM scores;

Math Operations के साथ CASE इस्तेमाल करना

आप invalid operations से बचाव के लिए math calculations के अंदर CASE शामिल कर सकते हैं। यह calculation को fail होने देने के बजाय NULL या एक default return करके division by zero जैसी errors से बचने के लिए उपयोगी है।

उदाहरण: Using CASE with Math Operations

sql
CREATE TABLE items (id INT, total INT, quantity INT);
INSERT INTO items VALUES (1, 100, 5), (2, 50, 0);
SELECT id, CASE WHEN quantity = 0 THEN NULL ELSE total / quantity END AS unit_price
FROM items;

Nested CASE Expressions

आप multi-level decision logic के लिए दूसरे CASE expressions के अंदर CASE expressions nest कर सकते हैं। यह hierarchical check requirements handle करने में मदद करता है, हालाँकि deeply nested CASE statements पढ़ना मुश्किल हो सकते हैं और refactor करना worth हो सकता है।

उदाहरण: Nested CASE Expressions

sql
CREATE TABLE users (id INT, role TEXT, active INT);
INSERT INTO users VALUES (1, 'admin', 1), (2, 'admin', 0), (3, 'member', 1);
SELECT id, CASE
  WHEN role = 'admin' THEN CASE WHEN active = 1 THEN 'Active Admin' ELSE 'Inactive Admin' END
  ELSE 'Member'
END AS label
FROM users;

Aggregations के साथ SELECT में CASE

SUM जैसे aggregate functions के अंदर CASE इस्तेमाल करना आपको conditional counts और custom calculations perform करने देता है, जैसे एक single query में सिर्फ paid mark किए गए orders sum करना बजाय अलग filtered queries चलाने के।

उदाहरण: CASE in SELECT with Aggregations

sql
CREATE TABLE orders (id INT, status TEXT, total INT);
INSERT INTO orders VALUES (1, 'paid', 100), (2, 'unpaid', 50), (3, 'paid', 75);
SELECT SUM(CASE WHEN status = 'paid' THEN total ELSE 0 END) AS paid_total
FROM orders;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. END भूल जाना, या बिना END के ELSE लिखना, जो एक syntax error है।
  2. ELSE छोड़ देना और यह surprise होना कि unmatched rows NULL return करती हैं।
  3. एक searched CASE में conditions को गलत order में रखना, क्योंकि पहला true WHEN जीतता है।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.