← Back to Python Course | Chapter 15: Python & MySQL | Lesson 7 of 12

Python MySQL Order By

एक ORDER BY clause SELECT statement से return होने वाली rows को sort करता है, या तो ascending (default, या explicit ASC के साथ) या descending (DESC के साथ) -- sorting database server पर होती है इससे पहले कि results Python को भेजे जाएँ, जो आमतौर पर बाद में Python में sort करने से कहीं ज़्यादा efficient है।
Syntax
python
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 के लिए लिखा जा सकता है।

Note: जब भी row order आपके application के लिए मायने रखे, तो explicitly ORDER BY जोड़ें, भले ही current data अभी sorted जैसा दिख रहा हो -- incidental insertion order पर भरोसा न करें।
Warning: बिना किसी ORDER BY के, MySQL results में किसी specific row order की guarantee नहीं देता -- यह testing के दौरान consistent दिख सकता है फिर भी अलग conditions में बदल सकता है।

उदाहरण: Sorting Ascending (the Default)

python
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 सबसे पहले दिखाएगा।

Note: जब भी "most recent," "highest," या "newest first" style के results दिखाने हों, जैसे date से sorted orders की एक list, DESC इस्तेमाल करें।
Warning: DESC सिर्फ उस column पर लागू होता है जिसके ठीक बाद वह आता है -- multi-column ORDER BY में, अगर हर column को अलग तरीके से sort करना है तो हर एक को अपना ASC/DESC चाहिए।

उदाहरण: Sorting Descending

python
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 करने जैसा।

Note: जब भी अकेला primary sort key ties को arbitrary order में छोड़ देता हो, multi-column sorting इस्तेमाल करें, जैसे last name से, फिर एक ही surname वाले लोगों के लिए first name से sort करना।
Warning: Multi-column ORDER BY में हर column का अपना independent ASC या DESC direction हो सकता है -- सबका एक जैसा sort होना ज़रूरी नहीं।

उदाहरण: Sorting by Multiple Columns

python
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 है।

Note: हमेशा database level पर ही WHERE से filter करें, न कि सब कुछ fetch करके किसी Python loop में filter करें -- database को filtering और sorting दोनों करने देना network पर transfer होने वाला data कम करता है।
Warning: SQL statement में WHERE clause को ORDER BY से पहले आना चाहिए -- उनका क्रम उलटना एक syntax error है।

उदाहरण: Combining ORDER BY with WHERE

python
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 के रूप में मौजूद ही नहीं हैं।

Note: SELECT list में computed column को AS से एक स्पष्ट alias दें, फिर ORDER BY में उसी alias को reference करें, cleaner और ज़्यादा readable SQL के लिए।
Warning: ORDER BY में computed expression के alias को reference करना MySQL में काम करता है, लेकिन हर SQL database में universally portable नहीं है -- अगर code को बाद में दूसरे databases support करने पड़ें तो यह ध्यान में रखने लायक है।

उदाहरण: Sorting by an Expression or Alias

python
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")
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.