the CASE statement, the CASE statement goes through the conditions and returns a value when
the first condition is met it is the same as the IF, THEN and ELSE statement. for example, we have
the first condition when the condition number one is met then the result number one will be returned
as a value if not it will go to the condition number two so when the condition number two
is true then the result number 2 will be returning and if the condition number 1 and 2
is not true it will return the result number 3 in the ELSE clause if the ELSE clause is not
specified or not written in the statement it will return null as a value so let's see in practical
example how to use the CASE statement. now I am on the school live studio and I have selected the
test database inside the test database we have an EMP_DATA table so let me select everything.
from the EMP_DATA employee data table let's see what we have we have these data
at the end we have the salary so if I go through the salary column I can see a different type of
salary now if I want to use the case statement to determine the low salary high salary and
median salary from the column salary I can use the case statement for this I will use
select so I would like to return the first name
of the employee and the salary and then I would like to use the case first I will
write the CASE then I will go down and write when and then I'll write the first condition
if the salary is less than 25 hundred dollars then
the salary is low
then I'll go to the second condition when the salary is lower than
then the salary is
medium salary
I can write else
then i have to write and then from we should select these things from what from the table
name emp data so let's run this syntax and see what we will get as you see we have returned the
first name salary and as you see we have a very long column name for this new column
so i can use alias for this column after end i can write as salary to range and let's run the syntax
again as you see we have a better name for this column so as you see if the salary is lower than
2005 it determines like a low salary and the rest has been accordingly detailed minded
now let me run the select all query to get all the data if you notice we have a job title at the end
with null values if they ask you to update this job title column according to the salary range
like this you can do it using the case statement so i have written the CASE statement according to
the salary salary range they gave it to me so any salary between this number will be this job title
so now i have to change the select statement to update statement so i have to write update
table name emp data and then set the column value set the job title equals to a value the
value will come from the case statement result so any result comes from the case will be applied as
a value to the job title we don't need that from emp data and we just close the statement so let's
run this update statement and see what we will get so i'm running the update query and then
let's select everything from the emp data and see what will happen as you see the job title
has been updated and it is a bit difficult to see so let me let me modify the select statement to
only select the salary and the job title from the emp data as you see the job title has been updated
notice that we have some null values because the conditions are not made for these numbers
so in case like this we can add the else close to the case statement any condition is not met this
also can be applied for example i'll write general manager and then if i run the update query again
it will update the the column job title and as you see the nulls value has been updated