SQL in Google Sheets: Calculating Salary Shares using window functions

Categories:

Google Sheets is a powerful tool, but some advanced tasks—like calculating an employee’s salary as a percentage of their department’s total—can be challenging. SQL, especially with window functions, makes this analysis straightforward.

The Challenge

Given a dataset with employee details (id, name, department, and salary), calculating salary shares in Sheets requires manual grouping, matching, and formulas—time-consuming and prone to errors.

The SQL Solution

Here’s how SQL simplifies the process:

SELECT
  Id,
  Name,
  Department,
  Salary,
  (
    Salary / SUM(Salary) OVER (
      PARTITION BY
        Department
    )
  ) * 100 AS Salary_Share_Percent
FROM
  "Employees!A1:D5?headers=1";

You can try running this query yourself on a sample dataset to see how it works in practice and better understand the results.

Example Output

If the dataset looks like this:

IdNameDepartmentSalary
1AliceHR50,000
2BobHR75,000
3CharlieIT100,000
4DanaIT150,000

The SQL query will return:

IdNameDepartmentSalarySalary Share Percent
1AliceHR50,00040.00%
2BobHR75,00060.00%
3CharlieIT100,00040.00%
4DanaIT150,00060.00%

Using SQL in Google Sheets

With the SheetQuery add-on, you can run SQL queries directly in Google Sheets:

  1. Install SheetQuery from the Google Workspace Marketplace.
  2. Fill your sheet with data.
  3. Run the query above to instantly compute salary shares.

Conclusion

SQL with SheetQuery unlocks advanced analytics in Google Sheets, letting you perform complex tasks like salary share calculations with ease. Install SheetQuery today and try it out!