Re: Why LIMIT and OFFSET are commutative

From: Lew <lew(at)lwsc(dot)ehost-services(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Why LIMIT and OFFSET are commutative
Date: 2007-11-25 17:23:09
Message-ID: yfCdnb-Xgs_jMtTanZ2dnUVZ_gWdnZ2d@comcast.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Lew 2007-11-25 17:24:49 Re: Error while compiling PostgreSQL with Java
Previous Message Kris Jurka 2007-11-25 17:18:44 Re: Error while compiling PostgreSQL with Java