Lesson 45 of 60 – CASE Statement
75%

SQL CASE Statement

The SQL CASE statement is used to create conditional logic in SQL queries. It works like an IF-ELSE statement in programming languages.

Note: The CASE statement is commonly used to display different values based on conditions.

1. What is CASE?

The CASE statement allows us to check conditions and return different results.

SELECT
    student_name,
    CASE
        WHEN marks >= 40 THEN 'Pass'
        ELSE 'Fail'
    END AS result
FROM students;

2. Basic CASE Syntax

The basic syntax of CASE is:

CASE
    WHEN condition THEN result
    ELSE result
END

The WHEN condition is checked first. If it is true, the corresponding result is returned.

3. WHEN and THEN

WHEN defines a condition and THEN defines the value returned when that condition is true.

SELECT
    student_name,
    CASE
        WHEN marks >= 40 THEN 'Pass'
    END AS result
FROM students;

4. ELSE in CASE

ELSE specifies the result when none of the WHEN conditions are true.

SELECT
    student_name,
    CASE
        WHEN marks >= 40 THEN 'Pass'
        ELSE 'Fail'
    END AS result
FROM students;

5. END Keyword

Every SQL CASE expression must end with the END keyword.

SELECT
    CASE
        WHEN age >= 18 THEN 'Adult'
        ELSE 'Minor'
    END AS category
FROM students;

6. CASE with Multiple Conditions

A CASE statement can contain multiple WHEN conditions.

SELECT
    student_name,
    marks,
    CASE
        WHEN marks >= 80 THEN 'Excellent'
        WHEN marks >= 60 THEN 'Good'
        WHEN marks >= 40 THEN 'Pass'
        ELSE 'Fail'
    END AS grade
FROM students;

7. CASE with Numbers

CASE can be used with numeric columns.

SELECT
    student_name,
    marks,
    CASE
        WHEN marks >= 90 THEN 1
        WHEN marks >= 75 THEN 2
        WHEN marks >= 50 THEN 3
        ELSE 4
    END AS grade_level
FROM students;

8. CASE with Text Values

CASE can compare text values and return meaningful descriptions.

SELECT
    student_name,
    status,
    CASE
        WHEN status = 'Active' THEN 'Currently Studying'
        ELSE 'Not Active'
    END AS student_status
FROM students;

9. CASE with Comparison Operators

CASE supports comparison operators such as >, <, =, >=, and <=.

SELECT
    student_name,
    fees,
    CASE
        WHEN fees >= 10000 THEN 'High Fee'
        ELSE 'Normal Fee'
    END AS fee_category
FROM students;

10. CASE with AND

Multiple conditions can be combined using AND.

SELECT
    student_name,
    marks,
    attendance,
    CASE
        WHEN marks >= 40 AND attendance >= 75
        THEN 'Eligible'
        ELSE 'Not Eligible'
    END AS eligibility
FROM students;

11. CASE with OR

The OR operator can also be used inside a CASE condition.

SELECT
    student_name,
    CASE
        WHEN course = 'Python' OR course = 'Java'
        THEN 'Programming Course'
        ELSE 'Other Course'
    END AS course_type
FROM students;

12. CASE with BETWEEN

The BETWEEN operator can be used inside CASE.

SELECT
    student_name,
    marks,
    CASE
        WHEN marks BETWEEN 80 AND 100 THEN 'A'
        WHEN marks BETWEEN 60 AND 79 THEN 'B'
        WHEN marks BETWEEN 40 AND 59 THEN 'C'
        ELSE 'F'
    END AS grade
FROM students;

13. CASE with IN

The IN operator can be used when checking multiple possible values.

SELECT
    student_name,
    course,
    CASE
        WHEN course IN ('Python', 'Java', 'PHP')
        THEN 'Programming'
        ELSE 'Other'
    END AS category
FROM students;

14. CASE with NULL Values

CASE can check whether a value is NULL using IS NULL.

SELECT
    student_name,
    CASE
        WHEN mobile IS NULL THEN 'Mobile Missing'
        ELSE 'Mobile Available'
    END AS mobile_status
FROM students;

15. CASE with IS NOT NULL

Use IS NOT NULL when you want to check for an available value.

SELECT
    student_name,
    CASE
        WHEN email IS NOT NULL THEN 'Email Available'
        ELSE 'Email Missing'
    END AS email_status
FROM students;

16. CASE with ORDER BY

CASE can be used with ORDER BY to create custom sorting.

SELECT student_name, status
FROM students
ORDER BY
    CASE
        WHEN status = 'Active' THEN 1
        ELSE 2
    END;

Active students will appear before other students.

17. CASE with SUM()

CASE can be combined with aggregate functions such as SUM().

SELECT
    SUM(
        CASE
            WHEN status = 'Paid' THEN amount
            ELSE 0
        END
    ) AS total_paid
FROM payments;

18. CASE with COUNT()

CASE can be used inside COUNT() to count records matching a condition.

