Essential cookies keep authentication working. With your permission, we also use analytics cookies to understand and improve the product. Read our Privacy Policy

DataEngPrep.tech
QuestionsPracticeAI CoachDashboardPricingBlog
ProLogin
Home/Questions/General/Other/Given the data with id, name, and department, how would you calculate how many employees are in each department?

Given the data with id, name, and department, how would you calculate how many employees are in each department?

General/Othermedium2 min read

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…

🤖 Analyze Your Answer
Frequency
Low
Asked at 1 company
Category
243
questions in General/Other
Difficulty Split
151E|43M|49H
in this category
Total Bank
1,863
across 7 categories
Asked at these companies
Infosys
Key Concepts Tested
sparksqlwindow

Why This Question Matters

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.

How to Approach This

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.

Expert Answer
325 wordsIncludes code

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.

Mechanics and Why

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.

Example and Performance Considerations

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…

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 Tip

Pro-Move: 'We validate with SUM(employee_count) = total; include department_id if name can change.'

Want all answers as a PDF for offline study?
Seven focused volumes with 750+ in-depth answers — Answer Vault →

Related General/Other Questions

hardHave you worked on Data Warehousing projects?FreemediumHow would you read data from a web API? What steps would you follow after reading the data?FreehardRetrieve the most recent sale_timestamp for each product (Latest Transaction).FreehardWhat is the difference between OLTP and OLAP?FreemediumWhat is the difference between SQL and NoSQL databases?Free

Level up your prep

Recommended
Educative
Educative Unlimited

800+ hands-on courses — Grokking System Design, Coding Patterns, and AI mock interviews for your DE loop.

Start learning →

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.

← Back to all questionsMore General/Other questions →
Categories
All QuestionsSQLSpark / Big DataPython / CodingSystem DesignCloud / ToolsBehavioral
By Company
AmazonGoogleDatabricksSnowflakeAWSAzureMicrosoftNetflixUberTCS
Interview Guides
All GuidesTop SQL QuestionsTop Spark QuestionsPySpark QuestionsTop Python QuestionsTop System DesignKafka QuestionsAirflow QuestionsSQL Window FunctionsETL QuestionsData Modeling
Products
AI Interview CoachAnswer AnalyzerSQL PlaygroundResume AnalyzerAnswer Vault PDFsPricing
Company
About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
© 2026 DataEngPrep.tech. All rights reserved.
AboutBlogContactDisclaimer