August 18, 2020 | Spring boot complete tutorial with example | No Comments
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.
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;