Skip to content

12. Departments with Large Teams

Difficulty: Easy · Topics: Aggregations, Filtering & Selection

Return every department in employees that has at least 3 employees, along with its headcount.

Columns: department, headcount.

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

Sample data

employees

idnamedepartment
1aEng
2bEng
3cEng
4dSales
5eSales
6fHR
7gHR
8hHR

Expected output

departmentheadcount
Eng3
HR3

Hints

Hint 1Aggregate first with groupBy().agg(F.count("*").alias("headcount")).
Hint 2SQL's HAVING is just a filter applied after the aggregation.

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