Do I need an else clause in a case expression?

Should I include an ELSE in a CASE expression?

For example, if I wanted to output the names of animals that are cats and nothing ELSE, can I use this SELECT :

 SELECT DISTINCT(CASE WHEN animal_type = 'cat' THEN animal_name END) AS cat_names 

I know that I can just put animal_type = 'cat' in my WHERE and then

 SELECT DISTINCT cat_names, 

but I would like to know the answer.

+6
source share
1 answer

You do not need else . If not specified, case will return null . In other words, it acts as if it has an else null clause.

+11
source

Source: https://habr.com/ru/post/975539/


All Articles