-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08_Order_by_clause.py
More file actions
30 lines (22 loc) · 908 Bytes
/
08_Order_by_clause.py
File metadata and controls
30 lines (22 loc) · 908 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
""" Order By 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."""
# Example: Order By clause in MySQL using Python
import mysql.connector
# Connect to the studentdb database
dataBase = mysql.connector.connect(
host ="localhost",
user ="root",
passwd ="Akhil@0109",
database = "studentdb"
)
# Create a cursor object
cursorObject = dataBase.cursor()
sql_query = "SELECT * FROM students ORDER BY marks DESC"
cursorObject.execute(sql_query)
myresult = cursorObject.fetchall()
print("🎓 Student Records:")
for x in myresult:
print(x)
# disconnecting from server
dataBase.close()