LIMIT Clause
The LIMIT clause can restrict the result set of the query to some maximum number of rows. If this clause specifies a value smaller than the number of qualifying rows, the query returns only a subset of the rows that satisfy the selection criteria.
The LIMIT clause is an extension to the ISO/ANSI standard for the SQL language. This syntax fragment is part of the SELECT statement.
Element | Description | Restrictions | Syntax |
---|---|---|---|
max | Integer (> 0) specifying maximum number of rows to return | If max > number of qualifying rows then all matching rows are returned | Literal Number |
max_var | Host variable or local SPL variable storing the value of max | Same as max; valid in prepared objects and in SPL routines | Language dependent |
Usage
The LIMIT clause specifies that the result set includes no more than max rows (or exactly max rows, if max is less than the number of qualifying rows). Any additional rows that satisfy the query selection criteria are not returned.
The following example sets max with a literal integer to retrieve at most 10 rows from table tab1:
SELECT a, b FROM tab1 LIMIT 10;
You can also use a host variable, or the value of an SPL input parameter in a local variable, to assign the value of max.
If the LIMIT clause follows the ORDER BY clause, the returned rows are sorted according to the ORDER BY specifications. Because query results are generally not retrieved in any particular sequence, using ORDER BY to constrain the order of rows can be useful in queries whose LIMIT clause returns only a subset of the qualifying rows.
SELECT name, salary FROM emp
ORDER BY salary DESC LIMIT 10;
SELECT *
FROM TABLE(MULTISET(SELECT * FROM employees
ORDER BY employee_id LIMIT 10 )) vt(x,y), tab2
WHERE tab2.id = vt.x;
SELECT a, b FROM tab1 LIMIT 10 UNION SELECT a, b FROM tab2;
Restrictions on the LIMIT clause
- In the definition of a view
- In nested SELECT statements
- In subqueries, except for subqueries that specify table expressions in the FROM clause
- In a singleton SELECT (where max = 1) within an SPL routine
- Where embedded SELECT statements are used as expressions.
Dependencies between the LIMIT clause and Projection clause
SELECT FIRST 5 c1, c2 FROM tab ORDER BY c3; SELECT c1, c2 FROM tab ORDER BY c3 LIMIT 5;
Whether or not a query that uses the LIMIT clause also specifies ORDER BY can affect which qualifying rows are in the result sent if the Projection clause includes the SKIP options. The SKIP option, which specifies an offset for the max qualifying rows that FIRST or LIMIT restricts, is valid only in the Projection clause, but its specification can affect which of the sorted rows the LIMIT clause includes in the query result.
SELECT SKIP 20 c1, c2 FROM tab ORDER BY c3 LIMIT 10;
SELECT * FROM (SELECT SKIP 2 col1 FROM tab1 WHERE col1 > 50 LIMIT 8);But because you cannot use FIRST as a synonym for LIMIT outside the Projection clause, the following query fails with a syntax error:
SELECT SKIP 20 c1, c2 FROM tab ORDER BY c3 FIRST 10;
SELECT FIRST 20 c1, c2 FROM tab ORDER BY c3 LIMIT 10; SELECT LIMIT 10 c1, c2 from tab ORDER BY c3 LIMIT 20;
SELECT LIMIT 20 c1, c2 FROM tab ORDER BY c4; SELECT c1, c2 FROM tab ORDER BY c4 LIMIT 20;
Besides the SKIP, FIRST, and LIMIT options, the DISTINCT and UNIQUE specifications of the Projection clause can also restrict query results to a subset of the qualifying rows, as their Projection clause topics explain.