---
title: Python MySQL Order By
description: "## Sort the Result – Үр дүнг эрэмбэлэх Use the `ORDER BY` statement to sort the result in ascending or descending order. Үр дүнг дээш эсвэл доош эрэмбэлэхийн тулд `ORDER BY` мэдэгдлийг ашиглана. The `ORDER BY` keyword sorts the result…"
url: "https://apprentice.mn/docs/python-mysql-order-by/"
updated: "2024-08-05"
category: Python
---

# Python MySQL Order By

## Sort the Result – Үр дүнг эрэмбэлэх

Use the `ORDER BY` statement to sort the result in ascending or descending order.

Үр дүнг дээш эсвэл доош эрэмбэлэхийн тулд `ORDER BY` мэдэгдлийг ашиглана.

The `ORDER BY` keyword sorts the result ascending by default. To sort the result in descending order, use the `DESC` keyword.

`ORDER BY` түлхүүр үг нь үр дүнг өгөгдмөлөөр өсөх эрэмбээр эрэмбэлдэг. Үр дүнг буурах эрэмбээр эрэмбэлэхийн тулд `DESC` түлхүүр үгийг ашиглана.

#### Example – Жишээ

Sort the result alphabetically by name: result:

Үр дүнг нэрээр нь цагаан толгойн үсгийн дарааллаар эрэмбэлэх:

`import mysql.connectormydb = mysql.connector.connect(  host="localhost",  user="yourusername",  password="yourpassword",  database="mydatabase")mycursor = mydb.cursor()sql = "SELECT * FROM customers ORDER BY name"mycursor.execute(sql)myresult = mycursor.fetchall()for x in myresult:  print(x)`

## ORDER BY DESC

Use the `DESC` keyword to sort the result in a descending order.

Үр дүнг буурах эрэмбээр эрэмбэлэхийн тулд `DESC` түлхүүр үгийг ашиглана.

#### Example – Жишээ

Sort the result reverse alphabetically by name:

Үр дүнг нэрээр нь цагаан толгойн эсрэг эрэмбээр эрэмбэлэх:

`import mysql.connectormydb = mysql.connector.connect(  host="localhost",  user="yourusername",  password="yourpassword",  database="mydatabase")mycursor = mydb.cursor()sql = "SELECT * FROM customers ORDER BY name DESC"mycursor.execute(sql)myresult = mycursor.fetchall()for x in myresult:  print(x)`

               [https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-order-by%2F&linkname=Python%20MySQL%C2%A0Order%20By](https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-order-by%2F&linkname=Python%20MySQL%C2%A0Order%20By)[https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-order-by%2F&linkname=Python%20MySQL%C2%A0Order%20By](https://www.addtoany.com/add_to/mastodon?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-order-by%2F&linkname=Python%20MySQL%C2%A0Order%20By)[https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-order-by%2F&linkname=Python%20MySQL%C2%A0Order%20By](https://www.addtoany.com/add_to/email?linkurl=https%3A%2F%2Fapprentice.mn%2Fdocs%2Fpython-mysql-order-by%2F&linkname=Python%20MySQL%C2%A0Order%20By)[https://www.addtoany.com/share](https://www.addtoany.com/share)
