Database.Middle.If i don’t use ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW, so i take all rows ?

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 UsedInterpreted As
ORDER BY (without ROWS BETWEEN)ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
No ORDER BYRANGE 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

SituationWindow Frame Used
PARTITION BY + ORDER BYROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
PARTITION BY only (no ORDER BY)RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING (entire group)
Explicit ROWS BETWEEN clauseAs specified
This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.