CompTIA Data+ (DA0-002) free question 4 / 10 · Domain 2: Data acquisition and preparation
A customer report should show the nickname when it exists, otherwise the first name, and the text Unknown when both are missing. Which expression builds that value in one step?
- CASE nickname WHEN NULL THEN first_name END, which tests the nickname against null
- NULLIF(nickname, first_name), which returns null when the two columns are equal
- COALESCE(nickname, first_name, 'Unknown'), which returns the first argument that is not null
- GREATEST(nickname, first_name, 'Unknown'), which returns the largest value in the list
Show the answer and explanation
Answer: C
Why this is correct
The manual says the COALESCE function returns the first of its arguments that is not null, and that null is returned only if all arguments are null. It is often used to substitute a default value for null values when data is retrieved for display. Listing the nickname, then the first name, then the text Unknown gives the fallback chain the report needs.
Why the other options are wrong
"NULLIF(nickname, first_name), which returns null when the two columns are equal" … NULLIF compares two values and returns null if they are equal. It does not work through a list of fallbacks.
"GREATEST(nickname, first_name, 'Unknown'), which returns the largest value in the list" … GREATEST picks the largest of the values and ignores nulls. It chooses by size, not by order of preference.
"CASE nickname WHEN NULL THEN first_name END, which tests the nickname against null" … A simple CASE compares with equality, and a comparison with null is never true. The branch would not run, and the result would stay null.
Key points
COALESCE is the standard tool for defaults. It evaluates arguments from left to right and stops at the first value that is not null.
Go to the CompTIA Data+ (DA0-002) practice app (10 free questions; purchase unlocks all questions)