Search Postgresql Archives

Re: Why LIMIT and OFFSET are commutative

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Andrus wrote:
I found that

SELECT * FROM foo
ORDER BY bar
OFFSET n
LIMIT m

and

SELECT * FROM foo
ORDER BY bar
LIMIT m
OFFSET n

produce always same results.

Why ?
OFFSET and LIMIT operations are NOT commutative in general.

Under what interpretation would the results differ?

<http://www.postgresql.org/docs/8.2/interactive/queries-limit.html>
If a limit count is given, no more than that many rows will be returned (but possibly less, if the query itself yields less rows).
...
OFFSET says to skip that many rows before beginning to return rows.

Why would the position of either clause matter, assuming the clause is in a legal position?

In both your examples, the LIMIT is 'm', so you will get 'm' rows.

In both your examples, the OFFSET is 'n', so you will skip 'n' rows before returning those 'm' rows.

I see no inconsistency.

Also, neither LIMIT nor OFFSET is a binary operator, so the term "commutative" has to be understood metaphorically at best. What exactly do you mean by "commutative"?

--
Lew
This post contained three requests for answers.

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux