You can use the IF condition in the SELECT statement with SELECT AS by using the CASE statement.
The syntax of using IF condition with SELECT AS in SQL using the CASE statement is as follows:
SELECT column1, column2, CASE WHEN condition THEN value1 ELSE value2 END AS aliasname FROM tablename;
In this example, column1 and column2 are the original column names that you want to make use of. The CASE statement is used to define the IF condition along with the value to display if the condition is true and the value to display if the condition is false. Finally, the AS keyword is used to create an alias name for the newly created column.
Here is a practical example of using IF condition with SELECT AS in SQL:
SELECT employeename, salary, CASE WHEN salary > 50000 THEN 'High Salary' ELSE 'Low Salary' END AS salarystatus FROM employee_data;
In this example, the SELECT statement retrieves the employee name and salary from the employeedata table. The CASE statement checks whether the employee's salary is greater than 50000. If yes, it returns the value 'High Salary' for the newly created column salarystatus. If not, it returns the value 'Low Salary'.
Asked: 2022-12-26 11:00:00 +0000
Seen: 8 times
Last updated: Jun 08 '21