Bird
0
0

Given the table transactions(tx_date, value) ordered by tx_date, what does this query return?

medium📝 query result Q5 of 15
PostgreSQL - Window Functions in PostgreSQL
Given the table transactions(tx_date, value) ordered by tx_date, what does this query return?
SELECT tx_date, value, AVG(value) OVER (ORDER BY tx_date RANGE BETWEEN INTERVAL '2 days' PRECEDING AND CURRENT ROW) AS avg_2days FROM transactions;
AThe average of values from all rows with <code>tx_date</code> within 2 days before and including the current row's date
BThe average of values from the 2 rows preceding the current row and the current row
CThe average of values from all rows in the partition
DThe average of values from the current row only
Step-by-Step Solution
Solution:
  1. Step 1: Understand RANGE BETWEEN with INTERVAL

    The RANGE BETWEEN INTERVAL '2 days' PRECEDING AND CURRENT ROW includes all rows where tx_date is within 2 days before up to the current row's tx_date.
  2. Step 2: Differentiate from ROWS

    This is based on logical value ranges, not physical row counts.
  3. Final Answer:

    The average of values from all rows with tx_date within 2 days before and including the current row's date -> Option A
  4. Quick Check:

    RANGE uses value intervals, not row counts [OK]
Quick Trick: RANGE with INTERVAL filters by value range, not row count [OK]
Common Mistakes:
  • Confusing RANGE with ROWS
  • Assuming RANGE counts rows instead of value ranges
  • Misunderstanding INTERVAL usage in RANGE

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes