0
0
SQLquery~20 mins

Why window functions are needed in SQL - Challenge Your Understanding

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!
🧠 Conceptual
intermediate
2:00remaining
Why use window functions instead of GROUP BY?

Which of the following best explains why window functions are needed instead of just using GROUP BY?

AGROUP BY can only be used with numeric columns, but window functions work with all data types.
BWindow functions automatically create indexes to speed up queries, unlike GROUP BY.
CWindow functions allow calculations across rows without collapsing the result into grouped rows, preserving individual row details.
DWindow functions replace the need for JOINs in combining tables.
Attempts:
2 left
💡 Hint

Think about whether the original rows remain visible after aggregation.

query_result
intermediate
2:00remaining
Output of ROW_NUMBER() window function

Given a table sales with columns id, region, and amount, what is the output of this query?

SELECT id, region, amount, ROW_NUMBER() OVER (PARTITION BY region ORDER BY amount DESC) AS rank FROM sales;
SQL
CREATE TABLE sales (id INT, region VARCHAR(10), amount INT);
INSERT INTO sales VALUES (1, 'East', 100), (2, 'East', 200), (3, 'West', 150), (4, 'West', 50);
ARows numbered starting at 1 within each region, ordered by amount descending.
BRows numbered globally from 1 to total rows, ignoring region.
CRows numbered starting at 1 within each region, ordered by amount ascending.
DRows numbered randomly without any order.
Attempts:
2 left
💡 Hint

Look at the PARTITION BY and ORDER BY inside the OVER clause.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in window function usage

Which option contains a syntax error in using a window function?

SELECT id, SUM(amount) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM sales;
ASELECT id, SUM(amount) OVER (PARTITION BY region ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM sales;
BSELECT id, SUM(amount) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) FROM sales;
CSELECT id, SUM(amount) OVER (ORDER BY id ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING) FROM sales;
DSELECT id, SUM(amount) OVER (ORDER BY id ROWS BETWEEN UNBOUNDED FOLLOWING AND CURRENT ROW) FROM sales;
Attempts:
2 left
💡 Hint

Check the order of frame boundaries in the ROWS clause.

optimization
advanced
2:00remaining
Why prefer window functions over self-joins for running totals?

Which reason best explains why window functions are preferred over self-joins to calculate running totals?

AWindow functions are more efficient and easier to write than self-joins for cumulative calculations.
BSelf-joins cannot be used with ORDER BY clauses.
CWindow functions do not require indexes, while self-joins do.
DSelf-joins always produce incorrect results for running totals.
Attempts:
2 left
💡 Hint

Think about query performance and code simplicity.

🔧 Debug
expert
2:00remaining
Diagnose the error in this window function query

What error will this query produce?

SELECT id, region, amount, RANK() OVER (PARTITION BY region ORDER BY amount) AS rank FROM sales WHERE rank <= 2;
ARuntime error due to missing GROUP BY clause.
BSyntax error because window functions cannot be used in WHERE clause.
CNo error, query runs and filters rows with rank less than or equal to 2.
DError because RANK() requires a GROUP BY clause.
Attempts:
2 left
💡 Hint

Consider when window functions are evaluated in SQL query processing.