From the course: PostgreSQL: Advanced Queries
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Merge columns with COALESCE - PostgreSQL Tutorial
From the course: PostgreSQL: Advanced Queries
Merge columns with COALESCE
- The coalesce function can be used to choose an output from multiple columns, if those columns may contain null values. It returns the first non-null value that it finds. The function looks something like this. We'll select the coalesce function. And I'll pass in three pieces of text: the letter A, B, and C. Inside of the parentheses, you can list out as many or as few options as you want. Here, I'm just hard-coding the letters A, B, and C, but these are more typically column references to a table. Then, when the function is executed, it outputs the first value that isn't null, or empty. In this case, that's the letter A. I'll change the query to make the first option null and then run it again. This time, the first value is null, so it skips to the second value, and that's exactly what it returns, B. If the second value is also null, then this query is going to return C, and if all of the options within the…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.