Python MySQL Order By
In this page:
cursor.execute("SELECT * FROM table_name ORDER BY column ASC") # or DESC
Ascending में Sorting (Default)
cursor.execute("SELECT * FROM customers ORDER BY name") customer rows को name से A से Z तक alphabetically sorted return करता है -- ascending order implicit default है, इसलिए ASC को explicitly लिखने की ज़रूरत नहीं, हालाँकि clarity के लिए लिखा जा सकता है।
उदाहरण: Sorting Ascending (the Default)
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers ORDER BY name") # ascending is the implicit default
print("Sorted A to Z by default")
Descending में Sorting
ORDER BY में column name के बाद DESC जोड़ना sort को descending order में पलट देता है -- ORDER BY name DESC alphabetically Z से A तक sort करता है, और ORDER BY price DESC सबसे महंगे items सबसे पहले दिखाएगा।
उदाहरण: Sorting Descending
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers ORDER BY name DESC") # DESC reverses the sort order
print("Sorted Z to A")
कई Columns से Sorting
ORDER BY column1, column2 मुख्य रूप से column1 से sort करता है, और जिन rows का column1 value समान है, उन्हें tie-breaker के रूप में column2 से sort करता है -- बिल्कुल किसी spreadsheet को एक column से, फिर एक secondary column से sort करने जैसा।
उदाहरण: Sorting by Multiple Columns
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SELECT * FROM customers ORDER BY city, name") # name breaks ties within the same city
print("Sorted by city, then name breaks ties")
ORDER BY को WHERE के साथ मिलाना
ORDER BY को उसी query में WHERE clause के साथ मिलाया जा सकता है -- MySQL पहले WHERE filter लागू करता है यह तय करने के लिए कि कौन सी rows qualify करती हैं, फिर सिर्फ उस filtered subset को sort करता है, जो बाद में Python में filter करने से ज़्यादा efficient है।
उदाहरण: Combining ORDER BY with WHERE
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT * FROM customers WHERE city = %s ORDER BY name", # filtered first, then the subset is sorted
("Austin",),
)
print("Filtered first, then sorted")
किसी Expression या Alias से Sorting
ORDER BY सिर्फ plain column names तक सीमित नहीं है -- यह किसी computed expression से, या SELECT list में computed column को दिए गए alias से भी sort कर सकता है, जिससे आप ऐसी values से sort कर सकते हैं जो stored columns के रूप में मौजूद ही नहीं हैं।
उदाहरण: Sorting by an Expression or Alias
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"SELECT name, price * quantity AS total FROM orders ORDER BY total DESC" # sorts by the computed alias
)
print("Sorted by the computed 'total' alias")
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join