0
0
PostgreSQLquery~20 mins

SUM, AVG, COUNT as window functions in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of SUM() as a window function
Given the table sales with columns id, region, and amount, what is the output of this query?
SELECT id, region, amount, SUM(amount) OVER (PARTITION BY region ORDER BY id) AS running_total FROM sales ORDER BY id;
PostgreSQL
CREATE TABLE sales (id INT, region TEXT, amount INT);
INSERT INTO sales VALUES
(1, 'East', 100),
(2, 'East', 150),
(3, 'West', 200),
(4, 'East', 50),
(5, 'West', 100);
A[{"id":1,"region":"East","amount":100,"running_total":100},{"id":2,"region":"East","amount":150,"running_total":250},{"id":3,"region":"West","amount":200,"running_total":200},{"id":4,"region":"East","amount":50,"running_total":300},{"id":5,"region":"West","amount":100,"running_total":300}]
B[{"id":1,"region":"East","amount":100,"running_total":100},{"id":2,"region":"East","amount":150,"running_total":150},{"id":3,"region":"West","amount":200,"running_total":200},{"id":4,"region":"East","amount":50,"running_total":50},{"id":5,"region":"West","amount":100,"running_total":100}]
C[{"id":1,"region":"East","amount":100,"running_total":100},{"id":2,"region":"East","amount":150,"running_total":250},{"id":3,"region":"West","amount":200,"running_total":300},{"id":4,"region":"East","amount":50,"running_total":350},{"id":5,"region":"West","amount":100,"running_total":400}]
D[{"id":1,"region":"East","amount":100,"running_total":100},{"id":2,"region":"East","amount":150,"running_total":250},{"id":3,"region":"West","amount":200,"running_total":200},{"id":4,"region":"East","amount":50,"running_total":250},{"id":5,"region":"West","amount":100,"running_total":300}]
Attempts:
2 left
💡 Hint
Think about how the SUM() window function adds amounts within each region, ordered by id.
query_result
intermediate
2:00remaining
Output of AVG() as a window function with frame
Consider the table scores with columns student_id and score. What is the output of this query?
SELECT student_id, score, AVG(score) OVER (ORDER BY student_id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS avg_score FROM scores ORDER BY student_id;
PostgreSQL
CREATE TABLE scores (student_id INT, score INT);
INSERT INTO scores VALUES
(1, 80),
(2, 90),
(3, 70),
(4, 100);
A[{"student_id":1,"score":80,"avg_score":80.0},{"student_id":2,"score":90,"avg_score":85.0},{"student_id":3,"score":70,"avg_score":70.0},{"student_id":4,"score":100,"avg_score":85.0}]
B[{"student_id":1,"score":80,"avg_score":80.0},{"student_id":2,"score":90,"avg_score":90.0},{"student_id":3,"score":70,"avg_score":70.0},{"student_id":4,"score":100,"avg_score":100.0}]
C[{"student_id":1,"score":80,"avg_score":80.0},{"student_id":2,"score":90,"avg_score":85.0},{"student_id":3,"score":70,"avg_score":80.0},{"student_id":4,"score":100,"avg_score":85.0}]
D[{"student_id":1,"score":80,"avg_score":85.0},{"student_id":2,"score":90,"avg_score":80.0},{"student_id":3,"score":70,"avg_score":85.0},{"student_id":4,"score":100,"avg_score":70.0}]
Attempts:
2 left
💡 Hint
The frame includes the current row and the one before it, so average is over 1 or 2 scores.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in COUNT() window function
Which option contains a syntax error when using COUNT() as a window function?
PostgreSQL
SELECT department, employee_id, COUNT(*) OVER PARTITION BY department FROM employees;
ASELECT department, employee_id, COUNT(*) OVER (PARTITION BY department) FROM employees;
BSELECT department, employee_id, COUNT(*) OVER PARTITION BY department FROM employees;
CSELECT department, employee_id, COUNT(*) OVER (PARTITION BY department ORDER BY employee_id) FROM employees;
DSELECT department, employee_id, COUNT(*) OVER (PARTITION BY department ROWS UNBOUNDED PRECEDING) FROM employees;
Attempts:
2 left
💡 Hint
Check the parentheses around the window specification.
optimization
advanced
2:00remaining
Optimizing AVG() window function with partition
You want to calculate the average sales per region for each sale row in the sales table. Which query is the most efficient and correct to achieve this?
PostgreSQL
Table sales(id INT, region TEXT, amount INT)
ASELECT id, region, amount, AVG(amount) OVER (PARTITION BY region) AS avg_region_sales FROM sales;
BSELECT id, region, amount, AVG(amount) FROM sales GROUP BY region;
CSELECT id, region, amount, AVG(amount) OVER () AS avg_region_sales FROM sales;
DSELECT id, region, amount, AVG(amount) OVER (ORDER BY region) AS avg_region_sales FROM sales;
Attempts:
2 left
💡 Hint
Think about how to get average per region for each row without grouping rows.
🧠 Conceptual
expert
3:00remaining
Understanding COUNT() window function behavior
Given the table orders with columns order_id, customer_id, and status, what will be the result of this query?
SELECT order_id, customer_id, status, COUNT(*) OVER (PARTITION BY customer_id ORDER BY order_id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_count FROM orders ORDER BY order_id;

Assuming data:
order_id | customer_id | status 1 | 101 | shipped 2 | 102 | pending 3 | 101 | shipped 4 | 101 | pending 5 | 102 | shipped
PostgreSQL
CREATE TABLE orders (order_id INT, customer_id INT, status TEXT);
INSERT INTO orders VALUES
(1, 101, 'shipped'),
(2, 102, 'pending'),
(3, 101, 'shipped'),
(4, 101, 'pending'),
(5, 102, 'shipped');
A[{"order_id":1,"customer_id":101,"status":"shipped","running_count":1},{"order_id":2,"customer_id":102,"status":"pending","running_count":1},{"order_id":3,"customer_id":101,"status":"shipped","running_count":3},{"order_id":4,"customer_id":101,"status":"pending","running_count":4},{"order_id":5,"customer_id":102,"status":"shipped","running_count":2}]
B[{"order_id":1,"customer_id":101,"status":"shipped","running_count":1},{"order_id":2,"customer_id":102,"status":"pending","running_count":2},{"order_id":3,"customer_id":101,"status":"shipped","running_count":2},{"order_id":4,"customer_id":101,"status":"pending","running_count":3},{"order_id":5,"customer_id":102,"status":"shipped","running_count":3}]
C[{"order_id":1,"customer_id":101,"status":"shipped","running_count":1},{"order_id":2,"customer_id":102,"status":"pending","running_count":1},{"order_id":3,"customer_id":101,"status":"shipped","running_count":1},{"order_id":4,"customer_id":101,"status":"pending","running_count":2},{"order_id":5,"customer_id":102,"status":"shipped","running_count":1}]
D[{"order_id":1,"customer_id":101,"status":"shipped","running_count":1},{"order_id":2,"customer_id":102,"status":"pending","running_count":1},{"order_id":3,"customer_id":101,"status":"shipped","running_count":2},{"order_id":4,"customer_id":101,"status":"pending","running_count":3},{"order_id":5,"customer_id":102,"status":"shipped","running_count":2}]
Attempts:
2 left
💡 Hint
COUNT(*) counts rows per customer_id up to current order_id in order.