SQL Window Functions: OVER Clause, PARTITION BY, ORDER BY, and Frames/Exclude the current row from a frame
Compare a row against all earlier rows only.
Section: OVER clause fundamentals
Exclude the current row from a frame
sql
sql
SELECT created_at,
amount,
AVG(amount) OVER (
ORDER BY created_at
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING
) AS avg_before_current
FROM orders;Explanation
This pattern is useful when you need a historical benchmark without including the current row in the average.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing OVER clause fundamentals
Count all rows without collapsing them
Return total row count on every row.
Assign row numbers inside each partition
Number rows in order within each customer.
Use RANGE for peer-aware frames
Group rows that share the same ordering value.