📖 Scenario: You are managing a small online store database. Some customers have not provided their phone numbers, so those entries are NULL. You want to create a query that shows each customer's name and phone number, but if the phone number is missing, it should show 'No Phone Provided' instead.
🎯 Goal: Build a SQL query using COALESCE to replace NULL phone numbers with the text 'No Phone Provided'.
📋 What You'll Learn
Create a table called customers with columns id (integer), name (text), and phone (text).
Insert exactly three customers with these details: (1, 'Alice', '123-456-7890'), (2, 'Bob', NULL), (3, 'Charlie', NULL).
Write a SELECT query that shows name and phone but replaces NULL phone values with 'No Phone Provided' using COALESCE.
Alias the replaced phone column as contact_phone.
💡 Why This Matters
🌍 Real World
Handling missing or NULL data is common in real databases. COALESCE helps show friendly default values instead of NULL.
💼 Career
Knowing how to manage NULL values with COALESCE is essential for database querying and reporting in many jobs.
Progress0 / 4 steps
1
Create the customers table and insert data
Write SQL statements to create a table called customers with columns id (integer), name (text), and phone (text). Then insert these three rows exactly: (1, 'Alice', '123-456-7890'), (2, 'Bob', NULL), and (3, 'Charlie', NULL).
PostgreSQL
Hint
Use CREATE TABLE to define the table and INSERT INTO to add the rows with exact values.
2
Set up the SELECT query base
Write a SELECT statement that selects the name and phone columns from the customers table. This will be the base query before adding COALESCE.
PostgreSQL
Hint
Use SELECT name, phone FROM customers; to get the basic data.
3
Use COALESCE to handle NULL phone numbers
Modify the SELECT query to use COALESCE on the phone column. Replace phone with COALESCE(phone, 'No Phone Provided') to show 'No Phone Provided' when phone is NULL.
PostgreSQL
Hint
Use COALESCE(phone, 'No Phone Provided') in the SELECT list.
4
Alias the replaced phone column as contact_phone
Add an alias to the COALESCE expression so the column name in the result is contact_phone. Use AS contact_phone after the COALESCE expression.
PostgreSQL
Hint
Use AS contact_phone to rename the column.
Practice
(1/5)
1. What does the COALESCE function do in PostgreSQL?
easy
A. Returns the first non-NULL value from the list of arguments
B. Returns the last value in the list regardless of NULL
C. Counts the number of NULL values in a column
D. Deletes rows with NULL values
Solution
Step 1: Understand COALESCE purpose
COALESCE checks each argument in order and returns the first one that is not NULL.
Step 2: Compare options with definition
Only Returns the first non-NULL value from the list of arguments correctly describes this behavior; others describe unrelated functions.
Final Answer:
Returns the first non-NULL value from the list of arguments -> Option A
Quick Check:
COALESCE = first non-NULL value [OK]
Hint: COALESCE picks first non-NULL value in order [OK]
Common Mistakes:
Thinking COALESCE counts NULLs
Assuming it returns last value
Confusing with DELETE or COUNT functions
2. Which of the following is the correct syntax to use COALESCE in PostgreSQL?
easy
A. COALESCE(value1; value2; ...; valueN)
B. COALESCE[value1, value2, ..., valueN]
C. COALESCE(value1, value2, ..., valueN)
D. COALESCE{value1, value2, ..., valueN}
Solution
Step 1: Recall function call syntax in SQL
Functions use parentheses with comma-separated arguments.
Step 2: Match options to syntax rules
Only COALESCE(value1, value2, ..., valueN) uses parentheses and commas correctly; others use invalid separators or brackets.
Final Answer:
COALESCE(value1, value2, ..., valueN) -> Option C
Quick Check:
Function syntax = parentheses + commas [OK]
Hint: Use parentheses and commas for SQL functions [OK]
Common Mistakes:
Using semicolons instead of commas
Using square or curly brackets
Omitting parentheses
3. Given the table users with columns id, nickname, and fullname, what will this query return?
SELECT id, COALESCE(nickname, fullname, 'Anonymous') AS display_name FROM users;
Assuming a row has nickname = NULL and fullname = 'John Doe'.
medium
A. NULL
B. 'John Doe'
C. 'Anonymous'
D. nickname value
Solution
Step 1: Evaluate COALESCE arguments in order
First argument is nickname which is NULL, so move to next.
Step 2: Check fullname value
fullname is 'John Doe', which is not NULL, so COALESCE returns 'John Doe'.
Final Answer:
'John Doe' -> Option B
Quick Check:
First non-NULL = 'John Doe' [OK]
Hint: COALESCE returns first non-NULL value in order [OK]
Common Mistakes:
Assuming NULL is returned if first value is NULL
Ignoring order of arguments
Choosing default 'Anonymous' incorrectly
4. Identify the error in this query:
SELECT id, COALESCE nickname, fullname, 'Guest' AS display_name FROM users;
medium
A. AS keyword misplaced
B. Wrong keyword instead of COALESCE
C. No error, query is correct
D. Missing parentheses around COALESCE arguments
Solution
Step 1: Check COALESCE function syntax
COALESCE requires parentheses around its arguments.
Step 2: Analyze given query
Query uses COALESCE without parentheses, causing syntax error.
Final Answer:
Missing parentheses around COALESCE arguments -> Option D
Quick Check:
Functions need parentheses [OK]
Hint: Always use parentheses with COALESCE arguments [OK]
Common Mistakes:
Omitting parentheses for functions
Misplacing AS keyword
Confusing COALESCE with other functions
5. You want to display a user's preferred contact method from columns email, phone, and address. If all are NULL, show 'No contact info'. Which query correctly uses COALESCE to achieve this?
hard
A. SELECT id, COALESCE(email, phone, address, 'No contact info') AS contact FROM users;
B. SELECT id, COALESCE(email, phone, address) OR 'No contact info' AS contact FROM users;
C. SELECT id, COALESCE(email, phone, address) ?? 'No contact info' AS contact FROM users;
D. SELECT id, COALESCE(email, phone, address, NULL) AS contact FROM users;
Solution
Step 1: Understand COALESCE usage for defaults
COALESCE returns first non-NULL; adding 'No contact info' as last argument provides default.
Step 2: Evaluate each option's correctness
SELECT id, COALESCE(email, phone, address, 'No contact info') AS contact FROM users; correctly lists all contact columns plus default string. Options B and C use invalid operators, D ends with NULL, no default.
Final Answer:
SELECT id, COALESCE(email, phone, address, 'No contact info') AS contact FROM users; -> Option A
Quick Check:
Default last in COALESCE list [OK]
Hint: Put default last inside COALESCE parentheses [OK]
Common Mistakes:
Using OR or ?? instead of COALESCE
Not providing a default value
Ending COALESCE with NULL instead of default
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.