---
title: "How can I apply IF...THEN in an SQL SELECT?"  
description: "How can I apply IF...THEN in an SQL SELECT?"  
author: "Revati S Misra"  
published: 2023-04-12  
updated: 2023-04-13  
canonical: https://www.mindstick.com/forum/157762/how-can-i-apply-if-then-in-an-sql-select  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 1 minute  

---

# How can I apply IF...THEN in an SQL SELECT?

How can I [apply](https://www.mindstick.com/articles/13148/discover-to-apply-make-up-like-a-professional-supermodel) [IF...THEN in an SQL](https://www.mindstick.com/forum/159025/how-can-i-write-an-if-then-in-an-sql-select) [SELECT](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance)?

## Replies

### Reply by Krishnapriya Rajeev

In [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database), 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:

```plaintext
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**.


---

Original Source: https://www.mindstick.com/forum/157762/how-can-i-apply-if-then-in-an-sql-select

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
