00:00Hello and welcome once again to Slidescope. We are learning practical usage of MySQL case statement.
00:07So the case statement in MySQL allows you to perform conditional logic inside SQL queries.
00:12It's like if else or switch statement in programming language.
00:17Let's say we have a table where we have a student name, student marks and based on their marks we want to give them grades.
00:24So if a student has scored more than 80 then we have to give them grade A more than 60, grade B more than 40, grade C.
00:33So output will be like this. If Alice who has scored 85 will get A, Bob will get B, Carol will get C and so on.
00:41So the simple statement for this will be select name, marks the columns that you want to select from your original table.
00:47Then a third column will be case and then in the next line we will write when marks is greater than equal to 80 then A, when marks is greater than equal to 60 then B, when marks is greater than equal to 40 then C and else F.
01:05So whatever condition you want to give if none of the conditions are fulfilled then you will write and so where you started and as and then this is the name.
01:16So you can see this is the name of that column and finally from students.
01:20So select name, marks, this entire case as grade from students and your output will be like this.
01:27We have a complete MySQL tutorial on our channel. Link is provided in the description. Check that out.