codingstreets
Search
Close this search box.
python-mysql-order-by

Get Started: Python MySQL Order By

In This Article, You Will Know About Python MySQL Order By.

Python MySQL Order By – Before moving ahead, let’s know a bit about Python MySQL Where.

Table of Contents

Sort the Result

ORDER BY

Use the ‘ORDER BY’ statement to sort the result in the ascending or descending order.

By default, ‘ORDER BY’ statement sorts the result in ascending order. Use the “DESC” statement to sort the result in descending order.

Example: Use “ORDER BY” statement to sort the “section” column in ascending order.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM Stu_information ORDER BY section")
for result in database_for_mysql:
  print(result)

				
			
python-mysql-order-by

As a result, it returned all column sorted by “section”.

Example: Use “ORDER BY” statement to sort the “class” column from “roll_no” column in ascending order.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT class FROM Stu_information ORDER BY roll_no")
for result in database_for_mysql:
  print(result)

				
			
python-mysql-order-by

As a result, it returned “roll_no” column sorted by “class”.

DESC

Use the “DESC” statement to sort the result in descending order.

Example: Use “DESC” statement to sort the “section” column in descending order.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT * FROM Stu_information ORDER BY section DESC")
for result in database_for_mysql:
  print(result)


				
			
python-mysql-order-by

As a result, it returned all column sorted by “section”.

Example: Use “DESC” statement to sort the “class” column from “roll_no” column in descending order.

				
					import mysql.connector
my_user_details = mysql.connector.connect(host="localhost",
                                          username="your_MySQL_username",
                                          password="Your_MySQL_password",
                                          database="YOUR_MYSQL_DATABASE_NAME")
database_for_mysql = my_user_details.cursor()

database_for_mysql.execute("SELECT class FROM Stu_information ORDER BY roll_no DESC")
for result in database_for_mysql:
  print(result)

				
			
python-mysql-order-by

As a result, it returned “class” column sorted by “roll_no”.

If you find anything incorrect in the above-discussed topic and have further questions, please comment below.

Connect on:

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on