TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst ยท 76.7K subscribers
Post #2732 996
๐Ÿš€ SQL Roadmap 2026 โ€” Part 7

CASE Statements โ€” Adding Business Logic to SQL ๐Ÿง 

In the previous parts, you learned how to:

โ€ข Retrieve data with SELECT

โ€ข Filter data with WHERE

โ€ข Sort results with ORDER BY

โ€ข Summarize data with aggregate functions

โ€ข Group data with GROUP BY

Now it's time to learn how to make SQL think in business categories.

For example:

โ€ข Is this customer High Value, Medium Value, or Low Value?

โ€ข Is this employee's salary High, Medium, or Low?

โ€ข Is this order Small, Medium, or Large?

That's what the CASE expression helps you do.

1๏ธโƒฃ What is CASE?

CASE allows you to create conditional logic inside SQL.

It's similar to:

โ€ข IF condition

โ€ข THEN result

โ€ข ELSE result

Basic Syntax

SELECT
column_name,
CASE
WHEN condition THEN result
WHEN condition THEN result
ELSE result
END AS new_column
FROM table_name;


2๏ธโƒฃ Simple CASE Example

Suppose we have employee salaries.

We want to classify employees based on salary.

SELECT
employee_name,
salary,
CASE
WHEN salary >= 1000000 THEN 'High'
WHEN salary >= 600000 THEN 'Medium'
ELSE 'Low'
END AS salary_category
FROM employees;


Result:

employee_name | salary  | salary_category
Rahul | 1200000 | High
Priya | 850000 | Medium
Amit | 500000 | Low


3๏ธโƒฃ How CASE Works

SQL checks conditions from top to bottom.

For:

CASE
WHEN salary >= 1000000 THEN 'High'
WHEN salary >= 600000 THEN 'Medium'
ELSE 'Low'
END


SQL effectively asks:

โ€ข Is salary >= 1,000,000? YES โ†’ High

โ€ข NO โ†’ Is salary >= 600,000? YES โ†’ Medium

โ€ข NO โ†’ Low

Once a matching WHEN condition is found, SQL returns that result.

4๏ธโƒฃ Order of WHEN Conditions Matters

Consider:

CASE
WHEN salary >= 600000 THEN 'Medium'
WHEN salary >= 1000000 THEN 'High'
ELSE 'Low'
END


This is problematic.

Why?

Someone earning โ‚น12 lakh satisfies:

โ€ข salary >= 600000 first.

So SQL labels them:

โ€ข Medium instead of High

Better:

CASE
WHEN salary >= 1000000 THEN 'High'
WHEN salary >= 600000 THEN 'Medium'
ELSE 'Low'
END


Rule:

โ€ข Put more specific or higher-priority conditions before broader conditions.

5๏ธโƒฃ CASE With Text Conditions

You can also classify based on text.

Example:

SELECT
employee_name,
department,
CASE
WHEN department = 'IT' THEN 'Technology'
WHEN department = 'Finance' THEN 'Corporate'
WHEN department = 'HR' THEN 'Corporate'
ELSE 'Other'
END AS department_group
FROM employees;


6๏ธโƒฃ CASE With Multiple Conditions

You can use AND and OR inside WHEN.

Example:

SELECT
customer_name,
city,
customer_segment,
CASE
WHEN customer_segment = 'Premium'
AND city = 'Mumbai'
THEN 'Premium Mumbai'

WHEN customer_segment = 'Premium'
THEN 'Other Premium'

ELSE 'Standard'
END AS customer_group
FROM customers;


7๏ธโƒฃ CASE With IN

You can combine CASE with IN.

SELECT
customer_name,
city,
CASE
WHEN city IN ('Mumbai', 'Pune', 'Nashik')
THEN 'Maharashtra'
WHEN city IN ('Delhi', 'Noida', 'Gurgaon')
THEN 'NCR'
ELSE 'Other'
END AS region
FROM customers;


This is useful for creating business regions.

8๏ธโƒฃ CASE With BETWEEN

Example:
  • โค 3
More from @sqlanalyst
  1. Oct 7, 2026๐Ÿš€๐—ฃ๐—ฎ๐˜† ๐—”๐—ณ๐˜๐—ฒ๐—ฟ ๐—ฃ๐—น๐—ฎ๐—ฐ๐—ฒ๐—บ๐—ฒ๐—ป๐˜ ๐—ง๐—ฟ๐—ฎ๐—ถ๐—ป๐—ถ๐—ป๐—ด | ๐—•๐—ฒ๐—ฐ๐—ผ๐—บ๐—ฒ ๐—ฎ ๐—™๐˜‚๐—น๐—น๐˜€๐˜๐—ฎ๐—ฐโ€ฆ
  2. Oct 7, 2026SQL Interview Series โ€” Part 4 ๐Ÿ“Œ Question 4: Find the Highest Salary in Each Department Suโ€ฆ
  3. Oct 7, 2026๐— ๐—ฎ๐˜€๐˜๐—ฒ๐—ฟ ๐—ฃ๐—ผ๐˜„๐—ฒ๐—ฟ ๐—•๐—œ ๐—ณ๐—ผ๐—ฟ ๐—™๐—ฅ๐—˜๐—˜! ๐Ÿ”ฅ Learn Power BI through these FREE learninโ€ฆ
  4. Sep 29, 2026SQL Interview Series โ€” Part 2 ๐Ÿ“Œ Question 2: Find Duplicate Records Suppose you have an Emโ€ฆ
  5. Sep 29, 2026๐—™๐—ฅ๐—˜๐—˜ ๐—ฅ๐—ฒ๐˜€๐—ผ๐˜‚๐—ฟ๐—ฐ๐—ฒ๐˜€ ๐—ง๐—ผ ๐—Ÿ๐—ฒ๐—ฎ๐—ฟ๐—ป ๐—”๐—œ ๐—ถ๐—ป ๐Ÿฎ๐Ÿฌ๐Ÿฎ๐Ÿฒ๐Ÿš€ โ€‹ Explore 6 free resourceโ€ฆ
  6. Sep 29, 2026SQL Interview Series โ€” Part 1 Hi guys, let's start a SQL interview series covering frequenโ€ฆ
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook โ†’Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 โ†’