English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PostgreSQL LIMIT Clause

In PostgreSQL limit The clause is used to limit the number of data queried in the SELECT statement.

Syntax

The basic syntax of a SELECT statement with LIMIT clause is as follows:

SELECT column1, column2, columnN
FROM table_name
LIMIT [no of rows]

The syntax when using LIMIT clause and OFFSET clause together is as follows:

SELECT column1, column2, columnN 
FROM table_name
LIMIT [no of rows] OFFSET [row num]

Online example

Create COMPANY table(Download COMPANY SQL file ),data content as follows:

w3codeboxdb# select * from COMPANY;
 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  1 | Paul  |  32 | California|  20000
  2 | Allen |  25 | Texas     |  15000
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
  6 | Kim   |  22 | South-Hall|  45000
  7 | James |  24 | Houston   |  10000
(7 rows)

The following example will find a specified number of data, that is, read 4 data items:

w3codeboxdb=# SELECT * FROM COMPANY LIMIT 4;

Get the following result:

 id | name  | age | address     | salary
----+-------+-----+-------------+--------
  1 | Paul  |  32 | California  |  20000
  2 | Allen |  25 | Texas       |  15000
  3 | Teddy |  23 | Norway      |  20000
  4 | Mark  |  25 | Rich-Mond   |  65000
(4 rows)

However, in some cases, it may be necessary to extract records starting from a specific offset.

The following example extracts starting from the third position 3 number of records:

w3codeboxdb=# SELECT * FROM COMPANY LIMIT 3 OFFSET 2;

Get the following result:

 id | name  | age | address   | salary
----+-------+-----+-----------+--------
  3 | Teddy |  23 | Norway    |  20000
  4 | Mark  |  25 | Rich-Mond |  65000
  5 | David |  27 | Texas     |  85000
(3 rows)