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:
Id | Name | Department | Salary |
---|---|---|---|
1 | Alice | HR | 50,000 |
2 | Bob | HR | 75,000 |
3 | Charlie | IT | 100,000 |
4 | Dana | IT | 150,000 |
The SQL query will return:
Id | Name | Department | Salary | Salary Share Percent |
---|---|---|---|---|
1 | Alice | HR | 50,000 | 40.00% |
2 | Bob | HR | 75,000 | 60.00% |
3 | Charlie | IT | 100,000 | 40.00% |
4 | Dana | IT | 150,000 | 60.00% |
Using SQL in Google Sheets
With the SheetQuery add-on, you can run SQL queries directly in Google Sheets:
- Install SheetQuery from the Google Workspace Marketplace.
- Fill your sheet with data.
- 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!