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

PostgreSQL - FETCH clause

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.

Syntax:

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.

Example:

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.

Comparison with LIMIT:

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.

  1. PostgreSQL FETCH Clause example:

    • Description: The FETCH clause is used to retrieve a specified number of rows from the result set.
    • Code:
      SELECT column1, column2 FROM table_name
      FETCH 5 ROWS ONLY;
      
  2. How to use FETCH Clause in PostgreSQL:

    • Description: Use the FETCH clause to limit the number of rows returned by a query.
    • Code:
      SELECT column1, column2 FROM table_name
      FETCH 10 ROWS ONLY;
      
  3. Limiting rows with FETCH in PostgreSQL:

    • Description: Limit the number of rows returned using the FETCH clause.
    • Code:
      SELECT column1, column2 FROM table_name
      FETCH 20 ROWS ONLY;
      
  4. OFFSET and FETCH in PostgreSQL:

    • Description: Use OFFSET to skip a certain number of rows and FETCH to retrieve a specified number of rows.
    • Code:
      SELECT column1, column2 FROM table_name
      OFFSET 5 FETCH 10 ROWS ONLY;
      
  5. Pagination using FETCH Clause in PostgreSQL:

    • Description: Implement pagination by combining OFFSET and FETCH clauses.
    • Code:
      SELECT column1, column2 FROM table_name
      OFFSET 20 FETCH 10 ROWS ONLY;
      
  6. FETCH FIRST vs. LIMIT in PostgreSQL:

    • Description: Both FETCH FIRST and LIMIT can be used to restrict the number of rows. FETCH FIRST is more flexible.
    • Code:
      SELECT column1, column2 FROM table_name
      ORDER BY column1
      FETCH FIRST 5 ROWS ONLY;
      
  7. Dynamic FETCH in PostgreSQL:

    • Description: Use dynamic SQL to create a flexible FETCH clause based on runtime conditions.
    • Code:
      EXECUTE 'SELECT column1, column2 FROM table_name FETCH ' || variable_count || ' ROWS ONLY';
      
  8. FETCH in combination with ORDER BY in PostgreSQL:

    • Description: Combine FETCH with ORDER BY to retrieve a specific set of ordered rows.
    • Code:
      SELECT column1, column2 FROM table_name
      ORDER BY column1
      FETCH 10 ROWS ONLY;
      
  9. Using FETCH with cursors in PostgreSQL:

    • Description: Use FETCH with cursors to retrieve a specified number of rows from a cursor.
    • Code:
      DECLARE my_cursor CURSOR FOR
      SELECT column1, column2 FROM table_name;
      
      OPEN my_cursor;
      FETCH 5 FROM my_cursor;
      
  10. FETCH NEXT in PostgreSQL:

    • Description: Use FETCH NEXT as an alternative syntax for retrieving rows.
    • Code:
      SELECT column1, column2 FROM table_name
      ORDER BY column1
      FETCH NEXT 10 ROWS ONLY;
      
  11. FETCH vs. OFFSET-FETCH in PostgreSQL:

    • Description: While FETCH is standalone, OFFSET-FETCH is used together to skip a certain number of rows and fetch the next ones.
    • Code:
      SELECT column1, column2 FROM table_name
      ORDER BY column1
      OFFSET 5 FETCH 10 ROWS ONLY;
      
  12. FETCH Clause with WHERE condition in PostgreSQL:

    • Description: Combine FETCH with WHERE to retrieve a specific set of rows that meet certain conditions.
    • Code:
      SELECT column1, column2 FROM table_name
      WHERE condition
      FETCH 10 ROWS ONLY;
      
  13. FETCH Clause in subqueries PostgreSQL:

    • Description: Use FETCH in subqueries to limit the number of rows returned from a subquery.
    • Code:
      SELECT column1, column2
      FROM (
        SELECT column1, column2 FROM table_name
        ORDER BY column1
        FETCH 10 ROWS ONLY
      ) AS subquery_alias;
      
  14. Common mistakes with FETCH in PostgreSQL:

    • Description: Common mistakes include forgetting to use ORDER BY with FETCH, improper use of OFFSET, and not considering the impact on performance when fetching large result sets.
    • Code (Avoiding common mistakes):
      -- 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;