0
0
PostgreSQLquery~20 mins

COALESCE for NULL handling in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
COALESCE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of COALESCE with multiple NULLs
Given the table employees with columns id, name, and bonus where some bonus values are NULL, what is the output of this query?
SELECT id, COALESCE(bonus, 1000) AS bonus_amount FROM employees ORDER BY id;
PostgreSQL
CREATE TABLE employees (id INT, name TEXT, bonus INT);
INSERT INTO employees VALUES (1, 'Alice', NULL), (2, 'Bob', 500), (3, 'Charlie', NULL);
A[{"id":1,"bonus_amount":1000},{"id":2,"bonus_amount":500},{"id":3,"bonus_amount":1000}]
B[{"id":1,"bonus_amount":NULL},{"id":2,"bonus_amount":500},{"id":3,"bonus_amount":NULL}]
C[{"id":1,"bonus_amount":0},{"id":2,"bonus_amount":500},{"id":3,"bonus_amount":0}]
D[{"id":1,"bonus_amount":1000},{"id":2,"bonus_amount":NULL},{"id":3,"bonus_amount":1000}]
Attempts:
2 left
💡 Hint
COALESCE returns the first non-NULL value from its arguments.
🧠 Conceptual
intermediate
1:30remaining
Understanding COALESCE with multiple arguments
What will be the result of this expression in PostgreSQL?
SELECT COALESCE(NULL, NULL, 'first non-null', 'second non-null');
A'first non-null'
BNULL
CSyntax error
D'second non-null'
Attempts:
2 left
💡 Hint
COALESCE returns the first argument that is not NULL.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in COALESCE usage
Which option contains a syntax error when using COALESCE in PostgreSQL?
ASELECT COALESCE(5, NULL, 10);
BSELECT COALESCE(NULL 5, 10);
CSELECT COALESCE(NULL, 5, 10);
DSELECT COALESCE(NULL, NULL, 10);
Attempts:
2 left
💡 Hint
Check for missing commas between arguments.
optimization
advanced
2:30remaining
Optimizing COALESCE with subqueries
Consider this query:
SELECT id, COALESCE((SELECT bonus FROM bonuses WHERE bonuses.emp_id = employees.id), 0) AS bonus_amount FROM employees;

Which option best improves performance without changing the output?
AUse COALESCE with a CASE statement:<br>SELECT id, COALESCE(CASE WHEN bonus IS NULL THEN 0 ELSE bonus END, 0) AS bonus_amount FROM employees;
BReplace COALESCE with IFNULL:<br>SELECT id, IFNULL((SELECT bonus FROM bonuses WHERE bonuses.emp_id = employees.id), 0) AS bonus_amount FROM employees;
CUse LEFT JOIN instead of subquery:<br>SELECT e.id, COALESCE(b.bonus, 0) AS bonus_amount FROM employees e LEFT JOIN bonuses b ON b.emp_id = e.id;
DAdd WHERE bonus IS NOT NULL in subquery:<br>SELECT id, COALESCE((SELECT bonus FROM bonuses WHERE bonuses.emp_id = employees.id AND bonus IS NOT NULL), 0) AS bonus_amount FROM employees;
Attempts:
2 left
💡 Hint
Replacing subqueries with joins often improves query speed.
🔧 Debug
expert
3:00remaining
Debugging unexpected NULL results with COALESCE
A developer runs this query:
SELECT id, COALESCE(bonus, 0) FROM employees;

But some rows still show NULL for the bonus column. Which option explains the most likely cause?
AThe employees table has no rows, so the query returns NULL.
BThe COALESCE function is case-sensitive and should be written as coalesce.
CThe query is missing an alias for the COALESCE column, causing NULL display.
DThe bonus column is of type JSON and contains JSON null, which COALESCE does not treat as SQL NULL.
Attempts:
2 left
💡 Hint
COALESCE only replaces SQL NULL, not other types of null-like values.