Courseiva
Using Spark SQL →hardMultiple Choice

Databricks-Spark-Assoc Using Spark SQL Practice Question

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?

⚠ Common 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.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

COALESCE(GREATEST(discount, 0.0), 0.0)

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.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✓

    COALESCE(GREATEST(discount, 0.0), 0.0)

    Why this is correct

    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.

  • ✗

    IF(discount < 0, 0.0, discount)

    Why it's wrong here

    This conditional replaces negative values with 0.0, but if discount is null, the condition discount < 0 evaluates to null, which is not true, so the ELSE branch returns null. Nulls are not replaced with 0.0. Therefore, this expression fails to handle null values as required. It would need an additional null check.

  • ✗

    GREATEST(discount, 0.0)

    Why it's wrong here

    GREATEST returns the largest of its arguments, which would convert negative values to 0.0 but leaves nulls as null because GREATEST returns null if any argument is null. The requirement includes replacing nulls with 0.0. Thus, GREATEST alone fails to handle nulls and does not meet the full requirement.

  • ✗

    COALESCE(discount, 0.0)

    Why it's wrong here

    COALESCE only replaces nulls with the first non-null argument. It does not affect negative values, so a discount of -5.0 would remain -5.0. The requirement explicitly states that negative values must also be set to 0.0. Therefore, COALESCE alone is insufficient and would leave invalid negative discounts in the result.

About these practice questions

One of 295 original Databricks-Spark-Assoc practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Databricks exam blueprint

This Databricks-Spark-Assoc practice question is part of Courseiva's free Databricks certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the Databricks-Spark-Assoc exam.