SQL | Case Statement

21 Mar, 2018
Control statements form the heart of most languages since they control the execution of other sets of statements. These are found in SQL too, and should be exploited for uses such as query filtering and query optimization through careful selection of tuples that match our requirement. In this post, we explore the Case-Switch statement in SQL.The CASE statement is SQL’s way of handling if/then logic.Syntax:There can be two valid ways of going about the case-switch statements.
The first takes a variable called case_value and matches it with some statement_list.CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE
The second considers a search_condition instead of variable equality and executes the statement_list accordingly.CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE
Examples:Say we have a relation, Faculty.Faculty Table:FacultyIDNameDepartmentGender001AakashCSM002SahilECM003JohnHSSM004ShelleyCSF005AnannyaCSF006SiaHSSFLet’s say we would like to modify this table such that if the department name is ‘CS’, it gets modified to ‘Computer Science’, if it is ‘EC’ it gets modified to ‘Electronics and Communication’, and if it is ‘HSS’ it gets modified to ‘Humanities and Social Sciences’. This can be achieved using case statement.Sample Query:Consider a variable, department_name which is entered in the SQL code.CASE department_name
 WHEN 'CS'
  THEN UPDATE Faculty SET
  department='Computer Science';
 WHEN 'EC'
  THEN UPDATE Faculty SET
  department='Electronics and Communication';
 ELSE UPDATE Faculty SET
 department='Humanities and Social Sciences';
END CASE
Output:The department name corresponding to the given input gets renamed.
Consider another query which selects all the fields corresponding to the Faculty table. Since the values written in the Gender field are single character values (M/F), we would like to present them in a more readable format.SELECT FacultyID, Name, Department,
CASE Gender
 WHEN'M' THEN 'Male'
 WHEN'F' THEN 'Female'
END
FROM Faculty
Output:FacultyIDNameDepartmentGender001AakashCSMale002SahilECMale003JohnHSSMale004ShelleyCSFemale005AnannyaCSFemale006SiaHSSFemaleConsider yet another application of case-switch in SQL- custom sorting.CREATE PROCEDURE GetFaculty(@ColToSort varchar(150)) AS
SELECT FacultyID, Name, Gender, Department
FROM Customers
ORDER BY
  CASE WHEN @ColToSort='Department' THEN Department
       WHEN @ColToSort='Name' THEN Name
       WHEN @ColToSort='Gender' THEN Gender
       ElSE FacultyID
  END 
Output:The output gets sorted according to the provided field.
The above procedure (function) takes a variable of varchar data type as its argument, and on the basis of that, sorts the tuples in the Faculty table.This article is contributed by Anannya Uberoi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.My Personal Notes
arrow_drop_upSave
The first takes a variable called case_value and matches it with some statement_list.CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

The second considers a search_condition instead of variable equality and executes the statement_list accordingly.CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE

The second considers a search_condition instead of variable equality and executes the statement_list accordingly.
CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE

Examples:
Say we have a relation, Faculty.
Faculty Table:
Let’s say we would like to modify this table such that if the department name is ‘CS’, it gets modified to ‘Computer Science’, if it is ‘EC’ it gets modified to ‘Electronics and Communication’, and if it is ‘HSS’ it gets modified to ‘Humanities and Social Sciences’. This can be achieved using case statement.
Sample Query:Consider a variable, department_name which is entered in the SQL code.
CASE department_name
 WHEN 'CS'
  THEN UPDATE Faculty SET
  department='Computer Science';
 WHEN 'EC'
  THEN UPDATE Faculty SET
  department='Electronics and Communication';
 ELSE UPDATE Faculty SET
 department='Humanities and Social Sciences';
END CASE

Output:
The department name corresponding to the given input gets renamed.

Consider another query which selects all the fields corresponding to the Faculty table. Since the values written in the Gender field are single character values (M/F), we would like to present them in a more readable format.
SELECT FacultyID, Name, Department,
CASE Gender
 WHEN'M' THEN 'Male'
 WHEN'F' THEN 'Female'
END
FROM Faculty

Output:
Consider yet another application of case-switch in SQL- custom sorting.
CREATE PROCEDURE GetFaculty(@ColToSort varchar(150)) AS
SELECT FacultyID, Name, Gender, Department
FROM Customers
ORDER BY
  CASE WHEN @ColToSort='Department' THEN Department
       WHEN @ColToSort='Name' THEN Name
       WHEN @ColToSort='Gender' THEN Gender
       ElSE FacultyID
  END 

Output:
The output gets sorted according to the provided field.

The above procedure (function) takes a variable of varchar data type as its argument, and on the basis of that, sorts the tuples in the Faculty table.
This article is contributed by Anannya Uberoi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
SQL-Clauses-Operators
SQL
SQL
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
SQL | Views
CTE in SQL
Difference between DELETE, DROP and TRUNCATE
SQL using Python
SQL | DDL, DML, TCL and DCL
SQL | TRANSACTIONS
SQL | Sub queries in From Clause
Difference between Where and Having Clause in SQL
SQL Trigger | Student Database
Difference between DDL and DML in DBMS