Averaging averages and COUNT(DISTINCT) after a join: why SQL results mislead and how to guard against it

Quick Overview

Asks why averaging group averages and using COUNT(DISTINCT) after a join can give misleading SQL results, and how to prevent both. Tests understanding of group weighting, join fan-out, query grain, and the checks that catch silent aggregation bugs.

Averaging averages and COUNT(DISTINCT) after a join: why SQL results mislead and how to guard against it

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Explain the averaging-averages pitfall and how `COUNT(DISTINCT)` after a join can produce misleading results. How do you guard against these correctness issues? Think of a typical analytics schema, such as an `orders` table with one row per order and an `order_items` table with one row per item. ```hint Compare group sizes Take two groups, one with 2 rows and one with 2,000. Compare the mean of their two group means with the mean over all rows. ``` ```hint Count the joined rows Join an order that has three items to its items. How many times does the order's amount appear in the result, and which aggregates notice? ``` ### What a Strong Answer Covers - Why the mean of group means differs from the overall mean, and when equal weighting is what you want. - How a one-to-many join changes the row count and each aggregate computed over the joined rows. - What `COUNT(DISTINCT)` repairs and what it leaves wrong in the same query. - Query patterns that compute each metric at the correct grain. - Checks that catch these bugs before a number is reported. ### Follow-up Questions - When is an unweighted average of group averages the right metric? - A query joins orders to both an items table and a payments table, each one-to-many. What goes wrong, and how do you write it correctly? - How would you test a metrics pipeline so that fan-out bugs are caught automatically? - How does the same weighting issue show up in a ratio metric such as revenue per user in an A/B test readout?

Overview: Asks why averaging group averages and using COUNT(DISTINCT) after a join can give misleading SQL results, and how to prevent both. Tests understanding of group weighting, join fan-out, query grain, and the checks that catch silent aggregation bugs.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain the averaging-averages pitfall and how COUNT(DISTINCT) after a join can produce misleading results. How do you guard against these correctness issues?

Think of a typical analytics schema, such as an orders table with one row per order and an order_items table with one row per item.

What a Strong Answer Covers Guidance

  • Why the mean of group means differs from the overall mean, and when equal weighting is what you want.
  • How a one-to-many join changes the row count and each aggregate computed over the joined rows.
  • What COUNT(DISTINCT) repairs and what it leaves wrong in the same query.
  • Query patterns that compute each metric at the correct grain.
  • Checks that catch these bugs before a number is reported.

Follow-up Questions Guidance

  • When is an unweighted average of group averages the right metric?
  • A query joins orders to both an items table and a payments table, each one-to-many. What goes wrong, and how do you write it correctly?
  • How would you test a metrics pipeline so that fan-out bugs are caught automatically?
  • How does the same weighting issue show up in a ratio metric such as revenue per user in an A/B test readout?