Python MySQL ORDER BY

Sort your data - organize results in the order you want!

📊 Sort Your Data!

ORDER BY arranges your results - like organizing books alphabetically or by date!


import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="yourpassword",
  database="school"
)

cursor = mydb.cursor()
cursor.execute("SELECT name, age FROM students ORDER BY age")
results = cursor.fetchall()

for student in results:
  print(f"{student[0]} - {student[1]} years old")
                                
ORDER
Sort
ASC/DESC
Direction
Clean
Results

What is ORDER BY?

ORDER BY helps you organize your data:

  • 📚 Sort Results - Arrange data in a specific order
  • 🔤 Alphabetical - Sort text A to Z or Z to A
  • 🔢 Numerical - Sort numbers smallest to largest
  • 📅 By Date - Sort by oldest or newest first

Sort by Numbers (Age)

Sort Students by Age

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="yourpassword",
  database="school"
)

cursor = mydb.cursor()

# Sort from youngest to oldest
cursor.execute("SELECT name, age FROM students ORDER BY age")

results = cursor.fetchall()
print("👶 Students from youngest to oldest:")
for student in results:
    print(f"  {student[0]} - {student[1]} years old")

Sort in Reverse (DESC)

Sort from Oldest to Youngest

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="yourpassword",
  database="school"
)

cursor = mydb.cursor()

# Sort from oldest to youngest
cursor.execute("SELECT name, age FROM students ORDER BY age DESC")

results = cursor.fetchall()
print("👴 Students from oldest to youngest:")
for student in results:
    print(f"  {student[0]} - {student[1]} years old")

Sort by Text (Names)

Sort Names Alphabetically

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="yourpassword",
  database="school"
)

cursor = mydb.cursor()

# Sort names A to Z
cursor.execute("SELECT name, grade FROM students ORDER BY name")

results = cursor.fetchall()
print("🔤 Students in alphabetical order:")
for student in results:
    print(f"  {student[0]} ({student[1]})")

Sort by Multiple Things

Sort by Grade, then by Name

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="yourpassword",
  database="school"
)

cursor = mydb.cursor()

# First sort by grade, then by name within each grade
cursor.execute("SELECT name, grade, age FROM students ORDER BY grade, name")

results = cursor.fetchall()
print("📚 Students sorted by grade, then name:")
for student in results:
    print(f"  {student[1]}: {student[0]} ({student[2]} years old)")

Sort with Filtering

Filter and Sort Together

import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  password="yourpassword",
  database="school"
)

cursor = mydb.cursor()

# Find students older than 16 and sort by age
cursor.execute("SELECT name, age FROM students WHERE age > 16 ORDER BY age DESC")

results = cursor.fetchall()
print("🎓 Students older than 16, from oldest to youngest:")
for student in results:
    print(f"  {student[0]} - {student[1]} years old")

ORDER BY Quick Reference

ASC

Ascending (default)
A→Z, 1→10, oldest→newest

DESC

Descending (reverse)
Z→A, 10→1, newest→oldest

Multiple Columns

ORDER BY grade, name
Sort by grade first, then name

With WHERE

WHERE age > 16 ORDER BY age
Filter first, then sort

🧠 Test Your Knowledge

What does ORDER BY do?

What does DESC mean?