EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL LIMIT

PostgreSQL LIMIT

By Priya PedamkarPriya Pedamkar

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.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
PROGRAMMING LANGUAGES Course
502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Certification Course
57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System - Design & Develop an IOT System
65+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Certification Course
19+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
POSTGRESQL Certification Course
 17+ Hour of HD Videos
4 Courses
1 Mock Tests & Quizzes
  Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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
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
Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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 Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more