How can I apply IF...THEN in an SQL SELECT?
How can I apply IF...THEN in an SQL SELECT?
Student
Skilled in SEO, content writing, and digital marketing. Completed several years of working in many organizations including multinational companies. I love to learn new things in life that keep me motivated.
In SQL, you can use the CASE expression to implement conditional logic in a SELECT statement, similar to the IF...THEN construct in other programming languages. Here's an example SQL query that demonstrates how to use the CASE expression in a SELECT statement:
In this query, the CASE expression is used to determine the value of column4 based on the value of column3. If column3 is greater than 0, the value of column4 is set to 'Positive', if column3 is less than 0, the value of column4 is set to 'Negative', and if column3 is equal to 0, the value of column4 is set to 'Zero'. The resulting SELECT statement returns four columns: column1, column2, column3, and column4.