A developer is writing a Spark SQL query that must handle null values in a column named discount. The requirement is to replace null discounts with 0.0, but also to replace any negative discount values with 0.0, leaving positive values unchanged. Which expression should be used?
GREATEST(discount, 0.0) turns negative values into 0.0 but returns null if discount is null. Wrapping it with COALESCE(..., 0.0) replaces that null with 0.0. This combination satisfies both conditions: nulls become 0.0, negatives become 0.0, and positive values are preserved. It is the correct nested expression.
Why this answer
The requirement is to treat both nulls and negatives as zero. GREATEST(discount, 0.0) ensures negatives become 0.0 but yields null for null input. COALESCE then replaces that null with 0.0.
The nested expression COALESCE(GREATEST(discount, 0.0), 0.0) correctly handles both cases and leaves positive values untouched. Using COALESCE alone misses negatives, GREATEST alone misses nulls, and the IF expression misses nulls.
Exam trap
The trap here is forgetting that GREATEST returns null when any argument is null, so a separate COALESCE is needed to handle null inputs.