Let’s discuss the ins and outs of using the PostgreSQL LIMIT clause to limit the output to a specified value.

Although the LIMIT clause is an optional statement, it is very handy when you do not want a query to return thousands and thousands of records.

Basic Usage

The LIMIT clause is pretty straightforward. The general syntax for the query is:

The Limit value is an integer value that specifies the row limit for the output.

For example, if a query returns 100 records and you set the limit as 5, only 5 rows are displayed.

NOTE: Unless the query itself returns fewer values than those specified in the LIMIT clause, the limit value always sets the return value.

If the LIMIT value is set to 0, the query will return an empty set. On the other hand, if no value is set for the LIMIT clause (NULL), the query will return the same values as it does not have a LIMIT clause specified.

PostgreSQL Limit Clause Examples

Let us illustrate how to use the LIMIT clause by examples.

Example 1

The first example shows a simple usage of the LIMIT clause. In this example, we will be using the sample sakila database.

Consider the film table; we can determine the number of records using the query:

In this example, the table contains 1000 records. Dumping all the records may not be very efficient. Instead, we can limit the output to only 10 records as:

This command should return filtered information as shown in the table below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image2-31.png" data-lazy- height="443" src="data:image/svg xml,” width=”714″>

Example 2

In most cases, when limiting the output, it is good to order by a specific column. Doing this allows you to get an understanding of the values returned.

For example, we can order the output in the previous example by the film_id.

An example query is below:

In this example, the results are organized by film_id in ascending order—unlike the previous example where the result was in random order.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image4-26.png" data-lazy- height="289" src="data:image/svg xml,” width=”509″>

Example 3

There is a friend to the LIMIT clause. The OFFSET clause is pretty similar to the LIMIT clause but only skips the specified number of rows.

For example, to start the result from the 100 row, we can set the offset as:

In this example, the result starts from the 100th row forward.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image3-27.png" data-lazy- height="365" src="data:image/svg xml,” width=”519″>

Similarly, it is good to order the results to follow a specific order.

Example 4

You can also use the LIMIT and OFFSET clauses in the same query. For example, consider the query below:

The order of the LIMIT and OFFSET is irrelevant as the OFFSET gets evaluated first then the LIMIT clause.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image6-24.png" data-lazy- height="289" src="data:image/svg xml,” width=”508″>

Example 5

As mentioned, you can use the LIMIT 0, which returns no records. However, it does return the columns as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image5-23.png" data-lazy- height="238" src="data:image/svg xml,” width=”871″>

Example 6

You can also use the LIMIT clause in conjunction with the ORDER BY to get a specific number of last elements.

For example, to get the last 5 elements in a table, we can set the ORDER BY clause to be DESC and LIMIT the value to 5 as:

In this example, we should get the 5 last elements as:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/image1-31.png" data-lazy- height="190" src="data:image/svg xml,” width=”500″>

Conclusion

In this quick guide, we discussed how the LIMIT and OFFSET clause works. We also used a few examples to illustrate how you can harness its features to get customized results.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/john-150×150.png61245ed4a3f30.jpg" height="112" src="data:image/svg xml,” width=”112″>

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