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:
SELECT column1, column2,
CASE
WHEN column3 > 0 THEN 'Positive'
WHEN column3 < 0 THEN 'Negative'
ELSE 'Zero'
END AS column4
FROM my_table;
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.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
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.