EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL LIMIT
 

PostgreSQL LIMIT

Priya Pedamkar
Article byPriya Pedamkar

Updated May 4, 2023

PostgreSQL LIMIT

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

postgreSQL LIMIT 1

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:

fetching data of all columns and specified number of rows

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:

fetching data of specified column and specified number of rows

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:

postgreSQL LIMIT 4

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:

postgreSQL LIMIT 5

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:

emp-salary from employe

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:

postgreSQL LIMIT 7

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.

  1. PostgreSQL UNION ALL
  2. Guide to PostgreSQL UNIQUE Constraint
  3. PostgreSQL Constraints
  4. PostgreSQL ORDER BY

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW