What does COALESCE(phone, email, 'unknown') return for a row where phone is NULL and email is 'a@b.com'?
Correct answer
D. the first non-NULL argument, a@b.com
17 views
D. the first non-NULL argument, a@b.com
17 views
COALESCE walks its arguments left to right and returns the first one that is not NULL, so with phone missing it moves on to email and returns that address. It is the portable fallback idiom and can take any number of arguments, unlike IFNULL, which some databases limit to two. A related function, NULLIF, returns NULL when two expressions are equal and is often used to turn a sentinel value into a true NULL.
Asked in: NTS, PPSC IT, Interviews
Not sure why the answer is D? Ask here and get an explanation.