Updated May 4, 2023
Introduction to PostgreSQL LIMIT
The postgreSQL limit clause returns the number of rows from the table, which was mentioned in the limit value when fetching the record from the table. It is an optional clause of the PostgreSQL select statement, used to fetch a limited number of rows from the whole table; this clause is also used with the offset clause to fetch records from the table. We can use this clause with an order by clause to find ascending and descending numbers; it is the best way to find the top and bottom value of rows using a limit clause in PostgreSQL.
Syntax:
1. PostgreSQL Limit clause
Select column_name1, …, column_nameN from table_name LIMIT N (Number of rows count)
Select * (select all table columns) from table_name LIMIT N (Number of rows count)
2. Limit clause using offset clause
Select column_name1, …, column_nameN from table_name LIMIT N (Number of rows count) OFFSET N (Number of offset)
Select * (select all table columns) from table_name LIMIT N (Number of rows count) OFFSET N (Number of offset)
3. Using order by clause
Select column_name1, …, column_nameN from table_name ORDER BY column_name LIMIT N (Number of rows count)
Select column_name1, …, column_nameN from table_name ORDER BY column_name LIMIT N (Number of rows count) OFFSET N (Number of offset)
Select column_name1, …, column_nameN from table_name ORDER BY column_name DESC LIMIT N (Number of rows count)
Select column_name1, …, column_nameN from table_name ORDER BY column_name ASC LIMIT N (Number of rows count)
The parameter description of the above syntax is as follows:
- Select: Select statement is used to select no rows using the limit clause.
- Column_name1 to column_nameN: We select a column to fetch data from a table using the limit clause.
- From: Keyword used to select a specified table to fetch data using the limit clause.
- Table name: Table used to fetch a specified record from the table using the limit clause.
- Asterisk (*): Asterisk selects all columns from the table to fetch data.
- Limit N: A limit clause in PostgreSQL to select a specified number of rows from the table.
- Offset N: Offset clause used with limit clause fetch specified rows using offset value.
- Order by: Order by clause used in limit clause to fetch a record in ascending or descending order.
- ASC: Fetch data in ascending order using PostgreSQL’s order by and limit clause.
- DESC: Fetch data in descending order using PostgreSQL’s order by and limit clause.
How does the LIMIT clause work in PostgreSQL?
- This clause is an optional clause for a select statement. The Limit clause is essential in PostgreSQL.
We can use the limit clause by using the offset clause. - The offset clause will skip the N number of rows before returning the result.
- The limit limits the number of records to return from the table.
- The limit is an optional clause of the PostgreSQL select statement used to fetch a limited number of rows from the whole table.
- This clause is also used with an offset clause to fetch records from the table. We can use this clause with an order by clause to find ascending and descending numbers.
- We can easily find the table’s top and bottom rows using limit in order by clause.
- When fetching the record from a table, the limit clause returns the rows from the table mentioned in the limit value.
Examples
Below are some of the examples:
We have used an employee table to describe an example of a limit in PostgreSQL:
Example #1
Employee table to describe an example of a limit in PostgreSQL.
Code:
select * from employee;
Output:
Example #2
Example of limit by fetching data of all columns and specified number of rows from the table.
In the below example, we are fetching records from all columns and retrieving data only from three columns using the PostgreSQL limit.
Code:
select * from employee limit 3;
Output:
Example #3
Example of limit by fetching data of specified column and specified number of rows from the table.
In the below example, we are fetching records from specified columns and retrieving data only from four columns using the PostgreSQL limit.
Code:
select emp_id, emp_name, emp_address emp_salary from employee limit 4;
Output:
Example #4
Limit clause by using the offset clause to fetch data from all columns and specified rows.
- In the example below, we are retrieving data from all columns and specified rows using the limit and offset clause. The offset clause will skip the rows of the N offset number.
- In the below example, we are skipping three rows as follows.
Code:
select * from employee limit 3 offset 3;
Output:
Example #5
Limit clause by using the offset clause to fetch data from the specified column and specified rows.
- In the example below, we are retrieving data from the specified column and rows using the limit and offset clause.
- In the below example, we are skipping three rows as follows.
Code:
select emp_id, emp_name, emp_address emp_salary from employee limit 4 offset 3;
Output:
Example #6
Limit using order by clause.
1. Fetch the data in ascending order by using order by.
Code:
select emp_id, emp_name, emp_address emp_salary from employee order by emp_id ASC limit 4 offset 3;
Output:
2. Fetch the data in descending order by using order by.
Code:
select emp_id, emp_name, emp_address emp_salary from employee order by emp_id DESC limit 4 offset 3;
Output:
Conclusion
The PostgreSQL limit clause is essential in PostgreSQL to return a limited number of rows from the select queries. We have used a limit clause using offset in PostgreSQL; we also have to fetch data in ascending and descending order by using order by clause. It is an optional clause of a select statement.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL LIMIT” was beneficial to you. You can view EDUCBA’s recommended articles for more information.