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
| id | name | department |
|---|---|---|
| 1 | a | Eng |
| 2 | b | Eng |
| 3 | c | Eng |
| 4 | d | Sales |
| 5 | e | Sales |
| 6 | f | HR |
| 7 | g | HR |
| 8 | h | HR |
Expected output
| department | headcount |
|---|---|
| Eng | 3 |
| HR | 3 |
Hints
Hint 1
Aggregate first withgroupBy().agg(F.count("*").alias("headcount")).Hint 2
SQL's HAVING is just afilter applied after the aggregation.Learn the concepts
- groupBy + agg · 4 min read. Aggregate rows per group: counts, sums, averages, and how count(*) differs from count(col).
- filter / where · 5 min read. Keep the rows that match a condition, and the null and operator traps that silently drop data.
PySpark functions you'll practise
- groupBy
- agg
- count
- filter
Related problems
- Average Salary by Department · Easy · Aggregations
- Find Repeat Customers · Easy · Aggregations, Filtering & Selection
- Entry and Exit Pages (MAX_BY / MIN_BY) · Medium · Aggregations
- New Users per Day · Medium · Aggregations
- Conditional Aggregation · Medium · Aggregations, Conditional Logic
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