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.
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
- SQL | DESCRIBE Statement
- SQL | MERGE Statement
- SELECT INTO statement in SQL
- SQL | UPDATE Statement
- SQL | DELETE Statement
- SQL | INSERT INTO Statement
- MERGE Statement in SQL Explained
- SQL | INSERT IGNORE Statement
- MySQL | CREATE USER Statement
- PLSQL | UPPER Function
- PLSQL | ACOS Function
- PLSQL | TRANSLATE Function
- PLSQL | ASIN Function
- PLSQL | VSIZE Function
Say we have a relation, Faculty.
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.
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
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
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
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 firstname.lastname@example.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.