PostgreSQL – LIMIT with OFFSET clause
Last Updated :
30 Oct, 2024
The PostgreSQL LIMIT clause is a powerful feature that allows users to retrieve a specific subset of rows from query results. This optional clause can be paired with the OFFSET clause to skip a specified number of rows before returning the desired results. Such functionality is particularly beneficial for pagination, enabling us to fetch data in manageable chunks.
In this article, we will explain the PostgreSQL LIMIT and OFFSET clauses in-depth, with practical examples and outputs to illustrate their application.
PostgreSQL LIMIT with OFFSET Clause
The LIMIT clause restricts the number of rows returned by a PostgreSQL query. By specifying a number after the LIMIT keyword, we train PostgreSQL to return only that many rows. This is particularly advantageous when working with large tables where retrieving every row could lead to performance issues, increased loading times, and an overcrowded user interface.
Syntax
SELECT * FROM table LIMIT n OFFSET m;
Key Terms
- The LIMIT clause returns a subset of “n” rows from the query result.
- The OFFSET clause placed after the LIMIT clause skips “m” number of rows before returning the result query.
- If “m” is Zero, then it acts as a normal LIMIT clause.
PostgreSQL LIMIT with OFFSET Clause Examples
Now, let us examine a few practical examples of using the LIMIT and OFFSET clauses in PostgreSQL to better understand their application. PostgreSQL LIMIT with OFFSET clause examples provide valuable insights into how to efficiently manage large datasets and implement pagination in our queries
Example 1: Fetching Films with LIMIT and OFFSET
Here we will query for 5 films starting from the seventh one ordered by ‘film_id’ from the ‘film’ table of our sample database.
Query:
SELECT
film_id,
title,
release_year
FROM
film
ORDER BY
film_id
LIMIT 5 OFFSET 6;
Output
Explanation:
This query retrieves 5 films starting from the seventh record (offset 6) in the table, ordered by their ‘film_id'
.
Example 2: Fetching Films Ordered by Title in Descending Order
Here we will query for 5 films starting from the seventh one ordered by ‘film_id’ from the film table of our sample database in descending order of the film ‘title’.
Query:
SELECT
film_id,
title,
release_year
FROM
film
ORDER BY
title DESC
LIMIT 5 OFFSET 6;
Output
Explanation:
This query retrieves 5 films starting from the seventh record (offset 6) in the table, ordered by their ‘title'
in descending order.
Important Points About PostgreSQL LIMIT with OFFSET Clause
- The
LIMIT
clause restricts the number of rows returned by a query. The OFFSET
clause skips a specified number of rows before beginning to return rows from the query.
- An
OFFSET
of zero (OFFSET
0
) effectively ignores the OFFSET
clause, making the query act as a standard LIMIT
query.
LIMIT
and OFFSET
are commonly used for pagination in applications, allowing users to retrieve data in smaller, manageable chunks.
- An alternative to
LIMIT
and OFFSET
is keyset pagination, which uses a WHERE clause to filter rows based on a specific key.
- PostgreSQL also supports the
FETCH
clause as an alternative to LIMIT
, which can be more readable in some contexts.
Conclusion
Understanding the LIMIT and OFFSET clauses in PostgreSQL is important for effective data retrieval, especially when working with large datasets. By utilizing these clauses, you can implement efficient pagination strategies in your applications, enhancing the overall user experience. Whether we are fetching a limited number of results or skipping records, mastering the use of LIMIT and OFFSET can significantly improve our SQL querying capabilities.
Similar Reads
Python PostgreSQL - Limit Clause
In this article, we are going to see how to use the limit clause in PostgreSQL using pyscopg2 module in Python. In PostgreSQL LIMIT constraints the number of rows returned by the query. By default, It is used to display some specific number of rows from the top. If we want to skip a number of rows b
2 min read
PostgreSQL - LIMIT clause
The PostgreSQL LIMIT clause is a handy tool used to fetch a specific subset of rows returned by a query. This clause is optional and can be a powerful way to control the amount of data your query returns, especially when working with large datasets. Let us better understand the LIMIT Clause in Postg
2 min read
PostgreSQL - WITH Clause
The WITH clause in PostgreSQL, also known as a Common Table Expression (CTE), simplifies complex queries by breaking them into smaller, readable sections. It allows us to define temporary result sets that can be referenced later in our main query. This makes the PostgreSQL code easier to manage and
4 min read
PostgreSQL FETCH clause
The PostgreSQL FETCH clause is an essential feature for controlling and managing the number of rows returned in our SQL queries. It provides a standardized approach for limiting results, similar to the LIMIT clause but with more flexibility and compatibility across different database systems. This a
4 min read
PostgreSQL - WHERE clause
The PostgreSQL WHERE clause is a critical component of SQL queries, allowing users to filter records based on specified conditions. In this tutorial, we'll explore how the WHERE clause works in PostgreSQL, its integration with the SELECT statement, and various examples. By using the WHERE clause, we
6 min read
SQLite Limit clause
SQLite is the most popular and most used database engine which is written in c programming language. It is serverless and self-contained and used for developing embedded software devices like TVs, Mobile Phones, etc. In this article, we will be learning about the SQLite Limit Clause using examples s
5 min read
SQL | OFFSET-FETCH Clause
The OFFSET-FETCH clause in SQL is a powerful tool used for pagination, allowing users to retrieve a subset of rows from a result set. It is especially useful when dealing with large datasets, enabling smooth navigation through data by skipping a certain number of rows and fetching only the required
4 min read
Python SQLite - LIMIT Clause
In this article, we are going to discuss the LIMIT clause in SQLite using Python. But first, let's get a brief about the LIMIT clause. If there are many tuples satisfying the query conditions, it might be resourceful to view only a handful of them at a time. LIMIT keyword is used to limit the data g
2 min read
PostgreSQL - ORDER BY clause
The PostgreSQL ORDER BY clause is used to sort the result query set returned by the SELECT statement. As the query set returned by the SELECT statement has no specific order, one can use the ORDER BY clause in the SELECT statement to sort the results in the desired manner. Syntax: SELECT column_1, c
2 min read
PostgreSQL - HAVING clause
The HAVING clause in PostgreSQL is an essential feature for filtering grouped data that has been aggregated using functions like SUM(), COUNT(), AVG(), and others. Unlike the WHERE clause, which filters rows before aggregation, the HAVING clause is used to filter results after the grouping and aggre
4 min read