EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL LIMIT

By Priya PedamkarPriya Pedamkar

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL LIMIT

PostgreSQL LIMIT

Introduction to PostgreSQL LIMIT

PostgreSQL limit clause returns the number of rows from the table, which was mentioned in the limit value at the time of 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 number; 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)

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 are selecting 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 used to select 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 by using the order by and limit clause in PostgreSQL.
  • DESC: Fetch data in descending order by using the order by and limit clause in PostgreSQL.

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 is used to limit 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.
  • Using limit in order by clause, we can easily find the table’s top and bottom rows.
  • Limit clause returns no of rows from the table which was mentioned in limit value at the time of fetching record from a table.

Examples of PostgreSQL LIMIT

Given below are the examples of PostgreSQL LIMIT:

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 below example, we are retrieving data from all columns and specified rows by 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 are 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 below example, we are retrieving data from the specified column and specified rows by using the limit and offset clause.
  • In the below example, we are skipping three rows are 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; also, we 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

This is a guide to PostgreSQL LIMIT. Here we discuss the introduction, how the LIMIT clause works in PostgreSQL? and examples. You may also have a look at the following articles to learn more –

  1. PostgreSQL UNION ALL
  2. PostgreSQL Constraints
  3. PostgreSQL ORDER BY
  4. PostgreSQL Features
  5. Guide to PostgreSQL UNIQUE Constraint
  6. PostgreSQL Notify | How to Work?

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

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

Let’s Get Started

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

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

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

EDUCBA

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

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

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More