1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. An expression computes revenue / items and items may be…

Added 30 Nov -0001

An expression computes revenue / items and items may be zero. Which construct prevents a division-by-zero error, and how?

  1. NULLIF(items, 0), which returns NULL for a zero divisor so the result becomes NULL Correct answer
  2. COALESCE(items, 0), which substitutes a zero divisor so that the division stays exact
  3. NULLIF(items, 0), which replaces NULL divisors with zero and skips those rows
  4. COALESCE(items, 0), which converts a zero divisor into a NULL before dividing

Correct answer

A. NULLIF(items, 0), which returns NULL for a zero divisor so the result becomes NULL

18 views

MCQSEXAM

Explanation

NULLIF(a, b) returns NULL when a equals b, so NULLIF(items, 0) turns a zero divisor into NULL and the division yields NULL instead of raising a division-by-zero error. COALESCE does the opposite job, returning the first non-null argument and defaulting missing values. Combining them, revenue / NULLIF(COALESCE(items, 0), 0), is the usual defensive idiom.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? 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