SQL interview question

Data ScienceSQLProduct Metricseasy

In PostgreSQL, converted is an INTEGER column holding 0 or 1. This daily conversion-rate query shows 0 for almost every day, and 1 only on days where every trial converted. Why?

sql
SELECT signup_date,
       SUM(converted) / COUNT(*) AS conversion_rate
FROM trials
GROUP BY signup_date;
  1. ACOUNT(*) also counts rows where converted is NULL, which inflates the denominator so much that the ratio falls to zero; use COUNT(converted) instead.
  2. BSUM over many 0/1 values overflows the INTEGER type on busy days and wraps around to 0; casting the column to BIGINT before summing avoids the overflow.
  3. CPostgreSQL rounds the ratio to the nearest integer, so days below 50% conversion show 0; wrapping the expression in ROUND(..., 2) keeps the decimals.
  4. DBoth SUM(converted) and COUNT() are integers, so / performs integer division and truncates; cast one side, e.g. SUM(converted)::numeric / COUNT().
Show the answer and explanation

Correct answer: D, Both SUM(converted) and COUNT() are integers, so / performs integer division and truncates; cast one side, e.g. SUM(converted)::numeric / COUNT().

Explanation SQL

In PostgreSQL (and SQL Server, and many other engines) integer / integer = integer, truncated toward zero. SUM(converted) returns bigint and COUNT(*) returns bigint, so 37 / 120 = 0. Only when every trial converts do you get n / n = 1.

Fixes:

SUM(converted)::numeric / COUNT(*)          -- cast one operand
AVG(converted::numeric)                     -- mean of a 0/1 flag = rate
SUM(converted) * 1.0 / NULLIF(COUNT(*), 0)  -- also guards against / 0

Dialect differences matter:

Engine7 / 2
PostgreSQL, SQL Server3 (integer division)
BigQuery3.5 (INT64 / INT64 returns FLOAT64; use DIV(7, 2) for integer division)
Python 33.5 (// is floor division)

In BigQuery you would typically write SAFE_DIVIDE(COUNTIF(converted = 1), COUNT(*)), which returns NULL instead of failing on division by zero.

Why the distractors are wrong: PostgreSQL truncates, it does not round, and ROUND applied after the division cannot recover lost decimals. Summing 0/1 flags cannot overflow: SUM of an INTEGER column already returns bigint, and PostgreSQL raises an error on overflow instead of wrapping around. NULLs in the denominator could bias the rate a little, but they cannot produce exactly 0 on nearly every day.

Interview tip: when a ratio metric looks suspiciously like 0 or 1, check integer division first; mention NULLIF or SAFE_DIVIDE for empty groups.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Data Science questions

  1. A retailer's marketing mix model (MMM) transforms weekly TV spend with geometric adstock aₜ = xₜ + λ·aₜ₋₁, with λ = 0.6 and a₀ = 0, and then passes the…
  2. A telecom's plan names are s = pd.Series(["Max+ 10GB", "Max 20GB", None, "Mini 5GB"]). What do print(s.str.contains("Max+", na=False).sum()) and…
  3. A retailer exports product reviews to CSV with the columns review_id, product_id, rating, review_text, created_at and loads them into a staging table that…
  4. A loyalty system stores points as pts = np.array([1201, 350, 85], dtype=np.int64). For a 1.5× points campaign, a notebook runs boosted = pts 1.5 and then pts…
  5. A bank automatically offers a premium card to customers with an internal score of 700 or higher. You compare customers just above and just below 700 to…