0
0
PostgreSQLquery~5 mins

SUM, AVG, COUNT as window functions in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the SUM() window function do in SQL?
It calculates the running total of a numeric column over a specified window of rows without collapsing the result into a single row.
Click to reveal answer
intermediate
How does AVG() as a window function differ from AVG() as an aggregate function?
AVG() as a window function calculates the average over a set of rows defined by the window, returning a value for each row, while as an aggregate it returns one average for the entire group.
Click to reveal answer
beginner
What is the purpose of COUNT() when used as a window function?
COUNT() counts the number of rows in the window frame for each row, allowing you to see counts that change as you move through the dataset.
Click to reveal answer
beginner
Explain the role of the OVER() clause in window functions.
The OVER() clause defines the window or set of rows the function operates on, specifying partitioning and ordering without grouping rows into one result.
Click to reveal answer
intermediate
Can you use SUM(), AVG(), and COUNT() as window functions without PARTITION BY? What happens?
Yes, if PARTITION BY is omitted, the window function operates over all rows in the query result, effectively treating the entire result as one partition.
Click to reveal answer
What does the COUNT() window function return for each row?
AThe maximum value in the column
BThe sum of values in the column
CThe total number of rows in the window frame
DThe average of values in the column
Which clause defines the window for SUM() as a window function?
AOVER()
BORDER BY
CGROUP BY
DWHERE
If you want to calculate a running total of sales by date, which function would you use?
ACOUNT()
BMAX()
CAVG()
DSUM() as a window function
What happens if you omit PARTITION BY in a window function?
AThe function returns NULL
BThe function operates on the entire result set as one partition
CThe query fails with an error
DThe function only works on the first row
Which window function would you use to find the average value over a sliding window of rows?
AAVG()
BSUM()
CCOUNT()
DMIN()
Describe how SUM(), AVG(), and COUNT() work as window functions and how they differ from aggregate functions.
Think about how results appear for each row versus grouped rows.
You got /5 concepts.
    Explain the role of the OVER() clause and how PARTITION BY and ORDER BY affect window functions like SUM(), AVG(), and COUNT().
    Consider how you can control which rows the function looks at.
    You got /4 concepts.