COALESCE in PostgreSQL

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Overview

The COALESCE function in PostgreSQL is a powerful tool for handling NULL values in a query. Dealing with NULL values can be tricky and might lead to unexpected results. The COALESCE function in PostgreSQL comes to the rescue by allowing us to replace NULL values with non-null alternatives, making our queries more robust and predictable. This article will dive into the details of the COALESCE function in PostgreSQL, its syntax, usage examples, and how it helps prevent NULL-related issues in PostgreSQL queries.

What is the COALESCE Function in PostgreSQL?

The COALESCE function in PostgreSQL is designed to evaluate a list of expressions and return the first non-NULL value from the list. Its primary purpose is to handle situations where a value might be NULL and replace it with a meaningful alternative. The function takes any number of arguments, such as columns, literals, or even other functions, and returns the first non-NULL value encountered in the list.

The syntax of the COALESCE function in PostgreSQL is straightforward, and we'll explore it in the next section.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

PostgreSQL COALESCE Function Syntax

The syntax of the COALESCE function is as follows:

Here, expr1, expr2, and so on are the expressions or values that we want to evaluate. PostgreSQL will check each expression in order and return the first non-NULL value. If all the expressions evaluate as NULL, the COALESCE function will also return NULL.

Let's move on to an example to better understand the syntax and behavior of the COALESCE function in PostgreSQL.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

PostgreSQL COALESCE Example

Suppose we create a table named "students" with the following SQL query.

This query creates a table called "students" with four columns: "id", "name", "age", and "city". The "id" column is defined as SERIAL, which will automatically generate a unique value for each new row. The "name" column is defined as VARCHAR(100) and is set as NOT NULL, meaning it cannot contain NULL values. The "age" and "city" columns allow NULL values.

Next, we insert the data into the "students" table using the following SQL queries.

These queries will insert the provided data into the "students" table, creating three rows with the specified values for each column. The "id" column will automatically generate unique values due to the SERIAL data type.

The final data in the "students" table looks as follows:

idnameagecity
1Alice23New York
2BobNULLNULL
3CharlieNULLChicago

Now, let's say we want to retrieve the age and city of the students, but if the age or city is NULL, we want to display a default value, such as "Not specified". We can achieve this using the COALESCE function as follows:

Output:

The result of the query will be:

nameagecity
Alice23New York
BobNot specifiedNot specified
CharlieNot specifiedChicago

In this example, the COALESCE function evaluated the age and city columns and replaced the NULL values with the default value "Not specified". As a result, our query provides a more informative and user-friendly output.

Preventing NULL Values with COALESCE

One of the main advantages of the COALESCE function is its ability to prevent NULL-related issues in queries. NULL values can lead to unexpected results and errors when performing calculations or comparisons. Using COALESCE to replace NULL values with meaningful alternatives ensures that our queries produce consistent and reliable outputs.

For example, consider a scenario where we have a table named "student_scores" which stores the scores of students obtained in different subjects. The SQL query to create this table looks as follows:

Next, let us insert data into the "student_scores" table using the following set of SQL queries.

Now, we have the "student_scores" table with data:

idnamemath_scorescience_scorehistory_score
1Alice908592
2Bob75NULL88
3Charlie8590NULL

We want to calculate the total score of students but some of the scores are missing (i.e. having a NULL value). We can use COALESCE to treat the NULL scores as zero and correctly calculate the total:

Output:

nametotal_score
Alice267
Bob163
Charlie175

Even if some of the scores are NULL, this query will handle them gracefully by treating them as zeros and providing an accurate total score for each student.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Conclusion

  • The COALESCE function in PostgreSQL is a valuable tool for handling NULL values in queries.
  • The syntax of the COALESCE function is as follows: COALESCE(expr1, expr2, ...)
  • The COALESCE function in PostgreSQL is designed to evaluate a list of expressions and return the first non-NULL value from the list.
  • The function takes any number of arguments, such as columns, literals, or even other functions, and returns the first non-NULL value encountered in the list.
  • Using COALESCE, we can provide default values, prevent NULL-related issues, and ensure our queries yield consistent and meaningful results.