Ask Your Question
0

How to use IF condition with SELECT AS in SQL?

asked 2022-12-26 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-06-08 02:00:00 +0000

lakamha gravatar image

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'.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-26 11:00:00 +0000

Seen: 8 times

Last updated: Jun 08 '21