Definition and Usage
The COALESCE() function returns the first non-null value in a list.
Syntax
COALESCE(val1, val2, …, val_n)
- Run the query entered below in the SQL workspace to notice the row with missing data.
SELECT *
FROM accounts a
LEFT JOIN orders o
ON a.id = o.account_id
WHERE o.total IS NULL;
- Use COALESCE to fill in the accounts.id column with the account.id for the NULL value for the table in 1.
SELECT COALESCE(a.id, a.id) filled_id, a.name, a.website, a.lat, a.long, a.primary_poc, a.sales_rep_id, o.*
FROM accounts a
LEFT JOIN orders o
ON a.id = o.account_id
WHERE o.total IS NULL;
- Use COALESCE to fill in the orders.account_id column with the account.id for the NULL value for the table in 1.
SELECT COALESCE