Percentiles

SQL · Extra Concepts

Theory

A percentile is the value below which a given percentage of the data falls. The 50th percentile is the median: half of the salaries are below it and half above. The 90th percentile of delivery time tells you that 90% of orders arrived faster than that value. Percentiles describe a distribution far better than the average when data is skewed, because one huge salary pulls the average up but barely moves the median.

Standard SQL offers two ordered-set aggregates. PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) is continuous: with an even number of rows it interpolates between the two middle values. PERCENTILE_DISC(0.5) returns an actual value from the data. PostgreSQL, Oracle and Snowflake support both as aggregates; SQL Server supports them as window functions with OVER; SQLite and MySQL lack them, so you sort the values and pick the middle one(s) with LIMIT/OFFSET, averaging the two middle values for an even count.

Why we use it

Averages are distorted by outliers and hide how values are spread. Percentiles show typical and extreme values (median, 90th, 99th) that describe salaries, response times and order sizes honestly.

When to use it

Use the median for 'typical' values in skewed data and high percentiles (p90, p95) for service levels and outliers. Use PERCENTILE_DISC when the answer must be a real value; use AVG only when data is roughly symmetric.

How it works

  1. Pick the percentile p, e.g. 0.5 for the median
  2. Sort the values: ORDER BY salary
  3. Find position p * (n - 1) in the sorted list
  4. Position between two rows? Yes: CONT interpolates; DISC picks a real value. No: Return that value.
  5. Percentile value for the group

Syntax

PERCENTILE_CONT(p) WITHIN GROUP (ORDER BY column)

Example

SELECT PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY salary) AS median_salary
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic