39

Is there an ANSI SQL alternative to the MYSQL LIMIT keyword?

The LIMIT keyword limits the number of rows returned by a SELECT e.g:

SELECT * FROM People WHERE Age > 18 LIMIT 2;

returns 2 rows.

SELECT * FROM People WHERE Age > 18 LIMIT 10, 2;

returns 2 rows after the first 10.

Gary Willoughby
  • 50,926
  • 41
  • 133
  • 199
  • Um... Those of us who know Ansi SQL may be able to help you if you told us what the MySql LIMIT keyword did... – James Curran Feb 27 '09 at 15:07
  • 1
    James, SELECT * FROM table LIMIT x returns the first x results, and SELECT * FROM table LIMIT x, y returns y results with an offset of x. – Aistina Feb 27 '09 at 15:09
  • @Aistina: Huh. I didn't know the LIMIT x, y version -- very handy! – Ben Feb 27 '09 at 15:10
  • Anyone know if which database abstraction layers support this and which do not (and for which DBs)? Answer for any language... – Marcus Feb 27 '09 at 16:12
  • 1
    LIMIT [x, ]y also works on Postgres. – bobince Feb 27 '09 at 17:14
  • 6
    Worth pointing out that the LIMIT x, y form still has to go over all the earlier rows, so it's not usually much faster than the other syntax (and can still be very slow for high limits). – Paul McMillan Aug 20 '10 at 21:06
  • 2
    possible duplicate of [How universal is the LIMIT statement in SQL?](http://stackoverflow.com/questions/1528604/how-universal-is-the-limit-statement-in-sql) – Lukas Eder Nov 01 '14 at 12:46

7 Answers7

41

this shows the different ways:

-- DB2
select * from table fetch first 10 rows only 
-- Informix 
select first 10 * from table 
-- Microsoft SQL Server and Access 
select top 10 * from table 
-- MySQL and PostgreSQL 
select * from table limit 10 
-- Oracle 
select * from (select * from table) where rownum <= 10
senshin
  • 10,022
  • 7
  • 46
  • 59
jle
  • 9,316
  • 5
  • 48
  • 67
  • 8
    I've always thought it was ridiculous that Oracle makes you do a subselect to do it this way, but the rownum property is assigned prior to reordering with ORDER BY. – Powerlord Feb 27 '09 at 15:15
  • 'oraclese' has its pros and cons eh? I do like Oracle's join shorthand (+) for an outer join... – jle Feb 27 '09 at 15:22
  • 4
    +1 nice summary! SQLite supports LIMIT, like MySQL/PostgreSQL. InterBase/Firebird support SELECT FIRST and SKIP, like Informix. – Bill Karwin Feb 27 '09 at 18:03
27

Not in SQL:1999.

There are two possible approaches you can use in later standards, with generally low levels of support in today's DBMSs.

In SQL:2008 you can use the DB/2 syntax:

SELECT * FROM things
ORDER BY smell
FETCH FIRST n ROWS ONLY

This only works for “LIMIT n” and not the extended “LIMIT m, n” offset syntax. In SQL:2003 you can use window functions, which can support the extended syntax but is a super PITA:

SELECT * FROM (
    SELECT *, ROW_NUMBER() OVER (ORDER BY smell) AS rn,
    FROM things
)
WHERE rn<=n -- or rn BETWEEN m+1 AND m+n

You will more usually use the DBMS-specific methods today.

bobince
  • 528,062
  • 107
  • 651
  • 834
15

see also http://en.wikipedia.org/wiki/Select_(SQL)#FETCH_FIRST_clause

SELECT * FROM T LIMIT 10 OFFSET 20 -- Netezza, MySQL, PostgreSQL (also supports the standard, since version 8.4), SQLite, HSQLDB, H2

SELECT * from T WHERE ROWNUM <= 10 -- Oracle (also supports the standard, since Oracle8i)

SELECT FIRST 10 * from T -- Ingres

SELECT FIRST 10 * FROM T order by a -- Informix

SELECT SKIP 20 FIRST 10 * FROM T order by c, d -- Informix (row numbers are filtered after order by is evaluated. SKIP clause was introduced in a v10.00.xC4 fixpack)

SELECT TOP 10 * FROM T -- MS SQL Server, Sybase ASE, MS Access

SELECT TOP 10 START AT 20 * FROM T -- Sybase SQL Anywhere (also supports the standard, since version 9.0.1)

SELECT FIRST 10 SKIP 20 * FROM T -- Interbase, Firebird

SELECT * FROM T ROWS 20 TO 30 -- Firebird (since version 2.1)

SELECT * FROM T
WHERE ID_T > 10 FETCH FIRST 10 ROWS ONLY -- DB2

SELECT * FROM T
WHERE ID_T > 20 FETCH FIRST 10 ROWS ONLY -- DB2 (new rows are filtered after comparing with key column of table T)
Wilfred Hughes
  • 29,846
  • 15
  • 139
  • 192
groovehunter
  • 3,050
  • 7
  • 26
  • 38
4

I don't believe so. All the databases that I'm aware of use vendor-specific keywords for that functionality.

Jeremy DeGroot
  • 4,496
  • 2
  • 20
  • 21
1

HSQL/H2 uses LIMIT like MySQL

Manik Surtani
  • 396
  • 3
  • 12
1

Adding to @jle's answer:

  • SQLite supports LIMIT (MySQL/PostgreSQL)
  • InterBase/Firebird support SELECT FIRST and SKIP (like Informix)

Also see Emulate MySQL LIMIT clause in Microsoft SQL Server 2000

Community
  • 1
  • 1
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
0

Let me link here a related SO question, with a great answer by Lukas Eder and another good answer by bobince:

How universal is the LIMIT statement in SQL?

edit: A few more good reference links, worth to look at in similar cases:

Community
  • 1
  • 1
ジョージ
  • 1,476
  • 1
  • 22
  • 29