Percentage Window

Master SQL Percentage Window Function with Interview Questions

Prepare for SQL interviews with top questions on the percentage window function. Learn how to use it for rank-based analytics, examples, and practical use cases.


SQL Percentage Window Function: Interview Questions and Answers

SQL Window Functions allow advanced analytics on query results without grouping data. One essential function is calculating percentages across rows. Here’s a guide with common interview questions, answers, and examples to help you excel.


1. What is a Percentage Window Function in SQL?

A Percentage Window Function calculates the relative proportion of a value compared to the total of a set, often using the SUM() aggregate function and the OVER() clause.

Example Query:

SELECT department_id, salary, 
       salary * 100.0 / SUM(salary) OVER (PARTITION BY department_id) AS percentage
FROM employees;

Output Explanation:

  • SUM(salary) OVER (PARTITION BY department_id) calculates the total salary for each department.
  • Each employee’s percentage is derived as (salary / total_salary) * 100.

2. How is a Percentage Window Function Different from Grouped Aggregates?

  • Grouped Aggregates collapse rows, returning one row per group.
  • Window Functions preserve individual rows while adding aggregate values.

Basic SQL Percentage Window Function Questions

3. Write a Query to Calculate the Percentage of Total Sales Per Region.

SELECT region, sales, 
       sales * 100.0 / SUM(sales) OVER () AS percentage_of_total_sales
FROM sales_data;

Explanation:

  • The OVER() clause without PARTITION calculates the total sales across all regions.

4. What Happens if the Total is Zero in a Percentage Calculation? How Do You Handle It?

If the total is zero, a division by zero error occurs. Use a conditional statement to prevent this.

SELECT region, sales, 
       CASE WHEN SUM(sales) OVER () = 0 
            THEN 0 
            ELSE sales * 100.0 / SUM(sales) OVER () 
       END AS percentage
FROM sales_data;

Intermediate SQL Percentage Window Function Questions

5. Calculate Each Employee’s Salary Percentage Within Their Department.

SELECT department_id, name, salary, 
       salary * 100.0 / SUM(salary) OVER (PARTITION BY department_id) AS percentage
FROM employees;

6. What is the Purpose of the PARTITION BY Clause in Window Functions?

  • Answer: It divides the result set into subsets before applying the function. Without PARTITION BY, the function applies to the entire dataset.

Advanced SQL Percentage Window Function Questions

7. Rank Products Based on Their Contribution to Total Sales.

SELECT product_name, sales, 
       sales * 100.0 / SUM(sales) OVER () AS percentage,
       RANK() OVER (ORDER BY sales DESC) AS rank
FROM products;

8. Find the Cumulative Percentage of Total Sales Across Regions.

SELECT region, sales, 
       SUM(sales) OVER (ORDER BY sales DESC) * 100.0 / SUM(sales) OVER () AS cumulative_percentage
FROM sales_data;

Explanation:

  • SUM(sales) OVER (ORDER BY sales DESC) calculates a running total.
  • Dividing the running total by the overall total gives the cumulative percentage.

9. Calculate the Percentage Difference Between Consecutive Rows.

SELECT name, sales, 
       LAG(sales) OVER (ORDER BY sales DESC) AS previous_sales,
       (sales - LAG(sales) OVER (ORDER BY sales DESC)) * 100.0 / LAG(sales) OVER (ORDER BY sales DESC) AS percentage_difference
FROM sales_data;

FAQs on SQL Percentage Window Function

Q1. When Should You Use a Window Function Over a Group By?

  • Use Window Functions when you need individual rows along with aggregate values.
  • Use Group By when summarizing data into distinct groups.

Q2. Can Window Functions Be Nested?

No, you cannot nest window functions directly, but you can combine them with CTEs or subqueries.

Q3. What Databases Support Percentage Window Functions?

Most relational databases support window functions, including:

  • MySQL (from version 8.0)
  • PostgreSQL
  • SQL Server
  • Oracle

SQL Percentage Window Function Best Practices

  1. Handle Division by Zero: Use CASE statements to avoid errors.
  2. Use Aliases for Clarity: Assign meaningful names to derived percentage columns.
  3. Optimize Query Performance: Use indexes to enhance the performance of queries with window functions.

Call-to-Action

Enhance your SQL skills with our SQL Advanced Functions Guide!
Practice these queries in a free SQL sandbox or your local database to gain hands-on experience.