×

SQL CASE

This page contains all the information about SQL CASE. The CASE is an If-Else type of logical query used in the statement. The CASE in Structured Query Language is similar to nested If-Else in programming Languages.

The CASE Statement goes through a condition if the first condition is true. It returns a value; otherwise, jump to another condition. If that condition matches, it returns the value; otherwise, jump to the else part.

CASE statement can have one condition or can have multiple conditions.

If there is no Else part in the CASE statement, it returns NULL directly.

The CASE statement always has one WHEN and THEN pair or can have more than one pair.

The CASE statement starts with CASE keywords and ends with END keywords.

We can use the WHERE clause, ORDER BY clause, and GROUP BY clause in the SQL query with the CASE statement.

There are two types of CASE statements in RDBMS are as follows:

  1. Simple CASE Statement.
  2. Searched CASE Statement.

CASE Statement Syntax in Structured Query Language:

CASE <case-expression>
WHEN conditions THEN statement
ELSE result
END [as Alias Name];

The CASE statement can have multiple conditions.

In the syntax, CASE will start from the first condition. If conditions match what is in the WHEN clause, the THEN clause statement will get a return or be executed, and the rest of the conditions will be get ignored.

If the first condition didn't match the first WHEN conditions, then it will jump to the next WHEN conditions, and if that condition matches, THEN part will get a return or be executed.

If the second condition also didn't match, then it will move to another, and if it matches the conditions, return the statement, and if no conditions match, then else part will get executed.

Example of CASE statement in Structured Query Language:

Consider the already existing table with the following data:

Table Name: Employee_Details

E_IdE_NameE_SalaryE_CityDesignationAgeGender
1001Kirti Kirtane60000MumbaiProject Manager26F
1002Akash Vani40000PuneSystem Engineer22M
1003Anupam Mishra55000HyderabadProject Manager25M
1004Anuj Rawat45000HyderabadSoftware Tester25M
1005Akanksha Yadav42000PuneAssociate Software Developer23F
1006Bhavesh Wani50000MumbaiAssociate Software Developer24M
2001Sakshi Sharma40000BangaloreSystem Engineer23F
2002Megha Ghatole45000BangaloreSoftware Tester24F
2003Surbhi Nahar60000PuneProject Manager26F
2004Supriya Shende55000MumbaiSoftware Developer25F
2005Prachi Sharma52000HyderabadSoftware Developer24F
2006Purva Dhandekar50000BangaloreSoftware Tester23F
3001Shruti Deshpande60000PuneProject Manager26F
3002Rohit Nikam40000HyderabadSystem Engineer23M
3003Sahil Jain50000MumbaiSoftware Developer24M

Consider the already existing table with the following data:

Table Name: Comp

Comp_IdMakeE_Id
101Dell1006
102Dell1003
103Lenovo1001
104Lenovo1005
105HP2002
106HP2004
107Asus2006
108Asus2001
109Acer3001
110Acer3002

Example 1: We will first see the single-use of WHEN and THEN condition to the CASE Statement:

SELECT E_ID, E_NAME, E_SALARY, E_CITY, CASE WHEN E_SALARY > 50000 THEN 'SALARY INCREMENT BY 5%' ELSE 'SALARY INCREMENT BY 10%' END AS SALARY_INCREMENT FROM EMPLOYEE_DETAILS;

The CASE statement checks the first condition in the above query if the employee salary is greater than 50000. If yes, it will write "Salary increment by 5%". Else it will write "Salary increment by 10%" in the Salary_Increement column.

The output in the above query is as follows:

E_IDE_NAMEE_SALARYE_CITYSALARY_INCREEMENT
1001Kirti Kirtane60000MumbaiSALARY INCREMENT BY 5%
1002Akash Vani40000PuneSALARY INCREMENT BY 10%
1003Anupam Mishra55000HyderabadSALARY INCREMENT BY 5%
1004Anuj Rawat45000HyderabadSALARY INCREMENT BY 10%
1005Akanksha Yadav42000PuneSALARY INCREMENT BY 10%
1006Bhavesh Wani50000MumbaiSALARY INCREMENT BY 10%
2001Sakshi Sharma40000BangaloreSALARY INCREMENT BY 10%
2002Megha Ghatole45000BangaloreSALARY INCREMENT BY 10%
2003Surbhi Nahar60000PuneSALARY INCREMENT BY 5%
2004Supriya Shende55000MumbaiSALARY INCREMENT BY 5%
2005Prachi Sharma52000HyderabadSALARY INCREMENT BY 5%
2006Purva Dhandekar50000BangaloreSALARY INCREMENT BY 10%
3001Shruti Deshpande60000PuneSALARY INCREMENT BY 5%
3002Rohit Nikam40000HyderabadSALARY INCREMENT BY 10%
3003Sahil Jain50000MumbaiSALARY INCREMENT BY 10%
SQL CASE

