Named windows with WINDOW clause in PostgreSQL - Time & Space Complexity
We want to understand how the time to run a query changes when using named windows with the WINDOW clause in PostgreSQL.
Specifically, how does naming windows affect the work the database does as data grows?
Analyze the time complexity of the following query using named windows.
SELECT employee_id, department_id, salary,
AVG(salary) OVER dept_win AS avg_dept_salary,
RANK() OVER dept_win AS rank_in_dept
FROM employees
WINDOW dept_win AS (PARTITION BY department_id ORDER BY salary DESC);
This query calculates the average salary and rank of employees within each department using a named window called dept_win.
Look for repeated work inside the query.
- Primary operation: The database processes each department's employees separately (partition) and orders them by salary.
- How many times: Once per department, for all employees in that department.
The work depends on how many employees there are and how they are grouped by department.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 employees | About 10 operations, grouped by departments |
| 100 employees | About 100 operations, grouped by departments |
| 1000 employees | About 1000 operations, grouped by departments |
Pattern observation: The total work grows roughly in direct proportion to the number of employees.
Time Complexity: O(n log n)
This means the time grows a bit faster than the number of employees because sorting is needed within each department.
[X] Wrong: "Using a named window makes the query run faster because it avoids repeated work."
[OK] Correct: Naming a window just helps write the query cleaner; the database still does the sorting and partitioning once per window, so the main work stays the same.
Understanding how window functions work with partitions and ordering helps you explain query performance clearly and shows you know how databases handle grouped calculations efficiently.
"What if we removed the ORDER BY clause inside the WINDOW definition? How would the time complexity change?"
Practice
WINDOW clause in PostgreSQL?Solution
Step 1: Understand the role of WINDOW clause
The WINDOW clause allows defining named window specifications once for reuse.Step 2: Compare options with WINDOW clause purpose
Options A, B, and C describe other SQL features, not the WINDOW clause.Final Answer:
To define named window specifications for reuse in the query -> Option CQuick Check:
WINDOW clause = named window reuse [OK]
- Confusing WINDOW with temporary tables
- Thinking WINDOW orders rows directly
- Mixing WINDOW with JOIN operations
w1 that orders rows by date ascending?Solution
Step 1: Recall WINDOW clause syntax
The correct syntax is WINDOW name AS (window_definition), e.g., WINDOW w1 AS (ORDER BY date ASC).Step 2: Check each option's syntax
WINDOW w1 AS (ORDER BY date ASC) matches correct syntax. Options A, B, and D have incorrect keyword order or missing AS.Final Answer:
WINDOW w1 AS (ORDER BY date ASC) -> Option BQuick Check:
WINDOW name AS (definition) syntax [OK]
- Omitting AS keyword
- Using BY instead of ORDER BY
- Misplacing parentheses
SELECT department, employee, salary, AVG(salary) OVER w AS avg_salary FROM employees WINDOW w AS (PARTITION BY department ORDER BY employee) ORDER BY department, employee;
What does the
avg_salary column show?Solution
Step 1: Analyze the WINDOW clause
The window named w partitions rows by department and orders by employee.Step 2: Understand AVG() OVER w behavior
AVG(salary) OVER w calculates the average salary for rows in the same department up to the current employee in order.Final Answer:
The average salary of employees in the same department up to the current employee -> Option AQuick Check:
Partition + order = running average per department [OK]
- Ignoring ORDER BY in window
- Assuming average over entire table
- Confusing partition with no partition
SELECT name, salary, SUM(salary) OVER w FROM employees WINDOW w AS (PARTITION BY department ORDER BY name BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW);
It returns an error. What is the likely cause?
Solution
Step 1: Check window frame syntax
The frame clause must specify ROWS or RANGE explicitly before frame boundaries.Step 2: Identify error cause
The query has ORDER BY name BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, missing the required ROWS or RANGE keyword before BETWEEN.Final Answer:
The window frame clause requires ROWS or RANGE keyword explicitly -> Option AQuick Check:
Window frame needs ROWS or RANGE keyword [OK]
- Placing WINDOW clause after ORDER BY
- Omitting ROWS or RANGE keyword
- Using window name incorrectly in OVER
Solution
Step 1: Understand cumulative sum window frame
Cumulative sum requires a frame from the start to current row, so use RANGE or ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW.Step 2: Check window definition correctness
DefineWINDOW sales_win AS (PARTITION BY region ORDER BY date RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)and useSUM(sales) OVER sales_wincorrectly defines WINDOW with PARTITION BY region, ORDER BY date, and RANGE frame for cumulative sum.Step 3: Compare with other options
DefineWINDOW sales_win AS (PARTITION BY region ORDER BY date ROWS UNBOUNDED PRECEDING)and useSUM(sales) OVER sales_winuses ROWS UNBOUNDED PRECEDING which sums the entire partition, not cumulatively. DefineWINDOW sales_win AS (ORDER BY date PARTITION BY region)and useSUM(sales) OVER sales_winhas wrong order of clauses. UseSUM(sales) OVER (PARTITION BY region ORDER BY date)without WINDOW clause does not reuse named window.Final Answer:
Define WINDOW sales_win AS (PARTITION BY region ORDER BY date RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) and use SUM(sales) OVER sales_win -> Option DQuick Check:
Named window with correct frame = DefineWINDOW sales_win AS (PARTITION BY region ORDER BY date RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW)and useSUM(sales) OVER sales_win[OK]
- Omitting frame clause for cumulative sum
- Incorrect order of PARTITION BY and ORDER BY
- Not using named window for reuse
Ready to prove it?
Pass the test and this pattern moves unknown
