Open In App

SQL CASE Statement

Improve
Improve
Like Article
Like
Save
Share
Report

Control statements are frequently used in programming languages to manage the execution of sets of statements. Similarly, SQL also utilizes control statements for query filtering and optimization by selecting the rows/tuples that meet our criteria.

In this article, we will explore the CASE-Switch Statement in SQL. The CASE statement is SQL’s way of handling if/else logic.

SQL CASE Statement

The CASE Expression in SQL allows to conditionally assign values based on different conditions. It goes through the given conditions and returns the value when any condition is met.

Once a condition is met, the CASE statement will return the result and not read the rest of the conditions. If no conditions are true, then it will return the value in the clause.

Note: If no condition is true and ELSE part is missing, CASE statement will return Null

CASE Syntax

CASE case_value

WHEN condition THEN result1

WHEN condition THEN result2

Else result

END CASE;

Demo SQL Database

We will be using this sample SQL table for our examples on SQL CASE statement:

CustomerIDCustomerNameLastNameCountryAgePhone
1ShubhamThakurIndia23xxxxxxxxxx
2AmanChopraAustralia21xxxxxxxxxx
3NaveenTulasiSri Lanka24xxxxxxxxxx
4AdityaArpanAustria21xxxxxxxxxx
5Nishant. Salchichas S.A.JainSpain22xxxxxxxxxx

You can create the same Database in your system, by writing the following MySQL query:

MySQL
CREATE TABLE Customer(
    CustomerID INT PRIMARY KEY,
    CustomerName VARCHAR(50),
    LastName VARCHAR(50),
    Country VARCHAR(50),
    Age int(2),
  Phone int(10)
);
-- Insert some sample data into the Customers table
INSERT INTO Customer (CustomerID, CustomerName, LastName, Country, Age, Phone)
VALUES (1, 'Shubham', 'Thakur', 'India','23','xxxxxxxxxx'),
       (2, 'Aman ', 'Chopra', 'Australia','21','xxxxxxxxxx'),
       (3, 'Naveen', 'Tulasi', 'Sri lanka','24','xxxxxxxxxx'),
       (4, 'Aditya', 'Arpan', 'Austria','21','xxxxxxxxxx'),
       (5, 'Nishant. Salchichas S.A.', 'Jain', 'Spain','22','xxxxxxxxxx');

SQL CASE Examples

Let’s look at some examples of CASE Statements in SQL.

Example 1: Adding Multiple Conditions to a CASE statement

We can add multiple conditions in the CASE statement by using multiple WHEN clauses.

Query:

SELECT CustomerName, Age,
CASE
    WHEN Age> 22 THEN 'The Age is greater than 22'
    WHEN Age = 21 THEN 'The Age is 21'
    ELSE 'The Age is over 30'
END AS QuantityText
FROM Customer;

Output

output

Output

Example 2: CASE Statement With ORDER BY Clause

Let’s take the Customer Table which contains CustomerID, CustomerName, LastName, Country, Age, and Phone. We can check the data of the Customer table by using the ORDER BY clause with the CASE statement.

Query:

SELECT CustomerName, Country
FROM Customer
ORDER BY
(CASE
    WHEN Country  IS 'India' THEN Country
    ELSE Age
END);

Output:

case statement with order by clauseoutput

output

Key TakeAways about CASE Statement:

  • There should always be a SELECT in the CASE statement.
  • END ELSE is an optional component but WHEN THEN these cases must be included in the CASE statement.
  • We can make any conditional statement using any conditional operator (like WHERE ) between WHEN and THEN. This includes stringing together multiple conditional statements using AND and OR.
  • We can include multiple WHEN statements and an ELSE statement to counter with unaddressed conditions.

Last Updated : 18 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads