![]() These are the results that are to be added to the table column or the particular cell when their corresponding condition is true. These are the conditions based on which the values are added in the table column or on a particular cell. The syntax of the Case Statement in SQL is as follows: The END keyword is followed by an alias_name, which is the column name you are adding to the table based on the conditions. We can also write an ELSE keyword that would execute when all other conditions specified in the case statement return false. The CASE keyword is used to specify the beginning of the case statement, whereas the END keyword specifies the end of the case statement and the end of the conditions. However, the CASE, WHEN, THEN, and END keywords are necessary while writing a case statement in SQL. You can specify as many conditions using these keywords. The conditions are specified using WHEN and THEN keywords. The case statement in SQL also works similarly. This condition will be based on some other cell in the same table.Īs in the if-else statement, several conditions are applied, and whichever first gives the result as true is executed. The Case Statement in SQL returns a value when some condition is specified. The case statement in SQL will add the value only when the given condition based on some other cell is true. In these scenarios, we use the case statement. Grades and marks will be two different columns in a table, and we will use the marks column to calculate the values of the grades column. ![]() For example, we need to calculate the students' grades based on their marks. This is an effective way to use the CASE statement as it will be more readable and understandable.Sometimes, a situation may arise when adding data to a particular cell depends on a condition based on another column.So we had provided the salary range inside the when and used then to return a result for all the others like the same.We have rounded off too to get the nearest integer.According to the tax rates table, salary range between 5000000 shall be subtracted with 500000 from LPA and get 20% off of it Adding 12500 with it.So CASE statement is used well in this example.select Name, Salary as "Salary(In LPA)", (case We have the names and salaries of each individual in LPA.Based on these tax rates, we will calculate the tax amount for each individual with a specific range of salaries that falls between these tax rates.Let us calculate the tax amount of individuals with a specific range of salaries. Let us look at a more realistic case scenario where CASE can use this. Use END at the final stage of CASE to end the statement.Start with CASE within it provide N number of conditions based on WHEN and THEN.If no ELSE is provided and no conditions are satisfied, the table value will be null. If no conditions satisfy, it automatically goes to the ELSE and returns whatever is provided inside. Think of it more like IF, ELSE, and THEN statement where a particular part is satisfied when the provided condition is true. The CASE function in MySQL provides certain conditions based on which, if it is true, then it returns the result.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |