SQLite LIMIT Clause
To limit the number of records fetched from a table, the SQLite LIMIT clause is used with the SELECT command.
Syntax 1:
SELECT column1, column2, column_N FROM table_name LIMIT [no of rows]
Syntax 2: Using Offset
SELECT column1, column2, column_N FROM table_name LIMIT [no of rows] OFFSET [row num]
Example 1:
TEACHERS Table:
ID | NAME | AGE | SUBJECT |
1 | Jim | 27 | English |
2 | John | 30 | Geology |
3 | Watson | 28 | French |
4 | Holmes | 40 | Chemistry |
5 | Tony | 35 | Physics |
SELECT * FROM TEACHERS LIMIT 2; |
Output:
ID | NAME | AGE | SUBJECT |
1 | Jim | 27 | English |
2 | John | 30 | Geology |
Explanation:
In the above example, we are fetching records of the first two rows of the TEACHERS table.
Example2:
TEACHERS Table:
ID | NAME | AGE | SUBJECT |
1 | Jim | 27 | English |
2 | John | 30 | Geology |
3 | Watson | 28 | French |
4 | Holmes | 40 | Chemistry |
5 | Tony | 35 | Physics |
SELECT * FROM TEACHERS LIMIT 2 OFFSET 2; |
Output:
ID | NAME | AGE | SUBJECT |
3 | Watson | 28 | French |
4 | Holmes | 40 | Chemistry |
Explanation:
In the above example, we are fetching records of two rows of the TEACHERS table, starting from the third position (after the offset value which is 2).