Compute each salesperson's share of company and team sales in one query with window functions

Quick Overview

Asks how to show each salesperson's share of total company sales and of their own team's sales side by side in a single SQL query. Tests understanding of aggregate window functions, partitioning, and safe division when computing percentages in SQL.

Compute each salesperson's share of company and team sales in one query with window functions

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Explain how to calculate a salesperson's percentage of total company sales and their percentage of their team's sales in a single query using aggregate window functions. Assume a table `salespeople(salesperson, team, sales)` with one row per salesperson. ```hint Two different windows A window aggregate can look at every row or only at the rows that share a value with the current row, and one SELECT can hold several of them. ``` ### What a Strong Answer Covers - How a window aggregate attaches a total to each row without collapsing the rows - The window definition for the company-wide total and for the team total - Why a plain GROUP BY does not give both percentages next to each salesperson - How to avoid integer division and other arithmetic pitfalls in the percentages ### Follow-up Questions - The table holds one row per sale instead of one per salesperson. How does the query change? - How would you also return each salesperson's rank by sales within their team? - What happens if a team's total sales are zero, and how would you guard against it?

Overview: Asks how to show each salesperson's share of total company sales and of their own team's sales side by side in a single SQL query. Tests understanding of aggregate window functions, partitioning, and safe division when computing percentages in SQL.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Explain how to calculate a salesperson's percentage of total company sales and their percentage of their team's sales in a single query using aggregate window functions. Assume a table salespeople(salesperson, team, sales) with one row per salesperson.

What a Strong Answer Covers Guidance

  • How a window aggregate attaches a total to each row without collapsing the rows
  • The window definition for the company-wide total and for the team total
  • Why a plain GROUP BY does not give both percentages next to each salesperson
  • How to avoid integer division and other arithmetic pitfalls in the percentages

Follow-up Questions Guidance

  • The table holds one row per sale instead of one per salesperson. How does the query change?
  • How would you also return each salesperson's rank by sales within their team?
  • What happens if a team's total sales are zero, and how would you guard against it?