Select Conditionally using CASE Statement

Author: Al-mamun Sarkar Date: 2021-04-26 23:18:40

We can use conditions while selecting data using CASE, ELSE Statement.  In this lesson, I will show you how we can provide conditions at the time of selecting data. 

 

CASE Syntax:

CASE
    WHEN condition THEN output
    WHEN antoher_condition THEN anohter_output
    ELSE output
END;

 

Example:

Adding Mr for male empalyess and Miss for female employee with their name:

SELECT email, 
CASE
	WHEN gender = 'male' THEN CONCAT('Mr. ', name)
    WHEN gender = 'female' THEN CONCAT('Miss. ', name)
    ELSE name
END as name
FROM employees;