PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, the FETCH
clause is used to limit the number of rows returned by a query, similar to the LIMIT
clause. It's often used in conjunction with the OFFSET
clause to implement pagination in applications.
The FETCH
clause is SQL standard compliant, whereas LIMIT
is more traditionally associated with PostgreSQL and some other databases. Some developers prefer using FETCH
over LIMIT
because it's more readable, especially when combined with OFFSET
.
SELECT column1, column2, ... FROM table_name OFFSET start FETCH { FIRST | NEXT } number_of_rows { ROW | ROWS } ONLY;
start
: Specifies the number of rows to skip before starting the fetch.number_of_rows
: The maximum number of rows to return.You can use either FIRST
or NEXT
with the FETCH
clause, and they can be used interchangeably without any difference in behavior.
Consider a table named employees
and you want to retrieve rows 11 to 20 from it. This can be achieved using the following query:
SELECT * FROM employees ORDER BY employee_id OFFSET 10 FETCH FIRST 10 ROWS ONLY;
This query will skip the first 10 rows and fetch the next 10 rows.
The equivalent query using the LIMIT
clause would be:
SELECT * FROM employees ORDER BY employee_id OFFSET 10 LIMIT 10;
While both FETCH
and LIMIT
can be used to achieve the same result in PostgreSQL, FETCH
is more standard across different SQL databases. If you're aiming for portability, FETCH
might be a better choice. Otherwise, LIMIT
is concise and very familiar to most PostgreSQL developers.
It's also worth noting that while LIMIT
can be used alone, FETCH
is often used in conjunction with OFFSET
, though it's not strictly necessary.
PostgreSQL FETCH Clause example:
FETCH
clause is used to retrieve a specified number of rows from the result set.SELECT column1, column2 FROM table_name FETCH 5 ROWS ONLY;
How to use FETCH Clause in PostgreSQL:
FETCH
clause to limit the number of rows returned by a query.SELECT column1, column2 FROM table_name FETCH 10 ROWS ONLY;
Limiting rows with FETCH in PostgreSQL:
FETCH
clause.SELECT column1, column2 FROM table_name FETCH 20 ROWS ONLY;
OFFSET and FETCH in PostgreSQL:
OFFSET
to skip a certain number of rows and FETCH
to retrieve a specified number of rows.SELECT column1, column2 FROM table_name OFFSET 5 FETCH 10 ROWS ONLY;
Pagination using FETCH Clause in PostgreSQL:
OFFSET
and FETCH
clauses.SELECT column1, column2 FROM table_name OFFSET 20 FETCH 10 ROWS ONLY;
FETCH FIRST vs. LIMIT in PostgreSQL:
FETCH FIRST
and LIMIT
can be used to restrict the number of rows. FETCH FIRST
is more flexible.SELECT column1, column2 FROM table_name ORDER BY column1 FETCH FIRST 5 ROWS ONLY;
Dynamic FETCH in PostgreSQL:
FETCH
clause based on runtime conditions.EXECUTE 'SELECT column1, column2 FROM table_name FETCH ' || variable_count || ' ROWS ONLY';
FETCH in combination with ORDER BY in PostgreSQL:
FETCH
with ORDER BY
to retrieve a specific set of ordered rows.SELECT column1, column2 FROM table_name ORDER BY column1 FETCH 10 ROWS ONLY;
Using FETCH with cursors in PostgreSQL:
FETCH
with cursors to retrieve a specified number of rows from a cursor.DECLARE my_cursor CURSOR FOR SELECT column1, column2 FROM table_name; OPEN my_cursor; FETCH 5 FROM my_cursor;
FETCH NEXT in PostgreSQL:
FETCH NEXT
as an alternative syntax for retrieving rows.SELECT column1, column2 FROM table_name ORDER BY column1 FETCH NEXT 10 ROWS ONLY;
FETCH vs. OFFSET-FETCH in PostgreSQL:
FETCH
is standalone, OFFSET-FETCH
is used together to skip a certain number of rows and fetch the next ones.SELECT column1, column2 FROM table_name ORDER BY column1 OFFSET 5 FETCH 10 ROWS ONLY;
FETCH Clause with WHERE condition in PostgreSQL:
FETCH
with WHERE
to retrieve a specific set of rows that meet certain conditions.SELECT column1, column2 FROM table_name WHERE condition FETCH 10 ROWS ONLY;
FETCH Clause in subqueries PostgreSQL:
FETCH
in subqueries to limit the number of rows returned from a subquery.SELECT column1, column2 FROM ( SELECT column1, column2 FROM table_name ORDER BY column1 FETCH 10 ROWS ONLY ) AS subquery_alias;
Common mistakes with FETCH in PostgreSQL:
ORDER BY
with FETCH
, improper use of OFFSET
, and not considering the impact on performance when fetching large result sets.-- Incorrect: Missing ORDER BY SELECT column1, column2 FROM table_name FETCH 10 ROWS ONLY; -- Correct: Adding ORDER BY SELECT column1, column2 FROM table_name ORDER BY column1 FETCH 10 ROWS ONLY;