SQL - CASE Function

SQL - CASE Function

The CASE statement selects one sequence of statements to execute. However, to select the sequence, the CASE statement uses a selector rather than multiple Boolean expressions.

Syntax for MySQL

CASE [ selector ]
   WHEN condition_1 THEN result_1
   ...
   WHEN condition_n THEN result_n
   ELSE result
END
Parameters or Arguments: selector - an optional expression, whose value is used to select one of several alternatives. condition_1, ... condition_n - Evaluated in the order listed. Once a condition is found to be true, the CASE function will return the result and not evaluate the conditions any further. result_1, ... result_n - The value returned once a condition is found to be true.

NOTE

If no condition is found to be true, then the CASE function will return the value in the ELSE clause, and if the ELSE clause is also omitted, then the CASE statement will return NULL.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
SQL snippets
Cosmo
Sign Up Now
or Subscribe for future quizzes