Department headcount and pay

aggregations

Department headcount and pay

Salesforce SQL Interview Question

Salesforce's HR team is preparing next year's budget and needs a summary of the size and pay of each department.

For each department, find the number of employees as headcount and the average salary as avg_salary. Do not round the average.

Asked of

  • Data Analyst
  • Business Analyst
  • BI Analyst

employeesTable16 rows

Column NameType
employee_idBIGINT
nameVARCHAR
departmentVARCHAR
salaryBIGINT
manager_idBIGINT
hire_dateDATE

employeesExample Input

employee_idnamedepartmentsalarymanager_idhire_date
1Ravi KumarExecutive240000NULL2018-01-15
2Mei LinEngineering18500012018-06-04
3Sofia RossiEngineering14200022019-02-18
4James ParkEngineering12800022019-09-02
7Diego AlvarezAnalytics16800012018-10-22
8Hannah WeberAnalytics13400072020-01-13

Example Output

departmentheadcountavg_salary
Analytics2151000
Engineering3151666.6667
Executive1240000

Explanation

Engineering has three employees in the example, earning 185,000, 142,000 and 128,000, so its average is 151,666.666... with the decimals kept. Executive has only Ravi Kumar, so its average is that one salary of 240,000.

The example above is a small slice of the data. Your query runs against the full tables.