---
title: "How can I write an IF...THEN in an SQL SELECT?"  
description: "How can I write an IF...THEN in an SQL SELECT?"  
author: "Revati S Misra"  
published: 2023-07-11  
updated: 2023-07-12  
canonical: https://www.mindstick.com/forum/159025/how-can-i-write-an-if-then-in-an-sql-select  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# How can I write an IF...THEN in an SQL SELECT?

How can I write an [IF...THEN in an SQL](https://www.mindstick.com/forum/157762/how-can-i-apply-if-then-in-an-sql-select) [SELECT](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance)?

## Replies

### Reply by Aryan Kumar

You can write an `if then` in an [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) select by using the `CASE` statement. The `CASE` statement allows you to execute different code depending on the value of a condition.

The syntax for writing an `if then` in an SQL select is as follows:

SQL

```plaintext
SELECT
  column1,
  column2,
  CASE
    WHEN condition1 THEN value1
    WHEN condition2 THEN value2
    ELSE value3
  END AS if_then_column
FROM table_name;
```

The `column1`, `column2`, etc. are the columns that you want to select. The `condition1`, `condition2`, etc. are the conditions that you want to check. The `value1`, `value2`, etc. are the values that you want to return if the condition is met. The `if_then_column` is the name of the new column that will be created.

For example, the following code will select the `CustomerName` column and the `Country` column from the `Customers` table. If the `Country` column is `USA`, then the `if_then_column` will be set to `US Customer`. Otherwise, the `if_then_column` will be set to `Non-US Customer`.

SQL

```plaintext
SELECT
  CustomerName,
  Country,
  CASE
    WHEN Country = 'USA' THEN 'US Customer'
    ELSE 'Non-US Customer'
  END AS if_then_column
FROM Customers;
```


---

Original Source: https://www.mindstick.com/forum/159025/how-can-i-write-an-if-then-in-an-sql-select

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
