Javatpoint Logo
Javatpoint Logo

Coalesce SQL

There are many functions available in SQL programming that are used to transform data. One such powerful function in SQL programming is Coalesce, which allows handling NULL values in SQL queries. In this article, we will learn about Coalesce SQL.

Coalesce is an SQL function that returns the first non-null value from a list of input values. If each parameter is NULL, then it returns the NULL.

Syntax of Coalesce function:

Advantages of using the Coalesce function in SQL:

Handling NULL values:

NULL values are a common occurrence in databases and can often lead to unwanted results when performing calculations. Coalesce function provides a good solution by permitting SQL developers to replace NULL values with an optional value. It ensures that the query returns significant results even in the existence of NULL.

Simplifying conditional logic:

Coalesce SQL removes the need for complex conditional logic for handling NULLs. SQL Developers can use Coalesce to provide a default value in case of NULL in place of creating numerous IF statements. It facilitates the code and makes it more effortless to read.

Efficient Data Recovery:

Coalesce can improve the efficiency of SQL data recovery operations. SQL developers can use Coalesce to combine many columns into a single result in place of querying individual columns and checking for NULL values. It enhances performance, especially when working with massive datasets.

Change data for reporting:

Coalesce SQL is mainly useful when constructing reports or presenting data. It allows SQL developers to combine multiple columns or fields into coherent output and provide a consolidated view of the data. This consolidation simplifies reporting tasks and improves the display of results to stakeholders.

Increased Readability:

Coalesce improves the readability of SQL queries by explicitly handling NULL values. When you encounter the Coalesce function in a query, it simply tells you that you are dealing with NULL values and provides a straightforward way to handle them. It helps maintain the SQL code and helps other developers to understand the code easily so that they can work without any issues.

Flexibility in Expressions:

The Coalesce function permits you to specify multiple expressions as arguments. It evaluates each expression in the order they are provided and returns the first non-null value. It gives you flexibility in defining the alternate expressions to use in case of NULL values. You can use as many expressions as required, and the function will return the first non-null value encountered.

Performance Optimization

The use of Coalesce function can improve query performance. When you have complex conditions involving NULL checks, using Coalesce function helps in removing the requirement for adding more conditional logic. It reduces the overall query complexity, which leads to improved performance.

Cross-Platform Compatibility:

The Coalesce function is widely supported by various database management systems (DBMS) that follow SQL standards. It is available in famous relational databases, including Oracle, SQL Server, MySQL, PostgreSQL, etc. It implies that you can continuously use the Coalesce function across various database platforms and make your SQL code portable and easily adaptable to different environments.

Examples of Coalesce function:

Example 1:

Let us create a table named 'students', which has fields such as Id, Name, Age, Subject, and Address.

Use the given command to construct the 'students' table:

The table is created, and it will look like you can see below:

Id Name Age Subject Address

Now, we will add values in the 'students' table,

After inserting the values in the 'students' table, it will look like you can see below:

Id Name Age Subject Address
201 Virat 24 Computer Science NULL
202 Sanjay 21 NULL Noida
203 Anjana 20 Mathematics NULL
204 Kriti 19 Mathematics Lucknow
205 Shivani 23 NULL Lucknow
206 Reshma 23 Arts NULL

Now, we will use the COALESCE function to obtain the first non-null value from the 'students' table. To do this, we will be using the given command:

As you can see below, a table is generated with 'Id', 'Name', 'Age', and 'INFO'. We have not applied the COALESCE function to 'Id', 'Name', and 'Age', so they will all remain the same. But we have used COALESCE function on the 'Subject' and 'Address' columns, so we have got the first non-null value from the 'Subject' and 'Address' columns and inserted that non-null value in the new column named 'INFO'.

Coalesce SQL

Example 2:

Let us create a table named 'employees', which has fields such as Id, first_name, middle_name, last_name, Address, and Salary.

Use the given command to create the 'employees' table:

The table is created, and it will look like you can see below:

Id first_name middle_name last_name Address Salary

Now, we will insert values in the 'employees' table,

After inserting the values in the 'employees' table, it will look like you can see below:

Id first_name middle_name last_name Address Salary
1 Rohit NULL Mishra Goa 50000
2 Sameer NULL NULL Meerut 49000
3 NULL NULL Singh Goa 35000
4 Rohan NULL Kapoor Bhopal 45000
5 Aarti NULL NULL Meerut 37000
6 NULL Singh Tomar Meerut 47000

Now, we will use the COALESCE function to obtain the first non-null value from the 'employees' table. To do this, we will be using the given statements:

As you can see below, a table is generated with 'Id', 'Address', 'Salary', and 'Name'. We have not applied the COALESCE function to 'Id', 'Address', and 'Salary', so they will all remain the same. But we have used COALESCE function on the 'first_name', middle_name', and last_name' columns, so we have got the first non-null value from the 'first_name', middle_name', and last_name' columns and inserted that non-null value in the new column named 'Name'.

Coalesce SQL

Example 3:

Let us create a table named 'customers', which has fields such as Id, Name, Gender, contact_no, and email.

Use the given command to create the 'customers' table:

The table is created, and it will look like you can see below:

Id Name Gender contact_no Email

Now, we will insert values in the 'customers' table,

After inserting the values in the 'customers' table, it will look like it is shown below:

Id Name Gender contact_no Email
1 Prachi F 9562548225 [email protected]
2 Rashi F NULL [email protected]
3 Kamal M NULL NULL
4 Harish M 8562314252 NULL
5 Vani F NULL [email protected]

Now, we will use the COALESCE function to get the first non-null value from the 'customers' table. To do this, we will be using the given statements:

As you can see below, a table is generated with 'Id', 'Name', 'Gender', and 'Information'. We have not applied the COALESCE function to 'Id', 'Name', and 'Gender', so they will all remain the same. But we have used COALESCE function on the 'contact_no', and 'Email' columns, so we have got the first non-null value 'contact_no', and 'Email' columns and inserted that non-null value in the new column named 'Information'.

You can also see that when both the 'contact_no', and 'Email' columns contain NULL, then the first non-null value will also be NULL.

Coalesce SQL

Conclusion:

In this article, you have learned about COALESCE function in SQL. The COALESCE function is used on columns that return the first non-null value from input columns. You have understood the various benefits of COALESCE function and its examples.


Next TopicCreate View SQL





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA