SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Tutorial / SQLite Limit

SQLite Limit

Summary: in this tutorial, you will learn how to use SQLite LIMIT clause to constrain the number of rows returned by a query.

Introduction to SQLite LIMIT clause

The LIMIT clause is an optional part of the  SELECT statement. You use the LIMIT clause to constrain the number of rows returned by the query.

For example, a SELECT statement may return one million rows. However, if you just need the first 10 rows in the result set, you can add the LIMIT clause to the SELECT statement to retrieve 10 rows.

The following illustrates the syntax of the LIMIT clause.

SELECT column_list FROM table LIMIT row_count;

The row_count is a positive integer that specifies the number of rows returned.

For example, to get the first 10 rows in the tracks table, you use the following statement:

SELECT trackId, name FROM tracks LIMIT 10;

Try It

SQLite LIMIT 10 tracks

If you want to get the first 10 rows starting from the 10th row of the result set, you use OFFSET keyword as the following:

SELECT column_list FROM table LIMIT row_count OFFSET offset;

Or you can use the following shorthand syntax of the LIMIT OFFSET clause:

SELECT column_list FROM table LIMIT offset, row_count;

For example, to get 10 rows starting from the 11th row in the tracks table, you use the following statement:

SELECT trackId, name FROM tracks LIMIT 10 OFFSET 10;

Try It

SQLite LIMIT 10 OFFSET 10 example

You often find the uses of OFFSET in web applications for paginating result sets.

SQLite LIMIT and ORDER BY clause

You should always use the LIMIT clause with the  ORDER BY clause. Because you want to get a number of rows in a specified order, not in an unspecified order.

The ORDER BY clause appears before the LIMIT clause in the SELECT statement. SQLite sorts the result set before getting the number of rows specified in the LIMIT clause.

SELECT column_list FROM table ORDER BY column_1 LIMIT row_count;

For example, to get the top 10 biggest tracks by size, you use the following query:

SELECT trackid, name, bytes FROM tracks ORDER BY bytes DESC LIMIT 10;

Try It

SQLite LIMIT Top 10 Largest Tracks

To get the 5 shortest tracks, you sort the tracks by the length specified by milliseconds column using ORDER BY clause and get the first 5 rows using LIMIT clause.

SELECT trackid, name, milliseconds FROM tracks ORDER BY milliseconds ASC LIMIT 5;

Try It

SQLite LIMIT 5 shortest tracks

Getting the nth highest and the lowest value

You can use the ORDER BY and LIMIT clauses to get the nth highest or lowest value rows. For example, you may want to know the second-longest track, the third smallest track, etc.

To do this, you use the following steps:

  1. First, use ORDER BY to sort the result set in ascending order in case you want to get the nth lowest value, or descending order if you want to get the nth highest value.
  2. Second, use the LIMIT OFFSET clause to get the nth highest or the nth lowest row.

The following statement returns the second-longest track in the tracks table.

SELECT trackid, name, milliseconds FROM tracks ORDER BY milliseconds DESC LIMIT 1 OFFSET 1;

Try It

SQLite LIMIT second longest track

The following statement gets the third smallest track on the tracks table.

SELECT trackid, name, bytes FROM tracks ORDER BY bytes LIMIT 1 OFFSET 2;

Try It

SQLite LIMIT third smallest track

In this tutorial, you have learned how to use SQLite LIMIT clause to constrain the number of rows returned by the query.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite Where
Next SQLite BETWEEN

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK constraints
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2021 SQLite Tutorial. All Rights Reserved.