Python MySQL – Order By Clause

A connector is employed when we have to use MySQL with other programming languages. The work of MySQL-connector is to provide access to MySQL Driver to the required language. Thus, it generates a connection between the programming language and the MySQL Server.

OrderBy Clause

OrderBy is used to arrange the result set in either ascending or descending order. By default, it is always in ascending order unless “DESC” is mentioned, which arranges it in descending order.
“ASC” can also be used to explicitly arrange it in ascending order. But, it is generally not done this way since default already does that.

Syntax-

SELECT column1, column2
FROM table_name
ORDER BY column_name ASC|DESC;

The following programs will help you understand this better.
DATABASE IN USE:

python-order-by



Example 1: Program to arrange the data in ascending order by name

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# order by clause
  
  
import mysql.connector
  
# Connecting to the Database
mydb = mysql.connector.connect(
  host ='localhost',
  database ='College',
  user ='root',
  password = ''
)
  
cs = mydb.cursor()
  
# Order by clause
statement ="SELECT * FROM Student ORDER BY Name"
cs.execute(statement)
  
result_set = cs.fetchall()
  
for x in result_set:
    print(x)
      
# Disconnecting from the database
mydb.close()

chevron_right


Output:

python-mysql-order-by

Example 2: Arranging the database in descending order

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# order by clause
  
  
import mysql.connector
  
# Connecting to the Database
mydb = mysql.connector.connect(
  host ='localhost',
  database ='College',
  user ='root',
)
  
cs = mydb.cursor()
  
# Order by clause
statement ="SELECT * FROM Student ORDER BY Name DESC"
cs.execute(statement)
  
result_set = cs.fetchall()
  
for x in result_set:
    print(x)
    
# Disconnecting from the database  
mydb.close()

chevron_right


Output:

python-mysql-order-by-2

Example 3: Program to get namefrom the table, arranged in descending order by Roll no.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate
# order by clause
  
  
import mysql.connector
  
# Connecting to the Database
mydb = mysql.connector.connect(
  host ='localhost',
  database ='College',
  user ='root',
)
  
cs = mydb.cursor()
  
# Order by clause
statement ="SELECT Name FROM Student ORDER BY Roll_no DESC"
cs.execute(statement)
  
result_set = cs.fetchall()
  
for x in result_set:
    print(x)
      
# Disconnecting from the database
mydb.close()

chevron_right


Output:

python-mysql-order-by-3




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.