본문 바로가기
Study Note/SQL

Difference between COALESCE and IFNULL

by jhleeatl 2024. 4. 23.

COALESCE

COALESCE function in SQL is used to return the first non-NULL value among its arguments.

It accepts multiple arguments and returns the first non-NULL value encountered.

If all arguments are NULL, `COALESCE` returns NULL.

Example:

Suppose we have a table named `sales` with columns `product_id`, `sale_date`, and `sale_amount`. Some records in the `sale_amount` column might have NULL values indicating missing data.

-- Example of using COALESCE function
SELECT product_id, COALESCE(sale_amount, 0) AS corrected_sale_amount
FROM sales;



In this example, the `COALESCE` function is used to replace NULL values in the `sale_amount` column with 0. 

It returns the `product_id` along with the corrected `sale_amount`. If `sale_amount` is NULL for a particular record, it will be replaced with 0 in the result set.

`COALESCE` offers flexibility in handling NULL values, allowing for more concise and readable SQL queries.

 

 


 

 

IFNULL

IFNULL function in SQL is used to replace NULL values with a specified alternative value. It accepts two arguments: the first argument is checked for NULL, and if it is NULL, the second argument is returned; otherwise, the first argument is returned.

 


Example:

Suppose we have a table named `employees` with columns `employee_id`, `employee_name`, and `salary`. Some records in the `salary` column might have NULL values indicating missing data.

 

-- Example of using IFNULL function
SELECT employee_id, employee_name, IFNULL(salary, 0) AS corrected_salary
FROM employees;



In this example, the `IFNULL` function is used to replace NULL values in the `salary` column with 0. It returns the 

`employee_id`, `employee_name`, and the corrected `salary`. If `salary` is NULL for a particular employee, it will be 

replaced with 0 in the result set.

`IFNULL` provides a simple way to handle NULL values and can be particularly useful when you want to replace NULL with a specific default value in your query results.

 


 

Differences between `COALESCE` and `IFNULL` 

 

Number of Arguments

  - `COALESCE` accepts one or more arguments.
  - `IFNULL` accepts exactly two arguments.

Behavior

- `COALESCE` returns the first non-NULL value among its arguments. If all arguments are NULL, it returns NULL.
- `IFNULL` checks if the first argument is NULL. If it is NULL, it returns the second argument; otherwise, it returns the first argument.

 


Example:

Suppose we have a table named `products` with columns `product_id`, `product_name`, and `price`. Some records in the `price` column might have NULL values indicating missing data.

-- Example of using COALESCE function
SELECT product_id, product_name, COALESCE(price, 0) AS corrected_price
FROM products;


In this example, `COALESCE` is used to replace NULL values in the `price` column with 0. It returns the `product_id`, `product_name`, and the corrected `price`. If `price` is NULL for a particular product, it will be replaced with 0 in the result set.

-- Example of using IFNULL function
SELECT product_id, product_name, IFNULL(price, 0) AS corrected_price
FROM products;



In this example, `IFNULL` is used similarly to replace NULL values in the `price` column with 0. It returns the `product_id`, `product_name`, and the corrected `price`. If `price` is NULL for a particular product, it will also be replaced with 0 in the result set.

Difference Illustrated:

- `COALESCE`:
  - If there are multiple columns to consider, `COALESCE` can handle them easily by providing multiple arguments.
  - It can handle more complex scenarios where you need to consider multiple columns or expressions.

- `IFNULL`:
  - Limited to replacing NULL with a single alternative value.
  - Useful when you have only one column to consider and want to replace NULL with a specific value.

'Study Note > SQL' 카테고리의 다른 글

With CTE  (0) 2024.05.20
Union & Union All  (0) 2024.05.01
How to add localhost at DBeaver (MY SQL)  (0) 2024.04.16
Having  (0) 2024.04.11
7. Pivot, Window function (Rank & sum), Date  (0) 2024.03.22