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

Last updated 13 July 2026

  • Averaging averages (AVG(group_avg)) weights every group equally regardless of size. Compute SUM(x) / SUM(n) or a weighted average instead.

  • A one-to-many join fans out rows, inflating SUM and COUNT(*). COUNT(DISTINCT) hides this for counts but leaves sums and averages wrong.

  • Guard by pre-aggregating each table to the correct grain (e.g. in a CTE) before joining, checking join-key uniqueness, and comparing row counts before and after the join.

  • https://medium.com/@sendoamoronta/the-hard-parts-of-dimensional-modeling-76048ae8c35a
  • https://www.varsitytutors.com/practice/subjects/sql/lessons/defining-and-implementing-metrics
  • https://skills.rest/skill/data-validation-monthop-gmail
  • https://dev.to/code_with_mwai/what-6000-pharmacy-transactions-taught-me-about-discounts-187h
  • https://www.wickedsmartdata.com/articles/combining-group-by-with-multiple-joins-aggregating-across-three-or-more-related-tables
  • https://www.varsitytutors.com/practice/subjects/sql/lessons/avoiding-join-mistakes
All Advanced SQL interview questions