1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. What does COALESCE(phone, email, 'unknown') return for a…

Added 30 Nov -0001

What does COALESCE(phone, email, 'unknown') return for a row where phone is NULL and email is 'a@b.com'?

  1. NULL, because the first argument is NULL
  2. the last argument, unknown
  3. a comma-separated list of all its arguments
  4. the first non-NULL argument, a@b.com Correct answer

Correct answer

D. the first non-NULL argument, a@b.com

17 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More SQL for Data Analysis MCQs

Practise all SQL for Data Analysis MCQs

Similar MCQs