Friday, February 20, 2026
HomeLanguagesPython MariaDB – Limit Clause using PyMySQL

Python MariaDB – Limit Clause using PyMySQL

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 the OFFSET keyword. OFFSET allows you to start from a custom row and get the required number of result rows.

Syntax :

SELECT * FROM tablename LIMIT limit;
SELECT * FROM tablename LIMIT limit OFFSET offset;

The following programs will help you understand this better.

Database in use:

data

Example 1: program to display only 2 records

Python3




import pymysql
  
# Create a connection object
# IP address of the MySQL database server
Host = "localhost"  
  
# User name of the database server
User = "user"       
  
# Password for the database user
Password = ""           
  
database = "GFG"
  
conn  = pymysql.connect(host=Host, user=User, password=Password, database)
  
# Create a cursor object
cur  = conn.cursor()
  
  
query = f"SELECT price,PRODUCT_TYPE FROM PRODUCT WHERE price > 10000 LIMIT 2"
  
cur.execute(query)
  
rows = cur.fetchall()
for row in rows :
    print(row)
  
conn.close()


Output :

limit clause

Example 2: program to start from the second record and display the first two records

Python3




import pymysql
  
# Create a connection object
conn  = pymysql.connect('localhost', 'user', 'password', 'database')
  
# Create a cursor object
cur  = conn.cursor()
  
  
query = f"SELECT * FROM PRODUCT LIMIT 2 OFFSET 1"
  
cur.execute(query)
  
rows = cur.fetchall()
for row in rows :
    print(row)
  
conn.close()


Output :

limit clause

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS