SQL Standard

SQL Limit

When we are working in an SQL database, we may come across instances where we need to fetch a specific subset of rows from a given table. This allows us to limit the resources needed to fetch the values from the table.

Luckily, in SQL, we have access to the LIMIT clause which allows us to control the number of rows that are returned within a given result set. This feature is very useful when we are dealing with a large data set and do not actually need the entire row but instead a subset of it. This can be either to get the layout of the data or presentation.

In this tutorial, we will learn how we can use and work with the LIMIT clause in SQL databases to specify the number of rows that we wish to retrieve from the database.


Before we dive into the workings and usage of the LIMIT clause in SQL, let us discuss some basic requirements for this tutorial.

To follow along with this post, you will require the following:

  1. MySQL 8.0 and above will work with MySQL 5
  2. Access to the Sakila sample database
  3. Permissions to query the rows from the target database (read access)

With the given requirements met, we can proceed with this tutorial.


In SQL, the LIMIT clause allows us to restrict the number of rows that are returned from a given SQL Query. For example, in a SELECT statement, instead of returning all the rows from the table, which may contain over 1000 records, we can choose to view only the first 10 rows.

The following shows the basic syntax of the LIMIT clause in SQL:

SELECT col1, col2, ...

FROM tbl_name

LIMIT num_rows;

In this example, we use the LIMIT clause in conjunction with the SELECT statement.

From the given syntax, the “tbl_name” represents the name of the table from which we wish to retrieve the data.

The “num_rows” allows us to specify the maximum number of rows that are returned in the result set.

Example 1: Limit the Number of Rows

The most common and fundamental role of the LIMIT clause is to set the maximum number of rows that are included in the result set.

Suppose we want to use the “film” table from the Sakila sample database. However, since we do not want to fetch all the associated rows, we can select the first 10 rows as shown in the following example clause:



Once we run the previous query, we should get an output as follows:

A screenshot of a computer Description automatically generated

In this example, we use the LIMIT clause to restrict the result set to 10 rows. This fetches the first 10 rows from the result.

Example 2: Using the OFFSET Value

In some cases, we may wish to skip or omit a specific number of rows. For example, suppose we wish to retrieve only five elements but we want to start at position 20. We can use the OFFSET parameter which allows us to tell the LIMIT clause at which position we wish to start.

This is particularly useful when you need to implement the pagination in a large dataset as shown in the following example:

SELECT film_id, title, release_year, `length` FROM film


This should return 10 rows which starts from position 20 as follows:

As you can see from the given result, the “film_id” starts at position 21 and proceeds to position 30.

Example 3: Using the Order BY Clause

Another common use of the LIMIT clause is in combination with the ORDER BY clause. This allows us to retrieve specific number of rows that are based on a particular order. This can include basic sorting (ascending or descending), etc.

For example, suppose we want to retrieve the top 10 longest films from the “film” table. We can use the ORDER BY clause to sort the values based on the length and then limit the first 10 rows.

An example is as follows:

SELECT f.film_id, f.title, f.length

FROM film f



In this case, we use the ORDER BY clause to order the rows in descending order (highest to lowest) and then fetch the first 10 rows using the LIMIT clause.

The resulting result set is as follows:

A table of a list Description automatically generated with medium confidence

There you have it!


In this post, we learned the fundamentals and advanced features of working with the LIMIT clause in SQL databases.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list