0
0
PostgreSQLquery~5 mins

COALESCE for NULL handling in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the COALESCE function do in SQL?
COALESCE returns the first non-NULL value from a list of expressions. It helps handle NULL values by providing a fallback value.
Click to reveal answer
beginner
How does COALESCE handle multiple NULL values?
COALESCE checks each value in order and returns the first one that is not NULL. If all values are NULL, it returns NULL.
Click to reveal answer
beginner
Write a COALESCE example to return 'Unknown' if a column 'name' is NULL.
SELECT COALESCE(name, 'Unknown') FROM table_name;
Click to reveal answer
intermediate
Why is COALESCE preferred over CASE WHEN for NULL handling?
COALESCE is simpler and more readable for checking multiple NULL values compared to writing multiple CASE WHEN conditions.
Click to reveal answer
intermediate
Can COALESCE handle different data types in its arguments?
All arguments in COALESCE must be of compatible data types or implicitly convertible to a common type; otherwise, it causes an error.
Click to reveal answer
What will COALESCE(NULL, NULL, 'Hello', 'World') return?
A'Hello'
BNULL
C'World'
DError
If all arguments to COALESCE are NULL, what is the result?
AEmpty string
BError
C0
DNULL
Which SQL function is best for replacing NULL values with a default?
ACOUNT()
BSUM()
CCOALESCE()
DAVG()
What happens if COALESCE arguments have incompatible data types?
AReturns first argument
BRaises an error
CReturns NULL
DConverts all to string
Which of these is a valid COALESCE usage?
ACOALESCE(name, 'No Name')
BCOALESCE()
CCOALESCE(5, 'text')
DCOALESCE(name, 0)
Explain how COALESCE helps handle NULL values in SQL queries.
Think about what happens when a column has missing data.
You got /3 concepts.
    Describe a scenario where using COALESCE improves query results.
    Imagine showing user names but some are missing.
    You got /3 concepts.