SELECT
    COUNT(
        CASE
            WHEN status = 'Paid' THEN 1
        END
    ) AS paid_students
FROM payments;

19. CASE with GROUP BY

CASE can create categories that can then be grouped.

SELECT
    CASE
        WHEN marks >= 40 THEN 'Pass'
        ELSE 'Fail'
    END AS result,
    COUNT(*) AS total
FROM students
GROUP BY
    CASE
        WHEN marks >= 40 THEN 'Pass'
        ELSE 'Fail'
    END;

20. CASE with UPDATE

CASE can be used with UPDATE to assign different values based on conditions.

UPDATE students
SET grade =
    CASE
        WHEN marks >= 80 THEN 'A'
        WHEN marks >= 60 THEN 'B'
        WHEN marks >= 40 THEN 'C'
        ELSE 'F'
    END;

21. CASE for Fee Status

CASE is useful for displaying fee status based on paid and total fees.

SELECT
    student_name,
    total_fee,
    paid_fee,
    CASE
        WHEN paid_fee >= total_fee THEN 'Paid'
        WHEN paid_fee > 0 THEN 'Partially Paid'
        ELSE 'Pending'
    END AS fee_status
FROM students;

22. CASE for Attendance

CASE can classify students according to their attendance percentage.

SELECT
    student_name,
    attendance,
    CASE
        WHEN attendance >= 90 THEN 'Excellent'
        WHEN attendance >= 75 THEN 'Good'
        WHEN attendance >= 60 THEN 'Average'
        ELSE 'Low'
    END AS attendance_status
FROM students;

23. CASE for Salary Classification

CASE can classify employees based on salary.

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

24. CASE with Date Conditions

CASE can also be used with dates.

SELECT
    student_name,
    admission_date,
    CASE
        WHEN admission_date >= '2026-01-01'
        THEN 'New Admission'
        ELSE 'Old Admission'
    END AS admission_type
FROM students;

25. Simple CASE Expression

A simple CASE expression compares one expression with multiple values.

SELECT
    student_name,
    course,
    CASE course
        WHEN 'Python' THEN 'Programming'
        WHEN 'Java' THEN 'Programming'
        WHEN 'PHP' THEN 'Web Development'
        ELSE 'Other'
    END AS category
FROM students;

This form is useful when comparing one column with several fixed values.

26. Searched CASE Expression

A searched CASE expression checks different conditions.

SELECT
    student_name,
    marks,
    CASE
        WHEN marks >= 80 THEN 'A'
        WHEN marks >= 60 THEN 'B'
        WHEN marks >= 40 THEN 'C'
        ELSE 'F'
    END AS grade
FROM students;

This is useful when conditions involve comparisons or calculations.

27. CASE Without ELSE

The ELSE part is optional. If no condition matches and ELSE is not provided, CASE returns NULL.

SELECT
    student_name,
    CASE
        WHEN marks >= 40 THEN 'Pass'
    END AS result
FROM students;

28. Common CASE Mistake

A common mistake is forgetting the END keyword.

-- Incorrect
CASE
    WHEN marks >= 40 THEN 'Pass'

-- Correct
CASE
    WHEN marks >= 40 THEN 'Pass'
    ELSE 'Fail'
END

Always close a CASE expression with END.

29. Practical Student Result Example

We can use CASE to generate a complete result category.

SELECT
    student_name,
    marks,
    CASE
        WHEN marks >= 90 THEN 'Outstanding'
        WHEN marks >= 75 THEN 'Very Good'
        WHEN marks >= 60 THEN 'Good'
        WHEN marks >= 40 THEN 'Pass'
        ELSE 'Fail'
    END AS performance
FROM students;

30. Complete CASE Example

The following example combines CASE with fee and attendance information.

SELECT
    student_name,
    marks,
    attendance,
    total_fee,
    paid_fee,
    CASE
        WHEN marks >= 40 AND attendance >= 75
        THEN 'Eligible'
        ELSE 'Not Eligible'
    END AS exam_status,
    CASE
        WHEN paid_fee >= total_fee THEN 'Paid'
        WHEN paid_fee > 0 THEN 'Partially Paid'
        ELSE 'Pending'
    END AS fee_status
FROM students;

This query creates two useful categories: examination eligibility and fee status.

📌 Key Points

  • CASE is used for conditional logic in SQL.
  • WHEN specifies a condition.
  • THEN specifies the result.
  • ELSE provides a result when no condition matches.
  • END closes the CASE expression.
  • CASE can contain multiple WHEN conditions.
  • CASE can work with numbers, text, dates, and NULL values.
  • CASE can be used with ORDER BY.
  • CASE can be combined with aggregate functions.
  • CASE can be used with GROUP BY.
  • CASE can be used in UPDATE statements.
  • Simple CASE compares one expression with multiple values.
  • Searched CASE checks different conditions.
  • CASE is useful for creating categories and reports.

🧠 Quick Quiz

Question: Which keyword is used to close a SQL CASE expression?