Skip to content

11. Average Salary by Department

Difficulty: Easy · Topics: Aggregations

For each department in employees, return:

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

employees

idnamedepartmentsalary
1AshaEngineering72000
2BenSales48000
3ChitraEngineering95000
4DevHR51000
5EshaSales52000
6FarahEngineering88001

Expected output

departmentavg_salaryheadcount
Engineering85000.333
Sales500002
HR510001

Hints

Hint 1groupBy("department").agg(...) accepts several aggregations at once.
Hint 2Name the outputs with .alias().
Hint 3F.count("*") counts rows; F.count("salary") would skip nulls.

Learn the concepts

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance

Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice