Recall & Review
beginner
What is the purpose of the WINDOW clause in SQL?
The WINDOW clause lets you define named window specifications that can be reused in multiple window functions within the same query, making your SQL cleaner and easier to read.
Click to reveal answer
beginner
How do you define a named window using the WINDOW clause?
You define a named window by writing WINDOW window_name AS (window_definition), where window_definition includes PARTITION BY, ORDER BY, and frame clauses.
Click to reveal answer
intermediate
Can named windows refer to other named windows in the WINDOW clause?
Yes, in PostgreSQL, a named window can refer to another named window to inherit its definition and add or override parts, helping avoid repetition.
Click to reveal answer
beginner
Example: What does this WINDOW clause do?
WINDOW w AS (PARTITION BY department ORDER BY salary DESC)
It defines a named window 'w' that groups rows by department and orders them by salary from highest to lowest. You can then use 'w' in window functions to apply this same partitioning and ordering.
Click to reveal answer
beginner
Why use named windows instead of repeating window definitions in each window function?
Named windows reduce repetition, make queries easier to maintain, and help avoid mistakes by defining the window once and reusing it multiple times.
Click to reveal answer
What does the WINDOW clause in SQL do?
✗ Incorrect
The WINDOW clause defines named windows that can be reused in window functions.
Which keyword is used to name a window in the WINDOW clause?
✗ Incorrect
You use AS to assign a name to a window specification, e.g., WINDOW w AS (...).
Can a named window inherit from another named window in PostgreSQL?
✗ Incorrect
PostgreSQL allows named windows to inherit from others to avoid repetition.
Which clause is NOT part of a window definition in the WINDOW clause?
✗ Incorrect
GROUP BY is not part of window definitions; PARTITION BY, ORDER BY, and frame clauses are.
Why is using named windows helpful?
✗ Incorrect
Named windows help by reducing repeated code and making queries easier to maintain.
Explain how the WINDOW clause helps when writing multiple window functions in a query.
Think about how you might write the same instructions multiple times versus once with a name.
You got /4 concepts.
Describe how you can create a named window that inherits from another named window in PostgreSQL.
It's like copying a recipe and then changing some ingredients.
You got /4 concepts.