SQL CASE statement is a conditional flow that returns a value when the first condition is met (like an IF-THEN-ELSE statement). Once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.

If there is no ELSE part and none of the conditions are true, it returns NULL value.

SYNTAX

188.png

EXAMPLE

Let us consider a demo database with table name Persons.

The following Query executes through conditions and returns a value when the first condition is met:

190.png
 
189.png

Figure 47.1. PERSONS TABLE

Figure 47.2 QUERY OUTPUT

PRACTICE YOURSELF 

191.png