Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing
Back
  1. Home
  2. Interview prep
  3. ROWS vs RANGE

SQL · Window Functions

ROWS vs RANGE

Mediumsql-21
rowsrangeframe

Question

What is the difference between ROWS and RANGE in a window frame?

Solution

ROWS works with physical rows.

RANGE uses values in the ORDER BY expression, so peer rows with the same ordering value can land in the same frame.

For example:

ROWS BETWEEN 2 PRECEDING AND CURRENT ROW

means:

> Current row + previous two physical rows.

Handy for a rolling 3-row calculation.

Example:

AVG(sales) OVER (
    ORDER BY date
    ROWS BETWEEN 2 PRECEDING AND CURRENT ROW
)

🎯 Put this concept into practice

Solidify this answer with real hands-on interview drills in the browser studio.

Open related drill →
PreviousNext