PostgreSQL TOP 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 tables 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:

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:

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:

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:

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:

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:

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:

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

Video liên quan

Chủ Đề