SQL Case | SQL Tutorial - Learn with VOKS
Back

SQL Case


πŸ”Ή What is SQL CASE?

The CASE statement in SQL is like an IF-THEN-ELSE statement in programming.

It allows you to:

  • Make decisions inside a query
  • Return different values based on conditions
  • Categorize data
  • Create custom output columns

Think of it like this:

β€œIf this condition is true β†’ return this value
If another condition is true β†’ return that value
Otherwise β†’ return something else”

πŸ”Ή Why Use SQL CASE?

You use CASE when you want to:

  • Categorize data (e.g., High/Medium/Low salary)
  • Replace values
  • Apply logic in SELECT
  • Create reports

πŸ”Ή Types of CASE in SQL

There are two types of CASE:

| Type            | Description |
|-----------------|------------|
| Simple CASE     | Compares one column to multiple values |
| Searched CASE   | Uses full conditions (>, <, =, etc.) |

πŸ”Ή Sample Employees Table

We’ll use this table for examples:

| id | name    | department | salary |
|----|---------|------------|--------|
| 1  | Alice   | HR         | 50000  |
| 2  | Bob     | Sales      | 60000  |
| 3  | Charlie | IT         | 55000  |
| 4  | David   | HR         | 45000  |
| 5  | Eve     | Sales      | 70000  |

1️⃣ Simple CASE

Used when you compare one column to specific values.

Syntax:

CASE column_name
    WHEN value1 THEN result1
    WHEN value2 THEN result2
    ELSE result
END

Example: Department Labels

SELECT name,
       department,
       CASE department
           WHEN 'HR' THEN 'Human Resources'
           WHEN 'IT' THEN 'Information Technology'
           WHEN 'Sales' THEN 'Sales Department'
           ELSE 'Other'
       END AS department_full_name
FROM Employees;

What Happens?

  • If department = 'HR' β†’ show "Human Resources"
  • If department = 'IT' β†’ show "Information Technology"
  • If department = 'Sales' β†’ show "Sales Department"
  • Otherwise β†’ "Other"

2️⃣ Searched CASE

Used when you need conditions (>, <, >=, etc.)

Syntax:

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

Example: Salary Categories

SELECT name,
       salary,
       CASE
           WHEN salary >= 65000 THEN 'High Salary'
           WHEN salary >= 50000 THEN 'Medium Salary'
           ELSE 'Low Salary'
       END AS salary_category
FROM Employees;

Explanation:

  • Salary β‰₯ 65000 β†’ High Salary
  • Salary β‰₯ 50000 β†’ Medium Salary
  • Otherwise β†’ Low Salary

SQL checks from top to bottom.

πŸ”Ή Using CASE in ORDER BY

You can even use CASE to control sorting.

SELECT name, department
FROM Employees
ORDER BY
    CASE
        WHEN department = 'HR' THEN 1
        WHEN department = 'IT' THEN 2
        ELSE 3
    END;

This sorts:

  1. HR first
  2. IT second
  3. Others last

πŸ”Ή Using CASE with Aggregate Functions

Example: Count employees by salary level.

SELECT
    CASE
        WHEN salary >= 60000 THEN 'High'
        ELSE 'Standard'
    END AS salary_group,
    COUNT(*) AS total_employees
FROM Employees
GROUP BY
    CASE
        WHEN salary >= 60000 THEN 'High'
        ELSE 'Standard'
    END;

πŸ”Ή Important Rules

| Rule | Explanation |
|------|------------|
| CASE must end with END | END is mandatory |
| ELSE is optional | But recommended |
| Order matters | First true condition is executed |
| Works inside SELECT, ORDER BY, GROUP BY | Very flexible |

πŸ”Ή Visual Explanation

Think of CASE like this:

IF salary >= 65000 β†’ High
ELSE IF salary >= 50000 β†’ Medium
ELSE β†’ Low

It’s just SQL’s way of writing IF-ELSE logic.

πŸ”Ή When Should You Use CASE?

Use CASE when:

  • You want readable reports
  • You need conditional logic inside queries
  • You want to transform data without changing the table


Example Code:
-- Simple CASE Example
SELECT name,
       department,
       CASE department
           WHEN 'HR' THEN 'Human Resources'
           WHEN 'IT' THEN 'Information Technology'
           WHEN 'Sales' THEN 'Sales Department'
           ELSE 'Other'
       END AS department_full_name
FROM Employees;

-- Searched CASE Example
SELECT name,
       salary,
       CASE
           WHEN salary >= 65000 THEN 'High Salary'
           WHEN salary >= 50000 THEN 'Medium Salary'
           ELSE 'Low Salary'
       END AS salary_category
FROM Employees;

-- CASE in ORDER BY
SELECT name, department
FROM Employees
ORDER BY
    CASE
        WHEN department = 'HR' THEN 1
        WHEN department = 'IT' THEN 2
        ELSE 3
    END;

-- CASE with GROUP BY
SELECT
    CASE
        WHEN salary >= 60000 THEN 'High'
        ELSE 'Standard'
    END AS salary_group,
    COUNT(*) AS total_employees
FROM Employees
GROUP BY
    CASE
        WHEN salary >= 60000 THEN 'High'
        ELSE 'Standard'
    END;
SQL
Introduction What is a Database, Advantages of Database, and Database Tables Creating Databases, Tables, Constraints, and Keys Defining Data Types, Unique ID, Inserting Values, and Handling NULL SELECT, DISTINCT, TOP, LIMIT, UPDATE, DELETE, ORDER BY, WHERE, HAVING, AND, OR, NOT Aggregate function; Min, Max, Count, Avg, Sum Wildcards, IN, AS, LIKE, BETWEEN, and Aliases SQL Joins: Inner, Outer, Left, Right, Full, Cross Set Theory for SQL: Joins, UNION, INTERSECT, EXCEPT, GROUP BY SQL Arithmetic Operators SQL Bitwise Operators SQL Comparison Operator SQL View/Null Functions SQL Comments SQL Case
All Courses
Advance AI Bootstrap C C++ Computer Vision Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel Figma HTML Java Script Machine Learning MySQLi Node JS PHP Power Bi Python Python for AI Python for Analysis React React Native SEO SMM SQL