What is the difference between fetch Next and fetch First in the Order By [...] OFFSET [..] FETCH [...] clause? What is the difference between fetch Next and fetch First in the Order By [...] OFFSET [..] FETCH [...] clause? sql sql

What is the difference between fetch Next and fetch First in the Order By [...] OFFSET [..] FETCH [...] clause?


FETCH FIRST and FETCH NEXT do exactly the same thing. The reason both exist because of the preceding OFFSET clause. Using the word FIRST combined with OFFSET can be confusing to a human reader:

SELECT * FROM FooORDER BY IDOFFSET 5 ROWSFETCH FIRST 5 ROWS ONLY; -- Does this mean rows 6 to 10, or 1 to 5?

Whereas:

SELECT * FROM FooORDER BY IDOFFSET 5 ROWSFETCH NEXT 5 ROWS ONLY; -- Now it's clear!

SqlFiddle example here.


There is no difference. The SQL standard simply allows both, maybe to bridge differences between syntaxes, or to allow you the choice to write 'fluent English' queries.

This similar as to why the standard allows you to write:

fetch first rowfetch first rowsfetch first 1 rowfetch first 1 rows

(and variants with next) which will all fetch a single row. That this also allows you to write grammatically incorrect sentences like fetch first 2 row is taken for granted.