Menu Close

Python MySQL Order By Clause

python mysql order by

In this Python MySQL Order By tutorial, we are going to learn all about how to select records from the database table in ascending or descending order using Python MySQL Character using Python MySQL connector.
In the previous tutorial, we have seen a complete process to filter the result set returned by the SELECT statement.

Make sure you have already installed Python MySQL connector in your machine using pip as well as create a database and table.

Python MySQL Order By

Python MySQL ORDER BY clause is used to sort the result set returned by the SELECT statement. ORDER BY clause is capable of sorting the result set in ascending or descending order.

Example:


import mysql.connector
mydb = mysql.connector.connect(

	host="localhost",
	user="root",
	password="root21",
	database = "demodb",
        port = 3308
	
)
cursor = mydb.cursor()
cursor.execute('SELECT * FROM students ORDER BY name')
all_students = cursor.fetchall()
for record in all_students:
	print(record)

Output

(3, 'Alex', 32)
(5, 'Alex Peter', 12)
(6, 'Alsena', 45)
(7, 'Jaini', 56)
(2, 'John', 22)
(4, 'Peter', 12)
(1, 'Vishvajit', 12)

Note:- ORDER BY clause sort the result by default ascending order. To sort the result set in descending order, you have to use the DESC keyword.

SELECT Columns:

Here we will select only those students whose id is greater than 2 and less than 10 and also we will sort the result by name. To filter the id, here we will use the MySQL WHERE clause.

Example:


import mysql.connector
mydb = mysql.connector.connect(

	host="localhost",
	user="root",
	password="root21",
	database = "demodb",
        port = 3308
	
)
cursor = mydb.cursor()
cursor.execute('SELECT * FROM students WHERE id < 10 and id > 2 ORDER BY name')
all_students = cursor.fetchall()
for record in all_students:
	print(record)

Output:

(1, 'Vishvajit')
(2, 'John')
(3, 'Alex')
(4, 'Peter')
(5, 'Alex Peter')
(6, 'Alsena')
(7, 'Jaini')

Note:- Above credentials may be different according to your MySQL configuration. I am showing all the credentials for only testing purposes, It will not work in your case.

ORDER BY DESC:

You can use the DESC keyword to sort all the results in descending order.

Example:


import mysql.connector
mydb = mysql.connector.connect(

	host="localhost",
	user="root",
	password="root21",
	database = "demodb",
        port = 3308
	
)
cursor = mydb.cursor()
cursor.execute('SELECT * FROM students ORDER BY name DESC')
all_students = cursor.fetchall()
for record in all_students:
	print(record)

Output:

(1, 'Vishvajit', 12)
(8, 'Saini', 23)
(4, 'Peter', 12)
(9, 'Palak', 30)
(2, 'John', 22)
(7, 'Jaini', 56)
(10, 'Jain', 19)
(11, 'Jack', 29)
(6, 'Alsena', 45)
(5, 'Alex Peter', 12)
(3, 'Alex', 32)

Conclusion

In this Python MySQL ORDER BY tutorial, you have learned all about how to sort the result set in ascending or descending order returned by the MySQL SELECT statement.
Python MySQL connector is the best for connecting Python applications to the MySQL database and executes SQL queries. After creating of database successfully, you can perform a query on that particular database.

If this article is helpful for you, please share and keep visiting for further Python MySQL database tutorials.

More Python MySQL Tutorials


For more information:- Click Here

Python MySQL Drop Table Statement
Connect Python To MySQL Database

Related Posts