Select Conditionally using CASE Statement

Select Conditionally using CASE Statement

Instructor-svgAl-Mamun Sarkar
Apr 26 , 2021

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;

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram