SQL interview question

Data ScienceSQLDescriptive Statisticsmedium

An online grocer's eight baskets in a pilot region have values 12, 15, 18, 22, 30, 41, 55 and 1,007. In PostgreSQL, what do PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY v), PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY v) and AVG(v) return, and which best describes a typical basket?

  1. A22, 26 and 150: CONT returns the lower middle value as a real data point, DISC averages the two middle values, and the median describes a typical basket better.
  2. B26, 22 and 150: CONT interpolates between the 4th and 5th values, DISC returns the first value whose cumulative share reaches 0.5, and the median describes a typical basket.
  3. C26, 30 and 150: CONT interpolates between the two middle values, DISC returns the first value that lies strictly above the 50% point, and the median describes a typical basket.
  4. D26, 30 and 150: CONT interpolates, DISC returns the upper of the two middle values, and the mean of 150 describes a typical basket best because it uses every value, including the 1,007.
Show the answer and explanation

Correct answer: B, 26, 22 and 150: CONT interpolates between the 4th and 5th values, DISC returns the first value whose cumulative share reaches 0.5, and the median describes a typical basket.

Explanation SQL

Both are ordered-set aggregates, but they differ when the percentile falls between two rows:

  • PERCENTILE_CONT(0.5) treats the data as continuous and interpolates. With 8 values the median lies halfway between the 4th (22) and 5th (30) values: (22 + 30) / 2 = 26.
  • PERCENTILE_DISC(0.5) returns an actual input value: the first one whose cumulative share reaches 0.5. The 4th of 8 values has a cumulative share of 4/8 = 0.5, so it returns 22.
  • AVG = (12 + 15 + 18 + 22 + 30 + 41 + 55 + 1,007) / 8 = 1,200 / 8 = 150.

The mean of 150 is larger than seven of the eight baskets, because one large order pulls it up. On right-skewed data such as basket values, income or latency, the median and percentiles such as p90 describe the typical case. The mean is still the right input for totals (revenue = mean × count).

FunctionInterpolates?Can return a value not in the data?
PERCENTILE_CONTyesyes (26 here)
PERCENTILE_DISCnono (22 here)

In BigQuery, PERCENTILE_CONT and PERCENTILE_DISC are analytic functions used with OVER (), and APPROX_QUANTILES(v, 100)[OFFSET(50)] is the usual approximate median for large tables.

Why the other answers fail: swapping CONT and DISC is the classic mix-up. DISC does not move up to the upper middle value (30); it stops at the first value whose cumulative share is at least 0.5. Using every value is exactly why the mean is not typical here.

Interview tip: when reporting skewed metrics, give median and p90 alongside the mean, and say which percentile function you used so others can reproduce the number.

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. What is an A/B test (an online controlled experiment)?
  2. In product analytics, what is meant by a north star metric?
  3. In a query that uses GROUP BY, how does the WHERE clause differ from the HAVING clause?
  4. In Python, what is the main difference between a list and a tuple?
  5. An inventory agent asks an LLM for a JSON reorder decision and passes the fields to a typed helper. This time the model returned units as a string. What…