0
0
SQLquery~5 mins

COALESCE and NULLIF as CASE shortcuts in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the SQL function COALESCE do?
COALESCE returns the first non-NULL value from a list of expressions. It helps to replace NULLs with a default value.
Click to reveal answer
beginner
How does NULLIF work in SQL?
NULLIF compares two expressions and returns NULL if they are equal; otherwise, it returns the first expression. It is useful to avoid certain values by turning them into NULL.
Click to reveal answer
intermediate
How can COALESCE be used as a shortcut for a CASE statement?
Instead of writing a CASE to check for NULL and provide a default, COALESCE can directly return the first non-NULL value, simplifying the query.
Click to reveal answer
intermediate
Give an example of NULLIF replacing a CASE statement.
Instead of CASE WHEN a = b THEN NULL ELSE a END, you can write NULLIF(a, b) which returns NULL if a equals b, else returns a.
Click to reveal answer
beginner
Why use COALESCE and NULLIF instead of CASE statements?
They make queries shorter and easier to read by handling common NULL-related logic without verbose CASE syntax.
Click to reveal answer
What will COALESCE(NULL, NULL, 'apple', 'banana') return?
A'banana'
B'apple'
CNULL
DError
What does NULLIF(5, 5) return?
A5
BError
CNULL
D0
Which SQL expression is equivalent to CASE WHEN col IS NULL THEN 'N/A' ELSE col END?
ACOALESCE(col, 'N/A')
BNULLIF(col, 'N/A')
CCOALESCE('N/A', col)
DNULLIF('N/A', col)
What does NULLIF('test', 'test') return?
A'test'
BEmpty string
CError
DNULL
Why might you choose COALESCE over a CASE statement?
ACOALESCE can handle multiple values more simply
BCOALESCE is slower but more readable
CCASE cannot handle NULL values
DCOALESCE always returns NULL
Explain how COALESCE can simplify handling NULL values compared to CASE statements.
Think about how you pick the first available option in a list.
You got /3 concepts.
    Describe a scenario where NULLIF is useful and how it acts as a shortcut for CASE.
    Consider when you want to treat a certain value as missing.
    You got /3 concepts.