Python PostgreSQL - Limit Clause Last Updated : 17 Feb, 2023 Comments Improve Suggest changes Like Article Like Report In this article, we are going to see how to use the limit clause in PostgreSQL using pyscopg2 module in Python. In PostgreSQL LIMIT constraints the number of rows returned by the query. By default, It is used to display some specific number of rows from the top. If we want to skip a number of rows before returning the rows then we use the OFFSET clause after the LIMIT clause. Syntax without using OFFSET: SELECT select_list FROM table_name LIMIT no_of_rows; Syntax with using OFFSET: SELECT select_list FROM table_name LIMIT no_of_rows OFFSET rows_to_skip; Table Used: Here, we are using the product table for demonstration. Now let's use the limit in this table, for we will use will psycopg2 module to connect the PostgreSQL and execute the SQL query in the cursor.execute(query) object. Syntax: cursor.execute(sql_query); Example 1: Using a limit clause in Postgre using Python Python3 # importing psycopg2 import psycopg2 conn=psycopg2.connect( database="geeks", user="postgre", password="root", host="localhost", port="5432" ) # Creating a cursor object using the cursor() method cursor = conn.cursor() print("\ndisplaying five rows from top"); sql = '''SELECT * from products LIMIT 5 ''' # Executing the query cursor.execute(sql) # Fetching the data result = cursor.fetchall(); print(result) # Commit changes in the database conn.commit() # Closing the connection conn.close() Output: Postgre limit clauseExample 2: Using limit and offset clause in Postgre using Python Python3 # importing psycopg2 import psycopg2 conn=psycopg2.connect( database="geeks", user="postgre", password="root", host="localhost", port="5432" ) # Creating a cursor object using the cursor() method cursor = conn.cursor() print("displaying four rows after a particular offset"); sql1 = '''SELECT * from products LIMIT 4 OFFSET 5 ''' # Executing the query cursor.execute(sql1) # Fetching the data result1 = cursor.fetchall(); print(result1) # Commit changes in the database conn.commit() # Closing the connection conn.close() Output: Postgre limit clause with offset Comment More infoAdvertise with us Next Article Python PostgreSQL - Limit Clause A annulata2402 Follow Improve Article Tags : Python Practice Tags : python Similar Reads PostgreSQL - LIMIT clause The PostgreSQL LIMIT clause is a handy tool used to fetch a specific subset of rows returned by a query. This clause is optional and can be a powerful way to control the amount of data your query returns, especially when working with large datasets. Let us better understand the LIMIT Clause in Postg 2 min read Python MySQL - Limit Clause The Limit clause is used in SQL to control or limit the number of records in the result set returned from the query generated. By default, SQL gives out the required number of records starting from the top but it allows the use of OFFSET keyword. OFFSET allows you to start from a custom row and get 2 min read Python PostgreSQL - Where Clause In this article, we are going to see how to use the Where clause in PostgreSQL using Psycopg2 in Python. Where Clauses help us to easily deal with the databases. As we know we have a huge amount of data stored in our database, so extracting only useful and required information clauses is helpful. Th 2 min read PostgreSQL - LIMIT with OFFSET clause The PostgreSQL LIMIT clause is a powerful feature that allows users to retrieve a specific subset of rows from query results. This optional clause can be paired with the OFFSET clause to skip a specified number of rows before returning the desired results. Such functionality is particularly benefici 4 min read Python PostgreSQL - Delete Data In this article, we are going to see how to delete data in tables from PostgreSQL using pyscopg2 module in Python. In PostgreSQL, DELETE TABLE is used to delete the data in the existing table from the database. It removes table definition and all associated data, indexes, rules, triggers, and constr 2 min read Like