Example 2: We will use the GROUP BY clause with the CASE Statement

SELECT E_ID, E_NAME, E_SALARY, E_CITY, CASE WHEN E_SALARY < 42000 THEN E_SALARY * 1.5 WHEN E_SALARY >= 42000 AND E_SALARY < 55000 THEN E_SALARY * 1.2 ELSE E_SALARY * 1 END AS EMPLOYEE_INC FROM EMPLOYEE_DETAILS GROUP BY E_NAME;

In the above query, we increment the salary of employees. If employee salary is less than 42000, then employee salary * 1.5, else if employee salary is greater than equal to 42000 and less than 55000, then employee salary increments by 1.2, else no increment of the employee in this query, we used GROUP BY Employee Name column.

The output of the above query is as follows:

E_IDE_NAMEE_SALARYE_CITYEMPLOYEE_INC
1005Akanksha Yadav42000Pune50400.0
1002Akash Vani40000Pune60000.0
1004Anuj Rawat45000Hyderabad54000.0
1003Anupam Mishra55000Hyderabad55000.0
1006Bhavesh Wani50000Mumbai60000.0
1001Kirti Kirtane60000Mumbai60000.0
2002Megha Ghatole45000Bangalore54000.0
2005Prachi Sharma52000Hyderabad62400.0
2006Purva Dhandekar50000Bangalore60000.0
3002Rohit Nikam40000Hyderabad60000.0
3003Sahil Jain50000Mumbai60000.0
2001Sakshi Sharma40000Bangalore60000.0
3001Shruti Deshpande60000Pune60000.0
2004Supriya Shende55000Mumbai55000.0
2003Surbhi Nahar60000Pune60000.0
SQL CASE

Example 3: Use of ORDER BY Clause with CASE Statement:

SELECT * FROM COMP ORDER BY CASE WHEN MAKE = 'DELL' THEN 0 ELSE 1 END, E_ID;

In the above query, we used the ORDER BY clause and displayed the data in order by making the name. Still, they also used one condition that employees' names are "Dell" should be displayed first and then the rest of the data order by make as well employee id.

The output of the above query is as follows:

Comp_IdMakeE_Id
102Dell1003
101Dell1006
103Lenovo1001
104Lenovo1005
108Asus2001
105HP2002
106HP2004
107Asus2006
109Acer3001
110Acer3002
SQL CASE

Example 4: In the given an example, we want to sort the result in the following methods:

  1. The employee salaries should come in ascending order where the employee gender is male.
  2. The employee salaries should come in descending order where employee gender is female.
SELECT E_ID, E_NAME, E_SALARY, E_CITY, GENDER FROM EMPLOYEE_DETAILS ORDER BY CASE GENDER WHEN 'M' THEN E_SALARY END ASC, CASE WHEN GENDER = 'F' THEN E_SALARY END DESC;

In the above, we sort the employee salary in ascending order if an employee is male; else employee salary will be descending where an employee is female.

The output of the above query is as follows:

E_IdE_NameE_SalaryE_CityGender
1001Kirti Kirtane60000MumbaiF
3001Shruti Deshpande60000PuneF
2003Surbhi Nahar60000PuneF
2004Supriya Shende55000MumbaiF
2005Prachi Sharma52000HyderabadF
2006Purva Dhandekar50000BangaloreF
2002Megha Ghatole45000BangaloreF
1005Akanksha Yadav42000PuneF
2001Sakshi Sharma40000BangaloreF
1002Akash Vani40000PuneM
3002Rohit Nikam40000HyderabadM
1004Anuj Rawat45000HyderabadM
1006Bhavesh Wani50000MumbaiM
3003Sahil Jain50000MumbaiM
1003Anupam Mishra55000HyderabadM
SQL CASE

Related Topics

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

SQL CONSTRAINTS

SQL Constraints specifies the rules/limitations/restrictions for data present in table. SQL Constraints are specified at the time of table creation or after table creation using ALTER command. There are two...

5 minutes read.

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL Cloning Tables

Cloning a Table: To create a copy of the table. To perform the operations, without affecting the actual table. Steps for creating a Cloning Table: Step 1: Empty Table Creation The syntax for Creating...

3 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

Nth highest salary

The most common and important question asked in interviews that how we can find the Nth highest salary in a table (2nd highest salary, 3rd highest salary, or Nth highest...

6 minutes read.

SQL Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

SQL HAVING Clause

In this tutorial, we will understand the HAVING clause concept in SQL with the help of examples. The HAVING Clause in the SQL is used as we cannot use the WHERE...

3 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 minutes read.

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

SQL KEYS

SQL KEYS are single or multiple attributes used to get data from the table according to the requirement or condition. They can also be used to set up relationships amongst...

3 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.