Lesson 3 of 4

Progress: 0/3 exercises solved (0%)

← Previous

Solve all exercises below to unlock the next lesson.

NTILE and PERCENT_RANK — statistical ranking

Lesson Description

Use analytical SQL to rank, compare, and calculate trends across partitions without losing row-level detail.

Easy Project

Mini project: Build a compensation ranking dashboard query with department-level rank and delta from the top earner.

These window functions assign relative positions to rows within an ordered set.

NTILE(n) — divide rows into n equal buckets

Assigns each row a bucket number from 1 to n. Useful for quartiles, deciles, percentiles.

SELECT
  first_name, salary,
  NTILE(4) OVER (ORDER BY salary ASC) AS quartile
FROM employees;
  • Quartile 1 = lowest salaries, 4 = highest.
  • If rows don't divide evenly, earlier buckets get one extra row.
  • Partition to rank within a group: NTILE(4) OVER (PARTITION BY department_id ORDER BY salary).

PERCENT_RANK() — relative rank as 0.0 → 1.0

PERCENT_RANK = (rank - 1) / (total rows - 1)
SELECT
  first_name, salary,
  ROUND(PERCENT_RANK() OVER (ORDER BY salary) * 100, 1) AS pct_rank
FROM employees;

A value of 0.90 means the employee earns more than 90% of colleagues.

CUME_DIST() — cumulative distribution

Similar to PERCENT_RANK but counts the current row in the numerator:

SELECT
  first_name, salary,
  ROUND(CUME_DIST() OVER (ORDER BY salary) * 100, 1) AS cume_pct
FROM employees;

CUME_DIST is always > 0; PERCENT_RANK of the lowest row is always 0.

Practical pattern: find top quartile

WITH quartiled AS (
  SELECT first_name, salary,
    NTILE(4) OVER (ORDER BY salary ASC) AS quartile
  FROM employees
)
SELECT first_name, salary
FROM quartiled
WHERE quartile = 4
ORDER BY salary DESC;

Tips

  • Use NTILE for segmentation (top/middle/bottom); use PERCENT_RANK for exact relative positioning.
  • NTILE requires an ORDER BY inside OVER().
  • For business reporting: NTILE(100) gives percentiles (1–100).

Learning context

This lesson is part of Window Functions. Focus on the core idea in NTILE and PERCENT_RANK — statistical ranking, then validate with deliberate practice.

What to master

  • Understand clause order and intent
  • Write deterministic result queries
  • Use clear aliases and readable structure

Common mistakes

  • Skipping ORDER BY in final outputs
  • Using joins before validating base rows
  • Not checking row counts after changes

High-level strategy

  • Start simple, then layer complexity
  • Run often and compare expected shape
  • Refactor with CTEs when logic grows

Task ladder

  1. Baseline query: get a correct first output.
  2. Add one complication: edge case, join, filter, or ranking rule.
  3. Re-run and refine until output is stable and explainable.

Transparent data checks

  • Check row count before and after each major clause.
  • Validate one manual sample row for correctness.
  • Confirm final ordering and column naming for trust.

Retention loop

  • Run the solution once.
  • Rewrite from memory with one variation.
  • Explain the logic in plain English in one sentence.

Logical reasoning for commands

WHERE

Why: Limits rows to only the business-relevant subset.

Memory cue: Filter early to reduce noise.

ORDER BY

Why: Makes output deterministic and reviewable.

Memory cue: No ORDER BY means no guaranteed row order.

AS alias

Why: Makes output columns readable for teams and reports.

Memory cue: If the name is clear, the query is easier to trust.

Concept check

Quick check: NTILE and PERCENT_RANK — statistical ranking

3 quick questions. One at a time. Instant score at the end.

Exercise 1

Assign employees to salary quartiles

Enterprise Workforce Analyticseasy

Return first_name, last_name, salary, and quartile (1–4) using NTILE(4) ordered by salary ascending. Quartile 1 = lowest earners. Order by quartile, then salary.

Run a query to see results here.
Exercise 2

Salary percentile rank

Enterprise Workforce Analyticseasy

Return first_name, last_name, salary, and pct_rank — the PERCENT_RANK() (multiplied by 100 and rounded to 2 decimal places) of each employee ordered by salary. Order by salary.

Run a query to see results here.
Exercise 3

Employees in the top salary quartile

Enterprise Workforce Analyticseasy

Using a CTE with NTILE(4), return only the employees in the top salary quartile (quartile = 4). Show first_name, last_name, and salary, ordered by salary descending.

Run a query to see results here.

Lesson 3 of 4

0/3 solved (0%)

← Previous

Finish this lesson to unlock next.