A window function performs a calculation across a set of table rows related to the current row without collapsing the result into a single output row. It allows you to keep the original rows while adding aggregated or ranked data.
Click to reveal answer
intermediate
Why are advanced window functions important in data analysis?
They let you perform complex calculations like running totals, moving averages, and ranking within groups efficiently, all while preserving the original data rows for detailed insights.
Click to reveal answer
intermediate
Name three common advanced window functions.
ROW_NUMBER(), RANK(), and LAG() are common advanced window functions used for ranking rows, handling ties, and accessing previous row values respectively.
Click to reveal answer
intermediate
How do window functions differ from GROUP BY aggregations?
GROUP BY aggregates rows into fewer rows, losing detail, while window functions calculate over a set of rows but keep each original row intact, adding extra info without losing detail.
Click to reveal answer
beginner
Give a real-life example where advanced window functions help.
In sales data, you can use window functions to calculate a running total of sales per salesperson over time, helping track progress without losing individual sale details.
Click to reveal answer
What does the ROW_NUMBER() window function do?
AJoins two tables
BCalculates the sum of a column
CFilters rows based on a condition
DAssigns a unique sequential number to rows within a partition
✗ Incorrect
ROW_NUMBER() assigns a unique sequential number to each row within a partition, useful for ranking.
Which window function lets you access data from a previous row?
ASUM()
BCOUNT()
CLAG()
DMAX()
✗ Incorrect
LAG() allows you to look at the value from a previous row in the window.
What is a key advantage of window functions over GROUP BY?
AThey keep all original rows while adding calculations
BThey delete duplicate rows
CThey reduce the number of rows
DThey create new tables
✗ Incorrect
Window functions keep all original rows and add extra calculated columns without grouping rows.
Which function would you use to rank rows with ties getting the same rank?
AROW_NUMBER()
BRANK()
CDENSE_RANK()
DCOUNT()
✗ Incorrect
RANK() assigns the same rank to tied rows but skips ranks after ties.
Why might you use a running total with window functions?
ATo see cumulative sums over time without losing detail
BTo join multiple tables
CTo filter out unwanted data
DTo summarize data by deleting rows
✗ Incorrect
Running totals show cumulative sums over rows, useful for trends, while keeping all rows.
Explain in your own words why advanced window functions are useful compared to simple aggregation.
Think about how you can add extra info without losing details.
You got /3 concepts.
Describe a scenario where you would use the LAG() function and what problem it solves.
Imagine you want to see how a value changed from one row to the next.
You got /3 concepts.
Practice
(1/5)
1. What is the main advantage of using advanced window functions in SQL compared to simple aggregate functions?
easy
A. They only work on single rows without considering others.
B. They automatically create indexes for faster queries.
C. They allow calculations across rows without collapsing the result into groups.
D. They replace the need for JOIN operations.
Solution
Step 1: Understand aggregate functions behavior
Aggregate functions like SUM or AVG group rows and return one result per group, collapsing data.
Step 2: Understand window functions behavior
Window functions perform calculations across rows but keep each row visible, without grouping them.
Final Answer:
They allow calculations across rows without collapsing the result into groups. -> Option C
Quick Check:
Window functions = calculations without grouping [OK]
Hint: Window functions keep rows, aggregates collapse them [OK]
Common Mistakes:
Thinking window functions group rows like aggregates
Confusing window functions with JOINs
Assuming window functions only work on single rows
2. Which of the following is the correct syntax to calculate a running total of sales using a window function?
easy
A. SELECT SUM(sales) OVER (ORDER BY date) AS running_total FROM sales_data;
B. SELECT SUM(sales) GROUP BY date ORDER BY running_total FROM sales_data;
C. SELECT RUNNING_TOTAL(sales) FROM sales_data ORDER BY date;
D. SELECT sales + PREVIOUS(sales) FROM sales_data;
Solution
Step 1: Identify correct window function syntax
SUM() OVER (ORDER BY column) computes running total ordered by that column.
Step 2: Check other options for syntax errors
The other options use invalid SQL syntax or functions that don't exist.
Final Answer:
SELECT SUM(sales) OVER (ORDER BY date) AS running_total FROM sales_data; -> Option A
Quick Check:
SUM() OVER (ORDER BY ...) = running total [OK]
Hint: Use SUM() OVER (ORDER BY ...) for running totals [OK]
Common Mistakes:
Using GROUP BY instead of OVER()
Trying non-existent functions like RUNNING_TOTAL()
Adding values manually without window syntax
3. Given the table employees with columns department, salary, and name, what will the following query return?
SELECT name, department, salary, RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS rank FROM employees;
medium
A. Calculates total salary per department.
B. Ranks employees by salary across all departments combined.
C. Returns employees sorted by name with no ranking.
D. Ranks employees by salary within each department, highest salary gets rank 1.
Solution
Step 1: Understand PARTITION BY usage
PARTITION BY department groups rows by department for the window function.
Step 2: Understand RANK() OVER (ORDER BY salary DESC)
Ranks salaries in descending order within each department, highest salary gets rank 1.
Final Answer:
Ranks employees by salary within each department, highest salary gets rank 1. -> Option D
Quick Check:
RANK() + PARTITION BY = rank within groups [OK]
Hint: PARTITION BY groups, ORDER BY sets rank order [OK]
Common Mistakes:
Thinking RANK() ignores PARTITION BY
Confusing RANK() with total aggregation
Assuming sorting by name affects rank
4. Identify the error in this SQL query that tries to calculate a cumulative sum of sales per region:
SELECT region, sales, SUM(sales) OVER (PARTITION region ORDER BY date) AS cumulative_sales FROM sales_data;
medium
A. Missing parentheses around PARTITION BY clause.
B. Incorrect keyword; should be PARTITION BY instead of PARTITION.
C. ORDER BY cannot be used inside window functions.
D. SUM() cannot be used as a window function.
Solution
Step 1: Check window function syntax
The correct syntax requires PARTITION BY, not just PARTITION.
Step 2: Validate other parts
ORDER BY inside OVER() is valid for running totals; SUM() is valid as window function.
Final Answer:
Incorrect keyword; should be PARTITION BY instead of PARTITION. -> Option B
Quick Check:
Use PARTITION BY, not PARTITION alone [OK]
Hint: Use PARTITION BY, not just PARTITION [OK]
Common Mistakes:
Omitting BY after PARTITION
Thinking ORDER BY is invalid in window functions
Believing SUM() can't be a window function
5. You want to find the difference in salary between each employee and the highest salary in their department using window functions. Which query correctly achieves this?
hard
A. SELECT name, department, salary, MAX(salary) OVER (PARTITION BY department) - salary AS diff FROM employees;
B. SELECT name, department, salary, salary - MAX(salary) OVER (PARTITION BY department) AS diff FROM employees;
C. SELECT name, department, salary, salary - MAX(salary) FROM employees GROUP BY department;
D. SELECT name, department, salary, salary - MAX(salary) FROM employees;
Solution
Step 1: Use MAX() as window function partitioned by department
MAX(salary) OVER (PARTITION BY department) gives highest salary per department for each row.
Step 2: Calculate difference as highest salary minus employee salary
Subtract employee salary from max salary to get positive difference.
Step 3: Check options for correct calculation and syntax
SELECT name, department, salary, MAX(salary) OVER (PARTITION BY department) - salary AS diff FROM employees; correctly subtracts salary from max salary per department; SELECT name, department, salary, salary - MAX(salary) OVER (PARTITION BY department) AS diff FROM employees; reverses subtraction; The remaining options misuse GROUP BY or lack window function.
Final Answer:
SELECT name, department, salary, MAX(salary) OVER (PARTITION BY department) - salary AS diff FROM employees; -> Option A
Quick Check:
MAX() OVER PARTITION BY + subtraction = difference [OK]
Hint: Subtract salary from MAX() OVER partition for difference [OK]
Common Mistakes:
Reversing subtraction order
Using GROUP BY instead of window functions
Omitting PARTITION BY in MAX()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.