HR: Average Salary by Department

SQL coding challenge · Difficulty: easy · Topic: Aggregation · +50 XP

Problem

The compensation committee is reviewing salary equity across departments. Calculate the average salary for each department.

Tables

Table: Employees

| emp_id | name | department | salary | hire_date |
| --- | --- | --- | --- | --- |
| 1 | Ashna | HR | 60000 | 2022-01-15 |
| 2 | Pinki | IT | 70000 | 2021-06-20 |
| 3 | Shruti | Finance | 65000 | 2020-09-10 |
| 4 | Parvati | Marketing | 62000 | 2023-03-05 |

Table: Performance

| perf_id | emp_id | rating | review_date |
| --- | --- | --- | --- |
| 101 | 1 | 4 | 2023-06-30 |
| 102 | 2 | 5 | 2023-06-30 |
| 103 | 3 | 3 | 2023-06-30 |
| 104 | 4 | 4 | 2023-06-30 |
| 105 | 1 | 5 | 2024-06-30 |
| 106 | 2 | 4 | 2024-06-30 |
| 107 | 3 | 4 | 2024-06-30 |
| 108 | 4 | 5 | 2024-06-30 |

Expected Output

| department | avg_salary |
| --- | --- |
| IT | 70000.0000 |
| Finance | 65000.0000 |
| Marketing | 62000.0000 |
| HR | 60000.0000 |

What this SQL challenge teaches you

“HR: Average Salary by Department” is a easy-level SQL challenge focused on Aggregation. Working through it gives you hands-on practice with AVG, ROUND, GROUP BY, ORDER BY — 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, avg_salary (rounded to 2 decimal places).
  2. Order the output avg_salary descending.
  3. Compare your output to the Expected Output — the columns, values and row order must match exactly.

Where this comes up

Variations of this problem have been reported in interviews at Infosys, TCS, Microsoft. 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 easy and covers Aggregation.

Solve this challenge free on PySpark.in