Median Salary by Department using PERCENTILE_CONT
hard
PERCENTILE_CONT
ordered-set aggregate
window functions
GROUP BY
statistics
Google
Meta
LinkedIn
Queries run in DuckDB in your browser. Syntax is close to Postgres; some MySQL/SQL Server functions will not work here.
An employee_salaries table stores salaries by department. For each department, compute the median salary using PERCENTILE_CONT(0.5), the mean salary (rounded to nearest integer), and the min/max salary range.
Table: employee_salaries
| Column | Type |
|---|---|
| department | VARCHAR |
| employee_id | INT |
| salary | INT |
Output columns: department, median_salary, avg_salary (rounded), min_salary, max_salary, employee_count. Order by department.
Schema
employee_salaries
department VARCHARemployee_id INTsalary INT
| department | employee_id | salary |
|---|---|---|
| Engineering | 1 | 95000 |
| Engineering | 2 | 105000 |
| Engineering | 3 | 115000 |
| Engineering | 4 | 88000 |
| Engineering | 5 | 132000 |
| Engineering | 6 | 78000 |
| Sales | 7 | 65000 |
| Sales | 8 | 72000 |
| Sales | 9 | 68000 |
| Sales | 10 | 85000 |
| Sales | 11 | 75000 |
| HR | 12 | 60000 |
| HR | 13 | 55000 |
| HR | 14 | 63000 |
| HR | 15 | 58000 |
SQL editor · DuckDB
Results
Write a query and press Run. DuckDB executes it in your browser.