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/SQL/Find average salary for each manager – Assume a table with manager_id and employee_salary

Find average salary for each manager – Assume a table with manager_id and employee_salary

SQLmedium1 min read

Reviewed by Aditya Kumar · Last reviewed 2026-08-08

To find the average salary for each manager, you should use the AVG() aggregate function combined with a GROUP BY clause on the manager id . This groups all employees under the same manager and then…

🤖 Analyze Your Answer
Frequency
Low
Asked at 1 company
Category
487
questions in SQL
Difficulty Split
130E|271M|86H
in this category
Total Bank
1,863
across 7 categories
Asked at these companies
Pubmatic

Why This Question Matters

This medium-level SQL question appears frequently in data engineering interviews at companies like Pubmatic. While less common, it tests deeper understanding that distinguishes strong candidates.

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
292 wordsIncludes code

To find the average salary for each manager, you should use the AVG() aggregate function combined with a GROUP BY clause on the manager_id. This groups all employees under the same manager and then calculates their average salary.

SELECT
    manager_id,
    AVG(employee_salary) AS avg_salary
FROM
    employees
GROUP BY
    manager_id;

Mechanics and Considerations

The GROUP BY manager_id clause is fundamental here; it partitions the dataset into distinct groups, one for each unique manager_id. The AVG(employee_salary) function then computes the arithmetic mean of salaries within each of these groups.

Handling NULLs: By default, GROUP BY treats NULL values in manager_id as a single group. If employees without an assigned manager should be excluded from the aggregation, add a WHERE manager_id IS NOT NULL clause before* the GROUP BY.
Filtering Aggregated Results: If you need to filter the results based on the average salary itself (e.g., managers with an average salary above a certain threshold), use the HAVING clause. HAVING filters after aggregation, whereas WHERE filters before* aggregation. For example: HAVING AVG(employee_salary) > 75000.

Performance and Scaling

For large employees tables, query performance is critical.

* Indexing: An index on manager_id is highly recommended. It allows the database to quickly locate and group records, significantly speeding up the GROUP BY operation by avoiding full table scans.
* Data Distribution: In distributed data warehouses or processing engines (e.g., Snowflake, Spark), how data is physically organized can impact performance. If employees data is clustered (like Snowflake's clustering keys) or partitioned by manager_id, it can minimize data shuffling (in Spark) or improve scan efficiency (in Snowflake micro-partitions), making the aggregation much faster.

In the interview, also mention: The distinction between WHERE and HAVING clauses and their execution order is a common interview topic.

⚡
Pro Tip

Red Flag: Forgetting NULL manager_id—may skew results. Pro-Move: 'We added HAVING COUNT(*) >= 3 to exclude managers with <3 reports—more statistically meaningful averages.'

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

Related SQL Questions

mediumWrite an SQL query to find the second-highest salary from an employee table.FreemediumDemonstrate the difference between DENSE_RANK() and RANK()FreemediumDiscuss differences between ROW_NUMBER(), RANK(), and DENSE_RANK(), and provide examples from your projects.FreemediumExplain the differences between Data Warehouse, Data Lake, and Delta LakeFreemediumExplain the differences between Repartition and Coalesce. When would you use each?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 SQL 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 SQL 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