🧬 SQL Operators & Conditions
Estimated reading: 3 minutes 70 views

πŸ“Œ SQL CASE – Apply Conditional Logic in Queries

🧲 Introduction – What is SQL CASE?

The CASE expression in SQL allows you to apply conditional logic inside your queries. Think of it as SQL’s version of IF...ELSE. It’s used to return custom values based on conditionsβ€”ideal for categorizing, labeling, or transforming query output.

🎯 In this guide, you’ll learn:

  • Basic syntax of CASE and WHEN
  • Use CASE in SELECT, WHERE, and ORDER BY
  • The difference between simple and searched CASE
  • Best practices and performance tips

βœ… 1. Basic CASE Syntax

CASE
  WHEN condition1 THEN result1
  WHEN condition2 THEN result2
  ELSE default_result
END

βœ… Use it to return different values based on matching conditions.


🎯 2. CASE in SELECT Clause

SELECT name,
  CASE
    WHEN score >= 90 THEN 'A'
    WHEN score >= 80 THEN 'B'
    ELSE 'F'
  END AS grade
FROM students;

βœ… Assigns letter grades based on score ranges.


🧠 3. CASE in ORDER BY

SELECT name, status
FROM tasks
ORDER BY
  CASE status
    WHEN 'urgent' THEN 1
    WHEN 'normal' THEN 2
    ELSE 3
  END;

βœ… Custom sorting logic based on priority.


πŸ”„ 4. CASE in WHERE Clause

SELECT * FROM orders
WHERE
  CASE
    WHEN customer_type = 'VIP' THEN total > 500
    ELSE total > 1000
  END;

βœ… Applies different filters based on customer type.


🧾 5. Simple vs. Searched CASE

Simple CASE

CASE status
  WHEN 'active' THEN 'In Use'
  WHEN 'inactive' THEN 'Disabled'
  ELSE 'Unknown'
END

Searched CASE

CASE
  WHEN status = 'active' AND last_login > '2023-01-01' THEN 'Returning'
  WHEN status = 'inactive' THEN 'Churned'
  ELSE 'Unknown'
END

βœ… Simple matches a fixed column; searched uses boolean logic.


πŸ“˜ Best Practices

βœ… Recommended❌ Avoid This
Use searched CASE for complex logicNesting too many CASE expressions
Add ELSE to avoid NULLs in edge casesLeaving out ELSE (defaults to NULL)
Format code for readabilityCramming CASE into one line

πŸ“Œ Summary – Recap & Next Steps

The SQL CASE expression empowers you to add decision-making logic directly inside your queriesβ€”no procedural code required.

πŸ” Key Takeaways:

  • Use CASE to replace IF…ELSE logic in SQL
  • Integrate with SELECT, WHERE, ORDER BY, GROUP BY
  • Prefer searched CASE for custom conditions

βš™οΈ Real-World Relevance:
Used in reporting, dashboards, conditional formatting, dynamic sorting, and business rules.

➑️ Next: Explore COALESCE and NULLIF for advanced expression control.


❓ FAQ – SQL CASE Expression

❓ What is SQL CASE used for?

βœ… To apply conditional logic and return values based on different conditions.

❓ Is CASE an IF statement?

βœ… Yes. CASE is SQL’s way of writing IF...ELSE logic inside queries.

❓ Can I use CASE in WHERE clause?

βœ… Yes, but it’s less readable. Prefer boolean logic where possible.

❓ What happens if no WHEN condition matches?

βœ… If there’s no ELSE, it returns NULL.

❓ Can I nest CASE statements?

βœ… Yes, but keep it readable and avoid excessive nesting.


Share Now :
Share

πŸ“Œ SQL CASE

Or Copy Link

CONTENTS
Scroll to Top