0
0
PostgreSQLquery~20 mins

Why window functions are powerful in PostgreSQL - 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!
query_result
intermediate
2:00remaining
Output of ROW_NUMBER() window function
Given a table employees with columns id, department, and salary, what is the output of this query?
SELECT id, department, salary, ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC) AS rank FROM employees;
PostgreSQL
CREATE TABLE employees (id INT, department TEXT, salary INT);
INSERT INTO employees VALUES
(1, 'Sales', 5000),
(2, 'Sales', 7000),
(3, 'HR', 4000),
(4, 'HR', 4500),
(5, 'IT', 6000);
A[{id:2, department:'Sales', salary:7000, rank:1}, {id:1, department:'Sales', salary:5000, rank:2}, {id:4, department:'HR', salary:4500, rank:1}, {id:3, department:'HR', salary:4000, rank:2}, {id:5, department:'IT', salary:6000, rank:1}]
B[{id:1, department:'Sales', salary:5000, rank:1}, {id:2, department:'Sales', salary:7000, rank:2}, {id:3, department:'HR', salary:4000, rank:1}, {id:4, department:'HR', salary:4500, rank:2}, {id:5, department:'IT', salary:6000, rank:1}]
C[{id:2, department:'Sales', salary:7000, rank:2}, {id:1, department:'Sales', salary:5000, rank:1}, {id:4, department:'HR', salary:4500, rank:2}, {id:3, department:'HR', salary:4000, rank:1}, {id:5, department:'IT', salary:6000, rank:1}]
D[{id:1, department:'Sales', salary:5000, rank:2}, {id:2, department:'Sales', salary:7000, rank:1}, {id:3, department:'HR', salary:4000, rank:2}, {id:4, department:'HR', salary:4500, rank:1}, {id:5, department:'IT', salary:6000, rank:1}]
Attempts:
2 left
💡 Hint
Remember that ROW_NUMBER() resets for each department and orders salaries descending.
🧠 Conceptual
intermediate
1:30remaining
Why use window functions instead of GROUP BY?
Which statement best explains why window functions are powerful compared to GROUP BY?
AWindow functions can only be used with numeric columns, unlike GROUP BY.
BWindow functions always run faster than GROUP BY queries.
CWindow functions allow calculations across rows without collapsing the result into fewer rows, preserving original row details.
DWindow functions replace the need for any JOIN operations.
Attempts:
2 left
💡 Hint
Think about whether the number of rows changes after using window functions.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in this window function query
What is wrong with this SQL query?
SELECT id, salary, RANK() OVER (ORDER salary DESC) AS rank FROM employees;
AThe alias 'rank' is a reserved keyword and cannot be used.
BMissing the keyword BY after ORDER in the OVER clause.
CRANK() cannot be used without PARTITION BY clause.
DThe table name 'employees' must be enclosed in quotes.
Attempts:
2 left
💡 Hint
Check the syntax of the ORDER BY clause inside the OVER() window function.
optimization
advanced
2:00remaining
Optimizing a query with window functions
You have a large sales table and want to calculate the running total of sales per region ordered by date. Which approach is most efficient?
AUse a correlated subquery to sum sales up to the current date for each row.
BCalculate running total in application code after fetching all rows.
CUse GROUP BY region and date, then join back to the original table.
DUse SUM(sales) OVER (PARTITION BY region ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW).
Attempts:
2 left
💡 Hint
Window functions can compute running totals efficiently inside the database.
🔧 Debug
expert
2:30remaining
Debugging unexpected results with window functions
A query uses ROW_NUMBER() OVER (ORDER BY salary) but returns unexpected row numbering when multiple employees have the same salary. What is the cause?
AROW_NUMBER() assigns unique numbers even if salaries are the same, so order among ties is arbitrary.
BThe query must include PARTITION BY salary to fix numbering.
CROW_NUMBER() cannot be used without a GROUP BY clause.
DThe ORDER BY clause inside OVER() must include employee id to break ties.
Attempts:
2 left
💡 Hint
Think about how ROW_NUMBER() handles ties in ordering.