---Advertisement---

MySQL ORDER BY: How to Sort Query Results Using ASC & DESC Great 2025

By Manisha

Updated On:

---Advertisement---
MySQL ORDER BY

What is ORDER BY in MySQL?

MySQL ORDER BY: The ORDER BY clause in MySQL is used with the SELECT statement to sort query results in ascending (ASC) or descending (DESC) order. By default, MySQL sorts results in ascending order unless explicitly specified otherwise.


ORDER BY Syntax in MySQL

sql

SELECT column_name(s) FROM table_name 

[WHERE condition] 

ORDER BY column_name [ASC | DESC];

  • SELECT column_name(s) FROM table_name – Retrieves data from the specified table.
  • [WHERE condition] – Optional condition to filter results before sorting.
  • ORDER BY column_name – Specifies the column(s) used for sorting.
  • ASC | DESC – Defines sorting order (ASC for ascending, DESC for descending).

Understanding ASC and DESC in MySQL

Sorting TypeBehavior
ASC (Ascending)Default sorting order: Numbers from smallest to largest, Dates from oldest to newest, Text from A to Z
DESC (Descending)Reverse sorting order: Numbers from largest to smallest, Dates from newest to oldest, Text from Z to A

Example 1: Sorting by Date of Birth (Descending Order)

MySQL ORDER BY: The marketing team wants to sort members by birthdate (newest first) to send birthday greetings.

sql

SELECT * FROM members ORDER BY date_of_birth DESC;

membership_numberfull_namesdate_of_birth
3Robert Phil1989-07-12
9Howard Wolowitz1981-08-24
2Janet Smith Jones1980-06-23

Example 2: Sorting by Multiple Columns (Gender & Birthdate)

MySQL ORDER BY: To group members by gender (ascending order) while sorting birthdates in descending order within each gender:

sql

SELECT * FROM members ORDER BY gender, date_of_birth DESC;

First, all Female members are listed in descending birthdate order, followed by Male members.


Example 3: Sorting by Name in Ascending Order

MySQL ORDER BY: To display a list of members alphabetically by their full names:

sql

SELECT * FROM members ORDER BY full_names ASC;

membership_numberfull_names
Gloria Williams
Howard Wolowitz
Janet Jones

Why Use ORDER BY ASC & DESC?

Sorting Payment History (DESC) – Displays the latest transactions first.
Sorting Movie Categories (ASC) – Allows quick lookup of available movies.
Sorting Customer Records – Organizes large datasets for easy access.


Summary

✔MySQL ORDER BY: ORDER BY sorts query results in ascending (ASC) or descending (DESC) order.
✔ ASC is the default sorting order in MySQL.
✔ DESC is useful for sorting latest dates, highest values, or reverse text order.
✔ Multiple columns can be sorted simultaneously using ORDER BY column1 ASC, column2 DESC.

Using ORDER BY, you can efficiently organize and retrieve data from MySQL databases for better reporting and decision-making.

Click To Open

👉Tutorial-2: SQL GROUP BY HAVING
👉Tutorial-3: MYSQL Wild Cards
👉Tutorial-4: MYSQL REGEXP Tutorial

MySQL UPDATE Query

Download My SQL

Leave a Comment

Index