EDUCBA

EDUCBA

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

PostgreSQL OFFSET

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL OFFSET

PostgreSQL OFFSET

Introduction to PostgreSQL OFFSET

PostgreSQL offset is used to skip rows before returning a result of the query; suppose we have 100 records in the employee table, and we need to fetch the last 50 records from the table at that time we used to offset. The offset will skip the first 50 records and display the last 50 records as required. Offset is very important in PostgreSQL to skip rows before returning a result of a query. If we use offset value is zero, it will return the same value; the offset condition is not used in this type of scenario.

Syntax

Below is the syntax of the PostgreSQL offset is as follows.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax #1 – PostgreSQL offset clause

select column_name1, …, column_nameN from table_name OFFSET N (Number of rows that we have skipping in query result)

select * (select all table columns) from table_name OFFSET N (Number of rows that we have skipping in query result)

Syntax #2 – Offset clause using limit clause

select column_name1, …, column_nameN from table_name OFFSET N (Number of rows that we have skipping in query result) LIMIT N

select * (select all table columns) from table_name OFFSET N (Number of rows that we have skipping in query result) LIMIT N

Syntax #3 –  PostgreSQL Offset using order by clause

select column_name1, …, column_nameN from table_name ORDER BY column_name OFFSET N (Number of rows that we have skipping in query result)

select column_name1, …, column_nameN from table_name ORDER BY column_name OFFSET N (Number of rows that we have skipping in query result) LIMIT N

select column_name1, …, column_nameN from table_name ORDER BY column_name DESC OFFSET N (Number of rows that we have skipping in query result)

select column_name1, …, column_nameN from table_name ORDER BY column_name ASC OFFSET N (Number of rows that we have skipping in query result)

Parameter PostgreSQL OFFSET

Below is the parameter description of the above syntax are as follows:

  • Select: Select statement is used to select no rows using the offset clause.
  • Column_name1 to column_nameN: We have selected a column to fetch data from the table using the offset clause.
  • From: Keyword used to select the specified table to fetch data using the offset clause.
  • Table name: Table used to fetch a specified record from the table using an offset clause.
  • Asterisk (*): Asterisk used to select all columns from the table to fetch data.
  • Offset N: Offset clause used in PostgreSQL to skip the rows before returning date.
  • Limit N: The limit clause is used with the offset clause in PostgreSQL to select a specified number of rows from the table.
  • Order by: Order by clause used with offset clause to fetch a record in ascending or descending order.
  • ASC: Fetch data in ascending order by using order by an OFFSET clause in PostgreSQL.
  • DESC: Fetch data in descending order by using order by an OFFSET clause in PostgreSQL.

How does OFFSET Clause work in PostgreSQL?

  • Offset is used to skip rows before returning a result of the query.
  • Suppose we have 1000 records in the student table, and we need to fetch the last 100 records from the table at that time we used offset in PostgreSQL.
  • Offset will skip the first 900 records and display the last 100 records as required.
  • Offset is very important to skip rows before returning a result of a query.
  • If we have using offset value is zero, then it will return the same value; the offset condition is not used at this type of scenario in PostgreSQL.

Examples to Implement PostgreSQL OFFSET

Below is the example of implementing offset are as follows. We have using an employee table to describe the example of offset in PostgreSQL.

Example #1

Employee table to describe the example of offset in PostgreSQL.

Code:

testing=#  select * from employee;

Output:

PostgreSQL OFFSET - 1

Example #2

Example of offset by fetching data from all columns and skipping the first three columns. In the below example, we have fetching records from all columns and skipping the first three rows using offset.

Code:

testing=# select * from employee offset 3;

Output:

PostgreSQL OFFSET - 2

Example #3

Example of offset by fetching data from a specified column and skipping the first four rows. In the below example, we have fetching records from specified columns and retrieving data only from four columns using PostgreSQL limits.

Code:

testing=# select emp_id, emp_name, emp_address emp_salary from employee OFFSET 4;

Output:

PostgreSQL OFFSET - 3

Example #4

offset clause by using a limit clause to fetch two records and skipping the first three rows. In the below example, we have to retrieve data from all columns and skipping the first three rows. We have used a limit clause with the offset clause.

Code:

testing=# select * from employee offset 3 limit 2;

Output:

PostgreSQL OFFSET - 4

Example #5

Offset clause by using a limit clause to fetch data from the specified column and skipping the first three rows. In the below example, we have retrieved data from the specified column and skipped the first three rows using the limit and offset clause.

Code:

testing=# select emp_id, emp_name, emp_address emp_salary from employee OFFSET 3 LIMIT 4;

Output:

using a limit clause

Example #6 – Offset using order by clause

1. Fetch the data in ascending order by using order by.

Code:

testing=# select emp_id, emp_name, emp_address emp_salary from employee order by emp_id ASC OFFSET 4 LIMIT 3;

Output:

ascending order

2. Fetch the data in descending order by using order by.

Code:

testing=# select emp_id, emp_name, emp_address emp_salary from employee order by emp_id DESC OFFSET 4 LIMIT 3;

Output:

descending order

Advantages of using OFFSET in PostgreSQL

Below are the advantages of offset are as follows.

  • Using the offset clause, we can skip the rows before returning an output.
  • The offset clause is used with the limit clause.
  • The offset clause is used with an order by clause to fetch a record in ascending and descending order.
  • Offset is very important in PostgreSQL.
  • Offset clause is skip specified rows and return the result of the query.

Conclusion

PostgreSQL offset clause is essential in PostgreSQL to skip the number of rows before returning the query’s output. The offset clause is used with a limit clause to fetch the specific number of rows. Using order by clause, we can fetch data in ascending and descending order in the offset clause.

Recommended Articles

This is a guide to PostgreSQL OFFSET. Here we discuss syntax, parameter, advantages, examples to implement with proper codes and outputs. You can also go through our other related articles to learn more –

  1. PostgreSQL Constraints
  2. PostgreSQL Architecture
  3. PostgreSQL Cross Join
  4. Indexes in PostgreSQL
  5. How to Work PostgreSQL Alias?
  6. PostgreSQL LIMIT | How to Works?

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
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE 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
    • 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 Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • 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 Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • 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 cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • 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 JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • 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 WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • 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

© 2020 - 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
Book Your One Instructor : One Learner Free Class

Let’s Get Started

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
EDUCBA Login

Forgot Password?

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

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