0
0
SQLquery~20 mins

Window function vs GROUP BY mental model in SQL - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Window Function vs GROUP BY Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Difference in output rows: GROUP BY vs Window Function

Consider a table sales with columns region, salesperson, and amount. Which statement best describes the difference in output rows when using GROUP BY region versus using SUM(amount) OVER (PARTITION BY region)?

AGROUP BY returns one row per region; window function returns one row per original row with aggregated values repeated.
BGROUP BY returns one row per salesperson; window function returns one row per region only.
CGROUP BY returns all rows unchanged; window function removes duplicates.
DGROUP BY and window function always return the same number of rows.
Attempts:
2 left
💡 Hint

Think about how GROUP BY collapses rows versus how window functions keep all rows.

query_result
intermediate
2:00remaining
Output rows count with GROUP BY vs window function

Given the table orders with 10 rows, what is the number of rows returned by these queries?

1) SELECT region, SUM(amount) FROM orders GROUP BY region;
2) SELECT region, amount, SUM(amount) OVER (PARTITION BY region) FROM orders;
A1) 10 rows; 2) 10 rows
B1) 10 rows; 2) Number of distinct regions
C1) 1 row; 2) 10 rows
D1) Number of distinct regions; 2) 10 rows
Attempts:
2 left
💡 Hint

GROUP BY reduces rows to groups; window functions keep all rows.

📝 Syntax
advanced
2:00remaining
Correct use of window function syntax

Which SQL query correctly uses a window function to calculate the running total of amount ordered by order_date?

SQL
SELECT order_id, amount, SUM(amount) OVER (ORDER BY order_date) AS running_total FROM orders;
ASELECT order_id, amount, SUM(amount) PARTITION BY order_date FROM orders;
BSELECT order_id, amount, SUM(amount) OVER (ORDER BY order_date) AS running_total FROM orders;
CSELECT order_id, amount, SUM(amount) GROUP BY order_date FROM orders;
DSELECT order_id, amount, SUM(amount) OVER (PARTITION order_date) FROM orders;
Attempts:
2 left
💡 Hint

Window functions require OVER() with correct clauses like ORDER BY or PARTITION BY.

optimization
advanced
2:00remaining
Performance impact of GROUP BY vs window function

Which statement about performance is generally true when comparing GROUP BY and window functions on large datasets?

AGROUP BY usually reduces data early, often improving performance; window functions process all rows, which can be slower.
BWindow functions always run faster because they do not group data.
CGROUP BY and window functions have identical performance on large datasets.
DWindow functions reduce the number of rows, improving performance over GROUP BY.
Attempts:
2 left
💡 Hint

Think about how grouping reduces rows versus window functions keeping all rows.

🔧 Debug
expert
2:00remaining
Identify error in window function usage

What error will this query produce?

SELECT region, salesperson, SUM(amount) OVER () FROM sales GROUP BY region;
ANo error; query runs and returns grouped sums
BERROR: window function cannot be used with GROUP BY
CERROR: column "salesperson" must appear in the GROUP BY clause or be used in an aggregate function
DERROR: missing PARTITION BY clause in window function
Attempts:
2 left
💡 Hint

Check which columns are selected and how GROUP BY works with non-aggregated columns.