Bird
0
0

Which window frame clause correctly computes this using RANGE?

hard📝 Application Q15 of 15
PostgreSQL - Window Functions in PostgreSQL
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?
ARANGE BETWEEN INTERVAL '7 days' PRECEDING AND CURRENT ROW
BRANGE BETWEEN INTERVAL '6 days' PRECEDING AND CURRENT ROW
CROWS BETWEEN 6 PRECEDING AND CURRENT ROW
DROWS BETWEEN 7 PRECEDING AND CURRENT ROW
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PostgreSQL Quizzes