Excellent question — and the answer depends on which window function you’re using, and whether you’re using ORDER BY
in the OVER()
clause.
Let’s clarify it fully:
✅ If You Use ORDER BY
Without ROWS BETWEEN
When you write something like:
SUM(sale_amount) OVER (
PARTITION BY employee_id
ORDER BY sale_date
)
You are implicitly using:
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
✅ So YES — it’s the same as using it explicitly.
🧪 What Happens Under the Hood
The default window frame depends on what you specify:
Clause Used | Interpreted As |
---|---|
ORDER BY (without ROWS BETWEEN ) | ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
No ORDER BY | RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING (entire partition) |
🧾 Example 1: With ORDER BY
SUM(sale_amount) OVER (
PARTITION BY employee_id
ORDER BY sale_date
)
→ Behaves like a running total
🧾 Example 2: Without ORDER BY
SUM(sale_amount) OVER (
PARTITION BY employee_id
)
→ Behaves like SUM() per group (same result for every row in the partition), because it considers all rows in the partition.
🧠 Summary
Situation | Window Frame Used |
---|---|
PARTITION BY + ORDER BY | ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW |
PARTITION BY only (no ORDER BY ) | RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING (entire group) |
Explicit ROWS BETWEEN clause | As specified |