Reviewed by Aditya Kumar · Last reviewed 2026-03-24
To calculate the number of employees in each department, you would aggregate the data by the department column and then count the records within each group. This is typically achieved using GROUP BY…
This medium-level General/Other question appears frequently in data engineering interviews at companies like Infosys. While less common, it tests deeper understanding that distinguishes strong candidates. Mastering the underlying concepts (spark, sql, window) will help you answer variations of this question confidently.
Break this problem into components. Identify the core trade-offs involved, then walk the interviewer through your reasoning step by step. Demonstrate awareness of edge cases and production considerations - this is what separates good answers from great ones. The expert answer includes a code example that demonstrates the implementation pattern.
To calculate the number of employees in each department, you would aggregate the data by the department column and then count the records within each group. This is typically achieved using GROUP BY with COUNT(*) in SQL or groupBy().count() in PySpark.
The GROUP BY clause in SQL aggregates rows that share the same value in the specified department column into a single summary row. The COUNT() aggregate function then counts all rows within each of these groups. It's crucial to use COUNT() rather than COUNT(department) because COUNT(*) counts all rows (including those with NULL department values if they exist), whereas COUNT(department) would only count non-NULL values in the department column, potentially misrepresenting the total employee count. In distributed systems like PySpark, groupBy('department').count() performs a similar aggregation, but it involves a data shuffle operation where data with the same department key is moved to the same partition for processing.
Here's the basic SQL query:
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
To calculate the percentage of employees in each department, you can extend this using a window function to get the total employee count: SELECT department, department, COUNT() 100.0 / SUM(COUNT(*)) OVER () AS percentage FROM employees GROUP BY department;.
For performance, in relational databases, an index on the department column can significantly speed up these aggregation queries. In cloud data warehouses like Snowflake, defining department as a clustering key can optimize query performance by enabling micro-partition pruning, reducing the amount of data scanned. For large-scale distributed processing with Spark, the shuffle phase of groupBy can be resource-intensive; optimizing partitioning strategies or pre-aggregating data can mitigate this.
In the interview, also mention data quality considerations (e.g., how to handle NULL or inconsistent department names), the scalability implications for very large datasets, and how this aggregated data might be used in downstream analytics or dbt models.
Pro-Move: 'We validate with SUM(employee_count) = total; include department_id if name can change.'
Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.
According to DataEngPrep.tech, this is one of the most frequently asked General/Other interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.