Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • PostgreSQL - Get Started
  • Install PostgreSQL
  • Connect to PostgreSQL DB
  • Create Database
  • Create Table
  • Copy Table
  • Drop Table
  • Drop Database
  • Truncate Table
  • ALTER Table
  • Rename Table
  • Rename Columns
  • Add Columns
  • Modify Column Type
  • Set Default Value of Column
  • Remove Columns
  • Add Constraints to Table
  • Insert Data
  • Upsert Data
  • Update Data
  • Delete Data
  • SELECT Statement
  • WHERE Clause
  • GROUP BY Clause
  • HAVING Clause
  • ORDER BY Clause
  • DISTINCT Clause
  • Inner Join
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join
  • Self Join
  • Natural Join
  • Cross Join
  • LIMIT OFFSET Clause
  • GROUPING SETS
  • GROUPING() Function
  • GROUP BY CUBE
  • GROUP BY ROLLUP
  • Sub Query
  • ALL Operator
  • ANY Operator
  • UNION Operator
  • INTERSECT Operator
  • EXCEPT Operator
  • IS NULL Operator
  • BETWEEN Operator
  • LIKE Operator
  • CAST Operator
  • CASE Expressions
  • NULLIF()
  • COALESCE()
  • GREATEST(), LEAST()
  • WITH Queries (CTE)
  • Constraints
  • NOT NULL Constraint
  • Unique Constraint
  • Check Constraint
  • Primary Key
  • Foreign Key
  • Sequence
  • Serial Type
  • Identity Columns
  • Generated Columns
  • Data Types
  • Boolean Type
  • Character Type
  • Integer Type
  • Numeric Type
  • Date Type
  • Time Type
  • TimeStamp Type
  • Interval Type
  • Array Type
  • Json Type
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

PostgreSQL: LIMIT and OFFSET

PostgresQL support LIMIT and OFFSET clauses in the SELECT query. The LIMIT clause is used to restrict the number of rows returned by a query. The OFFSET clause is used to skip the number of rows in the resultset of a query.

Syntax: LIMIT & OFFSET
SELECT <column_names>
FROM <table_name>
LIMIT <row_count> OFFSET <rows_to_skip>;

In the above syntax, row_count shows the number of rows to be returned from the query. If row_count is zero, the query will return an empty resultset. If row_count is null, the query will behave as if there is no LIMIT clause specified, meaning the query will return all rows from the resultset.

The OFFSET clause is used to skip the number of rows before returning the resultset. It can be used alone or can be used with the LIMIT clause. After the OFFSET keyword, specify rows_to_skip, so the query will skip the number of rows specified in rows_to_skip before returning the resultset. If the rows_to_skip is specified as zero, the query will behave as if there is no OFFSET clause specified.

Note: A table can have data stored in unspecified order, it is better to use LIMIT and OFFSET clauses along with ORDER BY clause to control the row order. If you do not use the ORDER BY clause, then LIMIT and OFFSET may return the result set in unspecified order of rows.

Let's use the following Employee table.

Demo Table

Let's use the LIMIT clause to fetch only the first 5 rows sorted by emp_id.

Example: LIMIT Clause
SELECT * FROM Employee
ORDER BY emp_id
LIMIT 5;

The above query will return the following result.

Now let's fetch 4 rows, after skipping the first 3 rows order by emp_id by using LIMIT and OFFSET clauses in the SELECT query.

Example: LIMIT and OFFSET
SELECT * FROM Employee
ORDER BY emp_id
LIMIT 4 OFFSET 3;

Normally, the LIMIT and OFFSET rows are used to get the first or last N rows. For example, to get the top 3 employees with the highest salary, sort the salary by descending order and use the LIMIT clause to get the first 3 employees.

Example: Get Top N Values
SELECT * FROM Employee
ORDER BY salary DESC
LIMIT 3;
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.