YouTip LogoYouTip

Sqlite Limit Clause

# SQLite Limit Clause The SQLite **LIMIT** clause is used to limit the amount of data returned by a SELECT statement. ## Syntax The basic syntax of a SELECT statement with the LIMIT clause is as follows: SELECT column1, column2, columnN FROM table_name LIMIT Here is the syntax when the LIMIT clause is used with the OFFSET clause: SELECT column1, column2, columnN FROM table_name LIMIT OFFSET The above SQL statement selects data from columns like column1, column2, etc., in the table_name table, and limits the number of rows returned and the starting row. This is typically used for paginated data display, where LIMIT controls the number of rows displayed per page, and OFFSET controls from which row to start displaying data. * `SELECT column1, column2, columnN`: This part specifies which columns to select from the database. `column1`, `column2`, and `columnN` are the names of the columns, which you can replace with actual column names. `columnN` indicates there may be more columns, with `N` representing a variable number of columns. * `FROM table_name`: This part specifies which table to query data from. `table_name` should be replaced with the actual table name. * `LIMIT `: This part is used to limit the number of rows in the query result. `` is a number representing the maximum number of rows you want to return. For example, if you write `LIMIT 10`, the query will return at most 10 rows of data. * `OFFSET `: This part is used to specify the starting point of the query result. `` is a number indicating from which row to start returning data. For example, if you write `OFFSET 5`, the query will skip the first 5 rows and start returning data from the 6th row. ## Examples Assume the COMPANY table has the following records: ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ----------1 Paul 32 California 20000.02 Allen 25 Texas 15000.03 Teddy 23 Norway 20000.04 Mark 25 Rich-Mond 65000.05 David 27 Texas 85000.06 Kim 22 South-Hall 45000.07 James 24 Houston 10000.0 Here is an example that limits the number of rows you want to fetch from the table: sqlite> SELECT * FROM COMPANY LIMIT 6; This will produce the following result: ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ----------1 Paul 32 California 20000.02 Allen 25 Texas 15000.03 Teddy 23 Norway 20000.04 Mark 25 Rich-Mond 65000.05 David 27 Texas 85000.06 Kim 22 South-Hall 45000.0 However, in some cases, you might need to fetch records starting from a specific offset. Here is an example that fetches 3 records starting from the third position: sqlite> SELECT * FROM COMPANY LIMIT 3 OFFSET 2; This will produce the following result: ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ----------3 Teddy 23 Norway 20000.04 Mark 25 Rich-Mond 65000.05 David 27 Texas 85000.0
← Sqlite Order BySqlite Glob Clause β†’