Bird
Raised Fist0
PostgreSQLquery~20 mins

Window frame (ROWS BETWEEN, RANGE BETWEEN) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Window Frame Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Output of ROWS BETWEEN window frame
Given the table sales with columns id, region, and amount, what is the output of the following query?
SELECT id, region, amount,
SUM(amount) OVER (PARTITION BY region ORDER BY id ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS running_sum
FROM sales
ORDER BY id;
PostgreSQL
CREATE TABLE sales (id INT, region TEXT, amount INT);
INSERT INTO sales VALUES
(1, 'North', 100),
(2, 'North', 200),
(3, 'South', 150),
(4, 'North', 50),
(5, 'South', 100);
A[{"id":1,"region":"North","amount":100,"running_sum":100},{"id":2,"region":"North","amount":200,"running_sum":300},{"id":3,"region":"South","amount":150,"running_sum":150},{"id":4,"region":"North","amount":50,"running_sum":350},{"id":5,"region":"South","amount":250,"running_sum":250}]
B[{"id":1,"region":"North","amount":100,"running_sum":100},{"id":2,"region":"North","amount":200,"running_sum":300},{"id":3,"region":"South","amount":150,"running_sum":150},{"id":4,"region":"North","amount":50,"running_sum":250},{"id":5,"region":"South","amount":100,"running_sum":250}]
C[{"id":1,"region":"North","amount":100,"running_sum":100},{"id":2,"region":"North","amount":200,"running_sum":200},{"id":3,"region":"South","amount":150,"running_sum":150},{"id":4,"region":"North","amount":50,"running_sum":50},{"id":5,"region":"South","amount":100,"running_sum":100}]
D[{"id":1,"region":"North","amount":100,"running_sum":100},{"id":2,"region":"North","amount":200,"running_sum":300},{"id":3,"region":"South","amount":150,"running_sum":250},{"id":4,"region":"North","amount":50,"running_sum":250},{"id":5,"region":"South","amount":100,"running_sum":350}]
Attempts:
2 left
💡 Hint
Remember that ROWS BETWEEN 1 PRECEDING AND CURRENT ROW includes the current row and the one before it within the same partition.
❓ query_result
intermediate
2:00remaining
Output of RANGE BETWEEN window frame
Using the same sales table, what is the output of this query?
SELECT id, region, amount,
SUM(amount) OVER (PARTITION BY region ORDER BY amount RANGE BETWEEN 50 PRECEDING AND CURRENT ROW) AS running_sum
FROM sales
ORDER BY id;
PostgreSQL
CREATE TABLE sales (id INT, region TEXT, amount INT);
INSERT INTO sales VALUES
(1, 'North', 100),
(2, 'North', 200),
(3, 'South', 150),
(4, 'North', 50),
(5, 'South', 100);
A[{"id":1,"region":"North","amount":100,"running_sum":100},{"id":2,"region":"North","amount":200,"running_sum":200},{"id":3,"region":"South","amount":150,"running_sum":150},{"id":4,"region":"North","amount":50,"running_sum":50},{"id":5,"region":"South","amount":100,"running_sum":100}]
B[{"id":1,"region":"North","amount":100,"running_sum":300},{"id":2,"region":"North","amount":200,"running_sum":200},{"id":3,"region":"South","amount":150,"running_sum":250},{"id":4,"region":"North","amount":50,"running_sum":150},{"id":5,"region":"South","amount":100,"running_sum":100}]
C[{"id":1,"region":"North","amount":100,"running_sum":150},{"id":2,"region":"North","amount":200,"running_sum":200},{"id":3,"region":"South","amount":150,"running_sum":250},{"id":4,"region":"North","amount":50,"running_sum":50},{"id":5,"region":"South","amount":100,"running_sum":100}]
D[{"id":1,"region":"North","amount":100,"running_sum":250},{"id":2,"region":"North","amount":200,"running_sum":200},{"id":3,"region":"South","amount":150,"running_sum":250},{"id":4,"region":"North","amount":50,"running_sum":150},{"id":5,"region":"South","amount":100,"running_sum":100}]
Attempts:
2 left
💡 Hint
RANGE BETWEEN considers the values of the ORDER BY column, not row positions.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in window frame clause
Which option contains a syntax error in the window frame clause of this query?
SELECT id, amount,
SUM(amount) OVER (ORDER BY id ROWS BETWEEN CURRENT ROW AND 1 PRECEDING) AS sum_amount
FROM sales;
AROWS BETWEEN 1 PRECEDING AND CURRENT ROW
BROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
CROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
DROWS BETWEEN CURRENT ROW AND 1 PRECEDING
Attempts:
2 left
💡 Hint
The frame boundaries must be in logical order: start before end.
❓ optimization
advanced
2:00remaining
Optimizing window frame for cumulative sum
Which window frame clause is the most efficient and correct to calculate a cumulative sum of amount ordered by id over the entire table?
SELECT id, amount,
SUM(amount) OVER (ORDER BY id ) AS cumulative_sum
FROM sales;
AROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
BRANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
CROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
DRANGE BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING
Attempts:
2 left
💡 Hint
Cumulative sum means summing all rows from the start up to the current row.
🧠 Conceptual
expert
2:30remaining
Effect of RANGE BETWEEN with non-numeric ORDER BY
Consider this query:
SELECT id, category, amount,
SUM(amount) OVER (PARTITION BY category ORDER BY category RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS sum_amount
FROM sales;

What will be the effect of using RANGE BETWEEN with ORDER BY category where category is a text column?
AIt will treat all rows in the partition as peers and sum all amounts, resulting in the same sum for every row in each category.
BIt will raise an error because RANGE frame requires numeric or date ORDER BY columns.
CIt will behave like ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, summing rows up to current row.
DIt will sum only the current row's amount because text ordering cannot define a range.
Attempts:
2 left
💡 Hint
RANGE frame treats peers with the same ORDER BY value as a group.

Practice

(1/5)
1. What does the ROWS BETWEEN clause specify in a window function?
easy
A. It defines the frame by counting rows relative to the current row position.
B. It defines the frame by counting rows based on the value range in the ORDER BY column.
C. It filters rows before applying the window function.
D. It sorts the rows before applying the window function.

Solution

  1. Step 1: Understand window frame types

    Window frames can be defined by position (ROWS) or by value range (RANGE).
  2. Step 2: Identify what ROWS BETWEEN does

    ROWS BETWEEN counts rows relative to the current row's position, not by values.
  3. Final Answer:

    It defines the frame by counting rows relative to the current row position. -> Option A
  4. Quick Check:

    ROWS BETWEEN = position-based frame [OK]
Hint: ROWS counts rows by position, RANGE counts by value range [OK]
Common Mistakes:
  • Confusing ROWS with RANGE
  • Thinking ROWS filters rows
  • Assuming ROWS sorts data
2. Which of the following is the correct syntax to specify a window frame using RANGE BETWEEN from 5 preceding to current row?
easy
A. RANGE BETWEEN 5 PRECEDING AND CURRENT ROW
B. RANGE BETWEEN CURRENT ROW AND 5 PRECEDING
C. ROWS BETWEEN 5 PRECEDING AND CURRENT ROW
D. ROWS BETWEEN CURRENT ROW AND 5 PRECEDING

Solution

  1. Step 1: Recall RANGE BETWEEN syntax

    The syntax is: RANGE BETWEEN <start> AND <end>, where start must be before end logically.
  2. Step 2: Check order of frame boundaries

    5 PRECEDING comes before CURRENT ROW, so 'RANGE BETWEEN 5 PRECEDING AND CURRENT ROW' is correct.
  3. Final Answer:

    RANGE BETWEEN 5 PRECEDING AND CURRENT ROW -> Option A
  4. Quick Check:

    Correct RANGE syntax = RANGE BETWEEN 5 PRECEDING AND CURRENT ROW [OK]
Hint: Frame boundaries go from earlier to later, e.g., 5 PRECEDING to CURRENT ROW [OK]
Common Mistakes:
  • Reversing frame boundaries order
  • Using ROWS syntax with RANGE
  • Omitting BETWEEN keyword
3. Given the table sales(date, amount) ordered by date, what will the following query return?
SELECT date, amount, SUM(amount) OVER (ORDER BY date RANGE BETWEEN INTERVAL '1 day' PRECEDING AND CURRENT ROW) AS running_sum FROM sales;
medium
A. Sum of amounts for the current row and 1 row before
B. Sum of all amounts up to the current row
C. Sum of amounts for the current date and the previous day
D. Sum of amounts for the current date only

Solution

  1. Step 1: Understand RANGE BETWEEN with INTERVAL

    RANGE BETWEEN INTERVAL '1 day' PRECEDING AND CURRENT ROW includes rows with date values within 1 day before current row's date.
  2. Step 2: Interpret the SUM window function

    SUM(amount) will add amounts for rows dated from 1 day before up to current date.
  3. Final Answer:

    Sum of amounts for the current date and the previous day -> Option C
  4. Quick Check:

    RANGE with INTERVAL sums by date range [OK]
Hint: RANGE with INTERVAL sums values in date/time range [OK]
Common Mistakes:
  • Confusing RANGE with ROWS counting
  • Assuming ROWS behavior for RANGE
  • Ignoring INTERVAL meaning
4. Identify the error in this window frame clause:
SUM(amount) OVER (ORDER BY date ROWS BETWEEN CURRENT ROW AND 3 PRECEDING)
medium
A. The clause is syntactically correct.
B. ROWS cannot be used with ORDER BY date.
C. SUM cannot be used as a window function.
D. The frame boundaries are reversed; '3 PRECEDING' should come before 'CURRENT ROW'.

Solution

  1. Step 1: Check frame boundary order

    Frame boundaries must go from earlier to later, so '3 PRECEDING' should be before 'CURRENT ROW'.
  2. Step 2: Validate syntax correctness

    Using ROWS with ORDER BY date and SUM is valid, so no other syntax errors.
  3. Final Answer:

    The frame boundaries are reversed; '3 PRECEDING' should come before 'CURRENT ROW'. -> Option D
  4. Quick Check:

    Frame boundaries order matters [OK]
Hint: Frame start must be before frame end [OK]
Common Mistakes:
  • Reversing frame boundaries
  • Thinking ROWS can't use ORDER BY
  • Misunderstanding window function usage
5. You want to calculate a moving average of sales amounts over the last 7 days including the current day, but your sales table has multiple sales per day. Which window frame clause correctly computes this using RANGE?
hard
A. RANGE BETWEEN INTERVAL '7 days' PRECEDING AND CURRENT ROW
B. RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW
C. ROWS BETWEEN 6 PRECEDING AND CURRENT ROW
D. ROWS BETWEEN 7 PRECEDING AND CURRENT ROW

Solution

  1. Step 1: Understand the requirement

    We want a 7-day moving average including current day, so the frame covers 6 days before plus current day (total 7 days).
  2. Step 2: Choose correct frame type

    Since multiple sales per day exist, using RANGE with INTERVAL ensures all rows within date range are included.
  3. Step 3: Select correct interval

    INTERVAL '6 days' PRECEDING to CURRENT ROW covers 7 days total (6 before + current).
  4. Final Answer:

    RANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW -> Option B
  5. Quick Check:

    Use RANGE with 6 days preceding for 7-day window [OK]
Hint: RANGE with INTERVAL covers value ranges, perfect for dates [OK]
Common Mistakes:
  • Using ROWS which counts rows, not days
  • Choosing wrong interval length
  • Confusing 6 days preceding with 7 days
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard