Median Salary by Department
Problem
**[Classic — asked at multiple companies]**
Calculate the **median salary** per department. SQLite does not have a
built-in median function — use ROW_NUMBER + COUNT trick:
For even counts, average the two middle rows. For odd counts, return the
single middle row.
Return `dept` and `median_salary`, ordered by dept.
Tables
Example rows — the live problem includes the full dataset.
employees
| id | dept | salary |
|---|
Expected output
Your answer should return 2 rows with the columns dept, median_salary.
Starter code (SQL)
SELECT *
FROM employees;Solve this SQL question free
Write SQL and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock