IntermediateWindow Functions

Compute quartiles with NTILE

The query

SQL
SELECT
  first_name,
  last_name,
  dept_id,
  salary,
  NTILE(4) OVER (PARTITION BY dept_id ORDER BY salary) AS quartile,
  CASE NTILE(4) OVER (PARTITION BY dept_id ORDER BY salary)
    WHEN 1 THEN 'Bottom 25%'
    WHEN 2 THEN '25-50%'
    WHEN 3 THEN '50-75%'
    WHEN 4 THEN 'Top 25%'
  END AS salary_quartile_label
FROM employees
WHERE status = 'active'
ORDER BY dept_id, salary;
Tested against PostgreSQL 16

Note

NTILE per department shows relative salary position within each dept, not company-wide.

Tables referenced