Order by and Limit in MySQL with Python

This post is lesson 53 of 54 in the subject Python Programming Language

To learn this topic well, you need to know how to insert data into a table in MySQL with Python. You can read the article Insert and Delete data in MySQL with Python to learn how to insert data into a table in MySQL.

The examples of ordering data with order by and limiting data with limit will perform on the table gochocit_posts in the database gochocit. This database and table have been created in the article Creating a database and a table in MySQL with Python. You need to read it again to see the structure of the database and the data of the table.

table gochocit_post
table gochocit_post
inserted data in gochocit_posts
inserted data in gochocit_posts

1. Order by in MySQL with Python

The "ORDER BY" clause helps to sort the results of a SELECT query in ascending or descending order.

import mysql.connector
# create connection to MySQL
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database = "gochocit"
)
mycursor = mydb.cursor()

sql = "SELECT * FROM gochocit_posts ORDER BY ID"
mycursor.execute(sql)
myresult = mycursor.fetchall()

for x in myresult:
  print(x)
# disconnect from MySQL
mydb.close()

Result

(1, 'Vinh Lê', 'Đây là bài số 1', 'Nội dung bài số 1', datetime.datetime(2022, 3, 27, 19, 46, 5))
(2, 'Vinh Lê', 'Đây là bài số 2', 'Nội dung bài số 2', datetime.datetime(2022, 3, 27, 19, 47, 6))
(3, 'Vinh Lê', 'Đây là bài số 3', 'Nội dung bài số 3', datetime.datetime(2022, 3, 27, 19, 48, 7))
(4, 'Vinh Lê', 'Đây là bài số 4', 'Nội dung bài số 4', datetime.datetime(2022, 3, 27, 19, 49, 8))
(5, 'Vinh Lê', 'Đây là bài số 5', 'Nội dung bài số 5', datetime.datetime(2022, 3, 27, 19, 50, 9))

In the above example, we sort the results of the SELECT query in ascending order of the ID. By default, "ORDER BY" sorts the results in ascending order (ASC). To sort the results in descending order, use the keyword DESC.

import mysql.connector
# create connection to MySQL
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database = "gochocit"
)
mycursor = mydb.cursor()

sql = "SELECT * FROM gochocit_posts ORDER BY ID DESC"
mycursor.execute(sql)
myresult = mycursor.fetchall()

for x in myresult:
  print(x)
# disconnect from MySQL
mydb.close()

Result

(5, 'Vinh Lê', 'Đây là bài số 5', 'Nội dung bài số 5', datetime.datetime(2022, 3, 27, 19, 50, 9))
(4, 'Vinh Lê', 'Đây là bài số 4', 'Nội dung bài số 4', datetime.datetime(2022, 3, 27, 19, 49, 8))
(3, 'Vinh Lê', 'Đây là bài số 3', 'Nội dung bài số 3', datetime.datetime(2022, 3, 27, 19, 48, 7))
(2, 'Vinh Lê', 'Đây là bài số 2', 'Nội dung bài số 2', datetime.datetime(2022, 3, 27, 19, 47, 6))
(1, 'Vinh Lê', 'Đây là bài số 1', 'Nội dung bài số 1', datetime.datetime(2022, 3, 27, 19, 46, 5))

2. Limit in MySQL with Python

We can limit the number of rows returned by a query (SELECT) using the "LIMIT" command.

import mysql.connector
# create connection to MySQL
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database = "gochocit"
)
mycursor = mydb.cursor()

sql = "SELECT * FROM gochocit_posts LIMIT 3"
mycursor.execute(sql)
myresult = mycursor.fetchall()

for x in myresult:
  print(x)
# disconnect from MySQL
mydb.close()

Result

(1, 'Vinh Lê', 'Đây là bài số 1', 'Nội dung bài số 1', datetime.datetime(2022, 3, 27, 19, 46, 5))
(2, 'Vinh Lê', 'Đây là bài số 2', 'Nội dung bài số 2', datetime.datetime(2022, 3, 27, 19, 47, 6))
(3, 'Vinh Lê', 'Đây là bài số 3', 'Nội dung bài số 3', datetime.datetime(2022, 3, 27, 19, 48, 7))

In the above example, we retrieve the first 3 rows of data with LIMIT 3. We can select the starting row of data to retrieve using the "OFFSET" keyword.

import mysql.connector
# create connection to MySQL
mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="",
  database = "gochocit"
)
mycursor = mydb.cursor()

sql = "SELECT * FROM gochocit_posts LIMIT 3 OFFSET 2"
mycursor.execute(sql)
myresult = mycursor.fetchall()

for x in myresult:
  print(x)
# disconnect from MySQL
mydb.close()

Result

(3, 'Vinh Lê', 'Đây là bài số 3', 'Nội dung bài số 3', datetime.datetime(2022, 3, 27, 19, 48, 7))
(4, 'Vinh Lê', 'Đây là bài số 4', 'Nội dung bài số 4', datetime.datetime(2022, 3, 27, 19, 49, 8))
(5, 'Vinh Lê', 'Đây là bài số 5', 'Nội dung bài số 5', datetime.datetime(2022, 3, 27, 19, 50, 9))

In the above example, we retrieve the 3 rows of data starting from the 3rd row with LIMIT 3 OFFSET 2.

Rate this post
Previous and next lesson in subject<< Update and drop in MySQL with PythonJoin clause in MySQL with Python >>

Leave a Reply

Your email address will not be published. Required fields are marked *