YouTip LogoYouTip

Postgresql Limit

# PostgreSQL LIMIT Clause The **LIMIT** clause in PostgreSQL is used to restrict the number of rows returned by a SELECT query. ### Syntax The basic syntax of a SELECT statement with the LIMIT clause is as follows: SELECT column1, column2, columnN FROM table_name LIMIT Here is the syntax when using the LIMIT clause with the OFFSET clause: SELECT column1, column2, columnN FROM table_name LIMIT OFFSET ### Example Create the COMPANY table ((https://static.jyshare.com/download/company.sql)), with the following data: tutorialdb# 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 limit the number of records to fetch, i.e., read 4 records: tutorialdb=# SELECT * FROM COMPANY LIMIT 4; This will produce 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, you might need to fetch records starting from a specific offset. Here is an example to fetch 3 records starting from the third position: tutorialdb=# SELECT * FROM COMPANY LIMIT 3 OFFSET 2; This will produce 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)
← Cpp TemplatesPostgresql Delete β†’