Re: Why LIMIT and OFFSET are commutative - Mailing list pgsql-general

From Lew
Subject Re: Why LIMIT and OFFSET are commutative
Date
Msg-id yfCdnb-Xgs_jMtTanZ2dnUVZ_gWdnZ2d@comcast.com
Whole thread Raw
List pgsql-general
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.

pgsql-general by date:

Previous
From: Lew
Date:
Subject: Re: Error while compiling PostgreSQL with Java
Next
From: Lew
Date:
Subject: Re: \copy ... with null as '' csv doesn't get nulls