Latest web development tutorials

SQLite Limit clause

SQLiteLIMIT clause is used to limit the amount of data returned by the SELECT statement.

grammar

The basic syntax of a SELECT statement with LIMIT clause as follows:

SELECT column1, column2, columnN 
FROM table_name
LIMIT [no of rows]

Here is the syntax for LIMIT and OFFSET clause clause when used with:

SELECT column1, column2, columnN 
FROM table_name
LIMIT [no of rows] OFFSET [row num]

SQLite engine will return to the beginning of the next line until all lines given OFFSET until, as shown in the following last instance.

Examples

Suppose COMPANY table has the following records:

ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0
7 James 24 Houston 10000.0

Here is an example, it limits the number of lines that you want to extract from the table:

sqlite> SELECT * FROM COMPANY LIMIT 6;

This produces the following results:

ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
1 Paul 32 California 20000.0
2 Allen 25 Texas 15000.0
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0
6 Kim 22 South-Hall 45000.0

However, in some cases, it may require a specific offset from the beginning of the extraction record. Here is an example, to start extracting three records from the third place:

sqlite> SELECT * FROM COMPANY LIMIT 3 OFFSET 2;

This produces the following results:

ID NAME AGE ADDRESS SALARY
---------- ---------- ---------- ---------- ----------
3 Teddy 23 Norway 20000.0
4 Mark 25 Rich-Mond 65000.0
5 David 27 Texas 85000.0