CASE Statement in SQL – JavaDream

Home / CASE Statement in SQL – JavaDream

In this post we will see CASE Statement in SQL. CASE Statement is like if-else block of programming language. Like in programming language we perform some task if the condition is true if not then we perform other task in else block same functionality is used for CASE statement. We use CASE when we have to perform some task based on the condition.

Syntax of Case statement in MySQL is like :

CASE
WHEN SOME_CONDITION THEN CONDITION_RESULT
WHEN SOME_CONDITION THEN CONDITION_RESULT
ELSE ELSE_CONDITION_RESULT
END

For using CASE Statement in SQL just take an Example suppose If i have a table of Student that contains two column name, course. Now i want that if the course is CSE then i will display you have exam tomorrow and for other courses i want to display Enjoy you don’t have exam tomorrow. So in this case we use CASE function.

Student Table:

CASE Statement in SQL

Now we will use CASE function in above table. The query will be like below.

select name,course,
case
when course = "CSE" then "you have Exam tomorrow"
else "Enjoy the day you don't have exam"
end as ExamStatus
from student;
SQL CASE Statement

You may also like:

How to database backup in MySQL

MySQL Dates and Times function example

Integration of Springboot with MySQL Database Example.

Refrences:

MySQL official docs for functions.

Help Others, Please Share

About Author

Leave a Reply

Your email address will not be published. Required fields are marked *