Median Salary per Department

SQL coding challenge · Difficulty: hard · Topic: Window Functions · +180 XP

Table: staff

+------------+---------+

| Column     | Type    |

+------------+---------+

| id         | INT     |
| name       | VARCHAR |
| department | VARCHAR |
| salary     | INT     |

+------------+---------+

Problem

-------

Find the median salary for each department.

Median rules:

Return: department, median_salary

Order: department ASC

Note on the result type

-----------------------

Take the average of the middle row(s) with AVG(). On an INT salary column

MySQL returns a DECIMAL with four decimal places, so 85000 is shown below as

85000.0000. Rounding is accepted too - the grader compares the values, not

the trailing zeros.

Example Input (table: staff)

----------------------------

+----+-------+-------------+--------+

| id | name  | department  | salary |

+----+-------+-------------+--------+

|  1 | Alice | Engineering |  95000 |
|  2 | Bob   | Engineering |  85000 |
|  3 | Carol | Engineering |  75000 |
|  4 | Dave  | Marketing   |  65000 |
|  5 | Eve   | Marketing   |  70000 |
|  6 | Frank | HR          |  55000 |
|  7 | Grace | HR          |  60000 |
|  8 | Hank  | HR          |  62000 |

+----+-------+-------------+--------+

Expected Output

---------------

+-------------+---------------+

| department  | median_salary |

+-------------+---------------+

| Engineering |  85000.0000   |
| HR          |  60000.0000   |
| Marketing   |  67500.0000   |

+-------------+---------------+

Explanation

-----------

Engineering (sorted): 75000, 85000, 95000

-> odd count (3): the middle value = 85000

HR (sorted): 55000, 60000, 62000

-> odd count (3): the middle value = 60000

Marketing (sorted): 65000, 70000

-> even count (2): (65000 + 70000) / 2 = 67500

What this SQL challenge teaches you

“Median Salary per Department” is a hard-level SQL challenge focused on Window Functions. Working through it gives you hands-on practice with PERCENTILE_CONT, window, statistics — the kind of transformation you are asked to write in real data engineering work and in technical interviews. You can solve it directly in the browser: the dataset is pre-loaded, so you write the query or DataFrame code, run it, and compare your output against the expected result immediately.

Concepts covered

How to approach it

If you get stuck, work through these steps in order before looking at a full solution — each one narrows the problem down:

  1. Your result should return: department, median_salary.
  2. Compare your output to the Expected Output — the columns, values and row order must match exactly.
  3. MySQL has no MEDIAN() and no PERCENTILE_CONT(). Build it from window functions instead: ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary) to number the rows, COUNT(*) OVER (PARTITION BY department) to get the size of each group, then keep the middle row(s) and AVG() them.

Where this comes up

Variations of this problem have been reported in interviews at Microsoft, Oracle, Accenture. Interviewers use it to check whether you can express the logic cleanly and reason about correctness on edge cases such as ties, nulls and empty groups.

How to practise it on PySpark.in

Open the challenge, write your SQL query in the editor and press Run to execute it against the sample dataset. Submitting checks your output against every test case, including hidden ones, so you find out straight away whether your logic holds up. You can retry as often as you like, and each solved challenge adds to your XP.

Related SQL challenges

Frequently asked questions

Do I need to install Spark or a database to solve this?

No. The SQL environment runs in your browser with the sample data already loaded, so there is nothing to install or configure.

Is this challenge free?

Yes - the problem, the sample dataset, the hints and unlimited test runs are free.

What level is it?

It is rated hard and covers Window Functions.

Solve this challenge free on PySpark.in