Then we'll give to the column the name grade using the AS keyword: SELECT *, CASEĪfter we've written the CASE statement, we will add it in a query. If the score is instead 90 or higher it will have the value of A-, and so on. When score is 94 or higher, the row will have the value of A. Let's first write the CASE statement, in which we will write the breakdown for each grade. We can use the CASE statement to give each student a grade, which we will add in a new column named grade. We need to give each student a grade, and we can use the case statement to do it automatically. We have a table with a list of students and their scores on an exam. Let's use the CASE statement in an example. Let's put this to practice to understand it better. This can then be followed by other WHEN/ THEN statements.Īt the end you can add a value to use by default if none of the conditions are true with the ELSE keyword, as shown below. After that comes the keyword THEN and the value for that condition, like WHEN THEN. Then for a single condition you can write the keyword WHEN followed by the condition that has to be satisfied. The syntax has a lot of stuff in it, but it is still rather intuitive: the keyword CASE signals the beginning of a case statement, and the keyword END signals its end. This can be really important in Data Analysis, so after introducing the Case Statement we will see a couple of examples of how you can use it to analyse data in a simple way. ![]() It allows you to conditionally specify a value, so that, depending on the condition satisfied, you get a different value in the cell. ![]() ![]() If you know other languages, the case statement in SQL is similar to an if statement, or a switch statement. If you need to add a value to a cell conditionally based on other cells, SQL's case statement is what you'll use.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |