0% found this document useful (0 votes)
15 views2 pages

SQL Window Functions Interview Guide

Window functions in SQL perform calculations across a set of related table rows without collapsing them into a single output. Key components include the function itself and the OVER() clause, which defines the window of rows, with options like PARTITION BY and ORDER BY. Common window functions include ROW_NUMBER(), RANK(), and LAG(), and they are useful for tasks such as ranking, accessing previous/next row values, and calculating running totals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

SQL Window Functions Interview Guide

Window functions in SQL perform calculations across a set of related table rows without collapsing them into a single output. Key components include the function itself and the OVER() clause, which defines the window of rows, with options like PARTITION BY and ORDER BY. Common window functions include ROW_NUMBER(), RANK(), and LAG(), and they are useful for tasks such as ranking, accessing previous/next row values, and calculating running totals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

SQL Window Functions - Explanation and Interview Q&A

What are Window Functions?


--------------------------
Window functions perform calculations across a set of table rows that are somehow related to the current row.
Unlike aggregate functions, they do not collapse rows into a single output row.

Key Components of a Window Function:


1. Function: e.g., ROW_NUMBER(), RANK(), SUM(), AVG(), LAG(), LEAD()
2. OVER() Clause: defines the window of rows.

OVER Clause Options:


- PARTITION BY: Divides result set into partitions.
- ORDER BY: Defines the order of rows within each partition.
- ROWS or RANGE: Optional frame specification (e.g., ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT
ROW)

Common Window Functions:


- ROW_NUMBER(): Unique row number per partition/order.
- RANK(): Ranks with gaps for ties.
- DENSE_RANK(): Ranks without gaps.
- LAG()/LEAD(): Access data from previous/next rows.
- FIRST_VALUE()/LAST_VALUE(): Value from the first/last row in the frame.
- SUM()/AVG(): Aggregates within the window frame.

Example:
SELECT
employee_id,
department,
salary,
RANK() OVER(PARTITION BY department ORDER BY salary DESC) AS dept_rank
FROM employees;

This gives each employee a rank based on salary within their department.

Interview Questions and Answers


-------------------------------

1. What is a window function in SQL?


A window function performs a calculation across a set of table rows related to the current row.

2. Difference between window and aggregate function?


Aggregate functions return one result per group. Window functions return one result per row, preserving all rows.
3. Use case for ROW_NUMBER()?
Useful for deduplicating data or finding the nth record in a group.

4. Write a query to get the top 2 salaries in each department.


SELECT * FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY department ORDER BY salary DESC) AS rn
FROM employees
) AS ranked
WHERE rn <= 2;

5. What is the use of LAG() and LEAD()?


LAG(): Access previous row's value.
LEAD(): Access next row's value.

6. How to calculate running total?


SELECT
employee_id,
salary,
SUM(salary) OVER(ORDER BY employee_id) AS running_total
FROM employees;

7. Difference between RANK() and DENSE_RANK()?


RANK() skips ranks if there are ties. DENSE_RANK() doesn't.

8. Can you use WHERE with window functions?


No, use a subquery or CTE. WHERE filters before window functions are applied.

9. Use FIRST_VALUE() and LAST_VALUE().


SELECT
employee_id,
department,
salary,
FIRST_VALUE(salary) OVER(PARTITION BY department ORDER BY salary) AS min_salary,
LAST_VALUE(salary) OVER(PARTITION BY department ORDER BY salary
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS max_salary
FROM employees;

10. Can window functions be used in GROUP BY?


Not directly. Use in SELECT or ORDER BY, typically with subqueries/CTEs if grouping is needed.

You might also like