(SQL Example for Citizen Data Scientist & Business Analyst)
SQL | Case Statement
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
- The first takes a variable called case_value and matches it with some statement_list.
Examples:
Say we have a relation, Faculty.
Faculty Table:
FACULTYID | NAME | DEPARTMENT | GENDER |
---|---|---|---|
001 | Aakash | CS | M |
002 | Sahil | EC | M |
003 | John | HSS | M |
004 | Shelley | CS | F |
005 | Anannya | CS | F |
006 | Sia | HSS | F |
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:
FACULTYID | NAME | DEPARTMENT | GENDER |
---|---|---|---|
001 | Aakash | CS | Male |
002 | Sahil | EC | Male |
003 | John | HSS | Male |
004 | Shelley | CS | Female |
005 | Anannya | CS | Female |
006 | Sia | HSS | Female |
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.
Learn to Code SQL Example – SQL | Case Statement
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.