---
title: "How to use the COALESCE function to handle NULL values in SQL queries?"  
description: "How to use the COALESCE function to handle NULL values in SQL queries?"  
author: "Sandra Emily"  
published: 2023-09-04  
updated: 2023-09-26  
canonical: https://www.mindstick.com/forum/159818/how-to-use-the-coalesce-function-to-handle-null-values-in-sql-queries  
category: "mssql server"  
tags: ["sql server", "sql"]  
reading_time: 2 minutes  

---

# How to use the COALESCE function to handle NULL values in SQL queries?

How to use the [COALESCE](https://www.mindstick.com/forum/158915/explain-the-purpose-of-the-coalesce-function-in-sql-and-provide-an-example) [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to [handle NULL values](https://www.mindstick.com/forum/159877/how-do-i-handle-null-values-when-using-linq-to-prevent-exceptions) in [SQL queries](https://www.mindstick.com/forum/160287/how-can-parameterized-stored-procedures-improve-performance-compared-to-dynamic-sql-queries)?

## Replies

### Reply by Aryan Kumar

You can use the **COALESCE** function in SQL to [handle null](https://www.mindstick.com/forum/159507/handle-null-checks-in-tree-traversal) values in [queries](https://www.mindstick.com/forum/33678/sub-queries-in-sql-server) by returning the first non-null value from a list of expressions. It's particularly useful when you want to replace [null values](https://www.mindstick.com/interview/34072/optional-class-in-java-handling-null-values-more-effectively) with a default value or a value from another column. Here's the syntax for the **COALESCE** function:

```plaintext
COALESCE(expression1, expression2, expression3, ...)
```

- **expression1**, **expression2**, **expression3**, and so on are the values or columns you want to evaluate. The function returns the first non-null expression from left to right.

Here are a few common use cases for the **COALESCE** function:

**Replace Null with a Default Value**:

- Suppose you have a column named **price**, and you want to display a default value of 0 when it's null:

```plaintext
SELECT COALESCE(price, 0) AS adjusted_price
FROM products;
```

In this query, if **price** is null, it will be replaced with 0 in the result set.

**Select the First Non-Null Value from Multiple Columns**:

- You can use **COALESCE** to select the first non-null value among multiple columns. For example, if you have columns **first_name** and **last_name** and you want to display the first non-null name:

```plaintext
SELECT COALESCE(first_name, last_name) AS full_name
FROM users;
```

If **first_name** is null, it will display the value from **last_name** as **full_name**.

**Handle Nested Nulls**:

- You can nest **COALESCE** functions to handle situations where multiple columns may contain null values:

```plaintext
SELECT COALESCE(col1, COALESCE(col2, col3, col4, 'No value')) AS result
FROM your_table;
```

This query checks **col1** first, and if it's null, it checks **col2**, and so on. If all columns are null, it returns 'No value'.

The **COALESCE** function is a powerful tool for handling null values and providing default values or alternate values in SQL queries, making your results more informative and user-friendly.


---

Original Source: https://www.mindstick.com/forum/159818/how-to-use-the-coalesce-function-to-handle-null-values-in-sql-queries

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
