Ask Claude about this

Department Budget Analysis

Business Context

At Google, monitoring departmental salary budgets is essential for financial planning, resource allocation, and ensuring equitable compensation across the organization. This analysis calculates the total salary allocation for each department and identifies departments exceeding the company average, providing valuable insights for budget forecasting, headcount planning, and organizational design decisions. The results help finance teams and department leaders make data-driven decisions about hiring, compensation strategies, and resource distribution.

Technical Requirements

The primary objective is to develop a PostgreSQL query for the following:

Task: Calculate the total salary budget for each department and identify departments that exceed the average department budget.

Analytical Components:

  • Calculate the total salary for each department.
  • Determine the average salary budget across all departments.
  • Flag departments whose total budget exceeds this average budget with 'Yes', otherwise 'No'.
  • The output should include department name, its total budget, and the 'exceeds_average' flag.

Definitions:

  • "Total salary budget" for a department is the sum of all employee salaries within that department.
  • "Average budget" refers to the average of these calculated total departmental budgets.
  • A department "exceeds average" if its total budget is strictly greater than the overall average department budget.

Database Schema

The database consists of two tables: Employees and Departments.

-- Table: Departments
CREATE TABLE Departments (
    department_id INT PRIMARY KEY,
    department_name VARCHAR(255) NOT NULL
);

-- Table: Employees
CREATE TABLE Employees (
    employee_id INT PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    department_id INT,
    salary INT NOT NULL,
    FOREIGN KEY (department_id) REFERENCES Departments(department_id)
);

Sample Data

Assume the following sample data for the Employees and Departments tables (salary is INTEGER, hire_date is DATE type but not directly used in this specific problem's calculation):

Table: Employees

employee_id name department_id salary
1 John Smith 1 60000
2 Jane Doe 1 70000
3 Bob Johnson 2 65000
4 Alice Brown 2 55000
5 Mike Wilson 2 60000
6 Sarah Garcia 3 80000
7 Tom Rodriguez 3 75000

Table: Departments

department_id department_name
1 Engineering
2 Marketing
3 Finance

Expected Output (based on sample data):

Calculation Walkthrough:
- Engineering Total Budget: 60000 + 70000 = 130000
- Marketing Total Budget: 65000 + 55000 + 60000 = 180000
- Finance Total Budget: 80000 + 75000 = 155000
- Average Department Budget: (130000 + 180000 + 155000) / 3 = 465000 / 3 = 155000.00

department_name total_budget exceeds_average
Engineering 130000 No
Marketing 180000 Yes
Finance 155000 No

Note: Marketing's budget of 180,000 exceeds the average of 155,000. Finance's budget equals the average, so it does not "strictly exceed".

Department Budgets vs. Average

MODERATE

Write a PostgreSQL query to calculate the total salary budget for each department. Then, determine the average budget across all departments and identify which departments have a total budget strictly greater than this average. Display the department name, its total budget, and a flag ('Yes'/'No') indicating if it exceeds the average.

Your Turn! What are your thoughts on this problem or alternative ways to solve it? Share your own SQL query attempts or insights in the comments below!

Nerchuko Academy · Free DS Interview Prep