how to write a if condition within a select statement? how to write a if condition within a select statement? sql sql

how to write a if condition within a select statement?


You should use CASE expression.

Example:

SELECT name,salary,CASE    WHEN  salary <= 2000 THEN 'low'WHEN  salary > 2000 AND salary <= 3000 THEN 'average'WHEN  salary > 3000 THEN 'high'END AS salary_levelFROM employeesORDER BY salary ASC

And in this way you should adapt your query to match your needs.