Introduction
COALESCE helps you pick the first value that is not empty or missing from a list. It makes sure you get a real value instead of nothing.
COALESCE(value1, value2, ..., valueN)
SELECT COALESCE(NULL, 'apple', 'banana');
SELECT COALESCE(NULL, NULL, 10, 20);
SELECT COALESCE(NULL, NULL, NULL);
CREATE TABLE fruits ( id SERIAL PRIMARY KEY, name TEXT, favorite_color TEXT ); INSERT INTO fruits (name, favorite_color) VALUES ('Apple', NULL), (NULL, 'Red'), (NULL, NULL); SELECT id, COALESCE(name, favorite_color, 'Unknown') AS display_name FROM fruits ORDER BY id;
COALESCE function do in PostgreSQL?COALESCE in PostgreSQL?users with columns id, nickname, and fullname, what will this query return?SELECT id, COALESCE(nickname, fullname, 'Anonymous') AS display_name FROM users;
nickname = NULL and fullname = 'John Doe'.SELECT id, COALESCE nickname, fullname, 'Guest' AS display_name FROM users;
email, phone, and address. If all are NULL, show 'No contact info'. Which query correctly uses COALESCE to achieve this?Pass the test and this pattern moves unknown