How do you compute a median or any quantile in SQL, and what are the trade-offs of PERCENTILE functions vs NTILE?

Quick Overview

Asks how to calculate a median or any percentile in SQL and compares PERCENTILE_CONT and PERCENTILE_DISC with bucketing rows using NTILE. Tests interpolation, tie and NULL handling, dialect differences, and a fallback for engines without percentile functions.

How do you compute a median or any quantile in SQL, and what are the trade-offs of PERCENTILE functions vs NTILE?

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

How do you compute a median or arbitrary quantile directly in SQL, and what are the trade-offs between using `PERCENTILE` functions versus `NTILE`? For example, you may need the median and the 90th percentile of `amount` in an `orders` table. ```hint Value or label Ask whether you need one cutoff value for the whole group or a bucket label on every row, and check what each function returns. ``` ```hint Try a tiny case Work out the median of 10, 20, 30, 40 by hand, then see what the largest value in the first of two `NTILE` buckets gives you. ``` ### What a Strong Answer Covers - The ordered-set aggregate syntax for the median and other quantiles, and how the continuous and discrete versions differ - How `NTILE` assigns rows to buckets and why its bucket edges miss the true quantile, especially with ties and small or uneven row counts - A fallback for engines that have no percentile function - When each tool fits: a single cutoff value vs a cohort label per row - Practical concerns such as NULLs, dialect differences and cost on large tables ### Follow-up Questions - How would you compute the median per group, and how does the syntax change across engines? - The table has billions of rows. How would you get the 99th percentile without sorting all of them? - How would you give each row its percentile rank, and how does that differ from its `NTILE` bucket?

Overview: Asks how to calculate a median or any percentile in SQL and compares PERCENTILE_CONT and PERCENTILE_DISC with bucketing rows using NTILE. Tests interpolation, tie and NULL handling, dialect differences, and a fallback for engines without percentile functions.

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

How do you compute a median or arbitrary quantile directly in SQL, and what are the trade-offs between using PERCENTILE functions versus NTILE?

For example, you may need the median and the 90th percentile of amount in an orders table.

What a Strong Answer Covers Guidance

  • The ordered-set aggregate syntax for the median and other quantiles, and how the continuous and discrete versions differ
  • How NTILE assigns rows to buckets and why its bucket edges miss the true quantile, especially with ties and small or uneven row counts
  • A fallback for engines that have no percentile function
  • When each tool fits: a single cutoff value vs a cohort label per row
  • Practical concerns such as NULLs, dialect differences and cost on large tables

Follow-up Questions Guidance

  • How would you compute the median per group, and how does the syntax change across engines?
  • The table has billions of rows. How would you get the 99th percentile without sorting all of them?
  • How would you give each row its percentile rank, and how does that differ from its NTILE bucket?