TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2733 1.07K
SELECT
employee_name,
salary,
CASE
WHEN salary BETWEEN 0 AND 500000
THEN 'Entry Level'

WHEN salary BETWEEN 500001 AND 1000000
THEN 'Mid Level'

ELSE 'Senior Level'
END AS salary_band
FROM employees;


However, for numeric ranges, inequality conditions are often easier to maintain:

CASE
WHEN salary < 500000 THEN 'Entry Level'
WHEN salary < 1000000 THEN 'Mid Level'
ELSE 'Senior Level'
END


Because the conditions are evaluated from top to bottom.

9️⃣ CASE for Customer Segmentation

Customer segmentation is a common analytics use case.

Suppose:

• total_spend represents total customer spending.

SELECT
customer_id,
customer_name,
total_spend,
CASE
WHEN total_spend >= 100000 THEN 'VIP'
WHEN total_spend >= 50000 THEN 'High Value'
WHEN total_spend >= 10000 THEN 'Medium Value'
ELSE 'Low Value'
END AS customer_segment
FROM customers;


This transforms raw spending into a business classification.

🔟 CASE for Order Size

Suppose you want to classify orders.

SELECT
order_id,
amount,
CASE
WHEN amount >= 10000 THEN 'Large'
WHEN amount >= 5000 THEN 'Medium'
ELSE 'Small'
END AS order_size
FROM orders;


Result:

order_id | amount | order_size
101 | 12000 | Large
102 | 7000 | Medium
103 | 2500 | Small


1️⃣1️⃣ CASE for Order Status

You can simplify several statuses into broader business categories.

SELECT
order_id,
order_status,
CASE
WHEN order_status = 'Completed'
THEN 'Successful'

WHEN order_status = 'Cancelled'
THEN 'Unsuccessful'

ELSE 'Pending'
END AS business_status
FROM orders;


1️⃣2️⃣ CASE With Dates

You can classify orders based on when they were placed.

SELECT
order_id,
order_date,
CASE
WHEN order_date < '2026-01-01'
THEN 'Previous Year'
ELSE 'Current Year'
END AS order_period
FROM orders;


1️⃣3️⃣ CASE for Profitability

Suppose you have:

• selling_price

• cost_price

You can classify products based on profit.

SELECT
product_name,
selling_price,
cost_price,
selling_price - cost_price AS profit,

CASE
WHEN selling_price - cost_price >= 10000
THEN 'Highly Profitable'

WHEN selling_price - cost_price > 0
THEN 'Profitable'

ELSE 'Loss'
END AS profitability
FROM products;


1️⃣4️⃣ CASE With Aggregate Functions

This is where CASE becomes extremely powerful.

Suppose you want to count completed orders.

SELECT
SUM(
CASE
WHEN order_status = 'Completed'
THEN 1
ELSE 0
END
) AS completed_orders
FROM orders;


Why does this work?

Each row becomes:

• Completed → 1

• Other → 0

Then SUM() adds them.

1️⃣5️⃣ Conditional Counting

You can calculate several metrics at once.

SELECT
COUNT(*) AS total_orders,

SUM(
CASE
WHEN order_status = 'Completed'
THEN 1 ELSE 0
END
) AS completed_orders,

SUM(
CASE
WHEN order_status = 'Cancelled'
THEN 1 ELSE 0
END
) AS cancelled_orders
FROM orders;


This is called conditional aggregation.

It is one of the most useful SQL techniques for dashboard development.
  • ❤ 2
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 →