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
✗ Incorrect
COUNT() counts how many rows are in the window frame for each row.
Which clause defines the window for SUM() as a window function?
AOVER()
BORDER BY
CGROUP BY
DWHERE
✗ Incorrect
The OVER() clause defines the window or set of rows the function works on.
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
✗ Incorrect
SUM() as a window function can calculate running totals over ordered rows.
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
✗ Incorrect
Without PARTITION BY, the window function treats all rows as one group.
Which window function would you use to find the average value over a sliding window of rows?
AAVG()
BSUM()
CCOUNT()
DMIN()
✗ Incorrect
AVG() calculates the average over the window frame for each row.
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.
Practice
(1/5)
1. What does the COUNT(*) OVER() window function do in PostgreSQL?
easy
A. Counts all rows in the result set without grouping them.
B. Counts only distinct rows in the table.
C. Counts rows and groups them by a specified column.
D. Counts rows but returns only one aggregated result.
Solution
Step 1: Understand COUNT(*) OVER() usage
The function counts all rows in the query result but does not group or collapse rows.
Step 2: Difference from GROUP BY
Unlike GROUP BY, it keeps all rows visible and adds the count as a column.
Final Answer:
Counts all rows in the result set without grouping them. -> Option A
Quick Check:
COUNT(*) OVER() counts all rows [OK]
Hint: Window COUNT(*) counts all rows without grouping [OK]
Common Mistakes:
Confusing window COUNT with GROUP BY count
Thinking it returns a single aggregated row
Assuming it counts distinct values only
2. Which of the following is the correct syntax to calculate a running total of sales using SUM as a window function in PostgreSQL?
easy
A. SELECT sales, SUM(sales) OVER (ORDER BY date) AS running_total FROM sales_data;
B. SELECT sales, SUM(sales) FROM sales_data ORDER BY date;
C. SELECT sales, SUM(sales) PARTITION BY date FROM sales_data;
D. SELECT sales, SUM(sales) OVER PARTITION BY date FROM sales_data;
Solution
Step 1: Identify correct window function syntax
SUM(sales) OVER (ORDER BY date) calculates cumulative sum ordered by date.
Step 2: Check other options for syntax errors
The other options misuse window function syntax or omit OVER keyword.
Final Answer:
SELECT sales, SUM(sales) OVER (ORDER BY date) AS running_total FROM sales_data; -> Option A
Quick Check:
SUM() OVER (ORDER BY ...) is correct syntax [OK]
Hint: Use OVER() with ORDER BY for running totals [OK]
Common Mistakes:
Omitting OVER() clause
Using PARTITION BY without OVER()
Confusing GROUP BY with window functions
3. Given the table employees with columns department and salary, what will the following query return?
SELECT department, salary, AVG(salary) OVER (PARTITION BY department) AS avg_dept_salary FROM employees;
medium
A. Syntax error due to missing GROUP BY clause.
B. Each row shows salary and overall average salary of all employees.
C. Each row shows salary and average salary of all departments combined.
D. Each row shows salary and average salary of its department.
Solution
Step 1: Understand PARTITION BY in AVG window function
AVG(salary) OVER (PARTITION BY department) calculates average salary per department for each row.
Step 2: Confirm output per row
Each row keeps salary and adds average salary of that row's department without grouping rows.
Final Answer:
Each row shows salary and average salary of its department. -> Option D
Quick Check:
AVG() OVER (PARTITION BY ...) averages per group per row [OK]
Hint: PARTITION BY groups rows but keeps all rows visible [OK]
Common Mistakes:
Thinking it aggregates to one row per group
Confusing window AVG with GROUP BY AVG
Expecting overall average without PARTITION BY
4. Identify the error in this query:
SELECT employee_id, salary, COUNT(*) OVER PARTITION BY department FROM employees;
medium
A. COUNT(*) cannot be used as a window function.
B. Missing parentheses around PARTITION BY clause in OVER().
C. employee_id cannot be selected with window functions.
D. No error; query is correct.
Solution
Step 1: Check syntax of window function
OVER clause must have parentheses: OVER (PARTITION BY department).
Step 2: Validate other parts
COUNT(*) is valid as window function; selecting employee_id is allowed.
Final Answer:
Missing parentheses around PARTITION BY clause in OVER(). -> Option B
Quick Check:
OVER() requires parentheses [OK]
Hint: Always use parentheses after OVER keyword [OK]
Common Mistakes:
Omitting parentheses in OVER()
Thinking COUNT(*) can't be window function
Assuming window functions require GROUP BY
5. You want to find the cumulative count of orders per customer ordered by order date. Which query correctly uses COUNT as a window function to achieve this?
hard
A. SELECT customer_id, order_date, COUNT(*) FROM orders GROUP BY customer_id, order_date ORDER BY order_date;
B. SELECT customer_id, order_date, COUNT(*) OVER (ORDER BY order_date) AS cumulative_orders FROM orders;
C. SELECT customer_id, order_date, COUNT(*) OVER (PARTITION BY customer_id ORDER BY order_date) AS cumulative_orders FROM orders;
D. SELECT customer_id, order_date, COUNT(*) OVER (PARTITION BY order_date ORDER BY customer_id) AS cumulative_orders FROM orders;
Solution
Step 1: Understand cumulative count per customer
COUNT(*) OVER (PARTITION BY customer_id ORDER BY order_date) counts rows per customer in order of order_date cumulatively.
Step 2: Check other options for correctness
SELECT customer_id, order_date, COUNT(*) FROM orders GROUP BY customer_id, order_date ORDER BY order_date; uses GROUP BY, not cumulative count; C lacks partition by customer; D partitions by order_date, not customer.
Final Answer:
SELECT customer_id, order_date, COUNT(*) OVER (PARTITION BY customer_id ORDER BY order_date) AS cumulative_orders FROM orders; -> Option C
Quick Check:
Use PARTITION BY customer_id with ORDER BY for cumulative count [OK]
Hint: Partition by customer and order by date for cumulative count [OK]
Common Mistakes:
Missing PARTITION BY customer_id
Using GROUP BY instead of window function
Partitioning by wrong column
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.