---Advertisement---

MySQL ORDER BY: Sorting Query Results Using ASC & DESC (Full Guide) Great 2025

By Manisha

Updated On:

---Advertisement---
MySQL ORDER BY

MySQL ORDER BY: Sorting data in MySQL is essential for organizing query results in a meaningful way. The ORDER BY clause allows you to arrange data in ascending (ASC) or descending (DESC) order based on one or more columns. This tutorial will cover everything you need to know about sorting in MySQL, including practical examples.


Table of Contents

  1. What is ORDER BY in MySQL?
  2. ORDER BY Syntax in MySQL
  3. Sorting in Ascending Order (ASC)
  4. Sorting in Descending Order (DESC)
  5. Sorting by Multiple Columns
  6. Using ORDER BY with NULL Values
  7. Practical Examples
  8. Summary

1. What is ORDER BY in MySQL?

MySQL ORDER BY: The MySQL ORDER BY clause is used with the SELECT statement to sort the result set based on a specified column. It can be used to sort data in either ascending (ASC) or descending (DESC) order.

By default, MySQL returns results in the order the records were inserted into the database. Using ORDER BY, you can sort them in a structured way, making data easier to analyze and display.


2. ORDER BY Syntax in MySQL

The basic syntax for sorting query results in MySQL is:

sql

SELECT column_name(s) FROM table_name

[WHERE condition]

ORDER BY column_name [ASC | DESC];

  • SELECT column_name(s): Retrieves data from the table.
  • FROM table_name: Specifies the table to fetch data from.
  • [WHERE condition]: (Optional) Filters the result set before sorting.
  • ORDER BY column_name: Sorts data based on the specified column.
  • [ASC | DESC]: Defines whether the sorting is ascending or descending (default is ASC).

3. Sorting in Ascending Order (ASC)

MySQL ORDER BY: The ASC keyword sorts query results from lowest to highest (numbers) or A to Z (strings). If ASC is omitted, MySQL automatically assumes ascending order.

sql

SELECT * FROM members ORDER BY date_of_birth ASC;

Result:

membership_numberfull_namesdate_of_birth
2Janet Smith Jones23-06-1980
1Janet Jones21-07-1980
9Howard Wolowitz24-08-1981

Here, the oldest members appear first in the list.


4. Sorting in Descending Order (DESC)

MySQL ORDER BY: The DESC keyword sorts query results from highest to lowest (numbers) or Z to A (strings).

sql

SELECT * FROM members ORDER BY date_of_birth DESC;

Result:

membership_numberfull_namesdate_of_birth
3Robert Phil12-07-1989
4Gloria Williams14-02-1984
1Janet Jones21-07-1980

Now, the youngest members appear first.


5. Sorting by Multiple Columns

You can sort query results using multiple columns by separating them with commas.

sql

SELECT * FROM members ORDER BY gender ASC, date_of_birth DESC;

How it Works:

  1. First, it sorts by gender (A to Z, Female first, Male second).
  2. Then, within each gender, it sorts by date_of_birth in descending order (youngest first).

6. Using ORDER BY with NULL Values

MySQL treats NULL values as the smallest possible values when sorting in ascending order.

sql

SELECT * FROM members ORDER BY date_of_birth ASC;

How MySQL Sorts NULLs:

  • Ascending (ASC): NULL values appear first.
  • Descending (DESC): NULL values appear last.

7. Practical Examples of ORDER BY in MySQL

sql

MySQL ORDER BY: SELECT * FROM movies ORDER BY title ASC;

→ Movies will be listed from A to Z.

sql

SELECT * FROM payments ORDER BY payment_date DESC;

→ Latest payments appear first, helping businesses track recent transactions.

sql

SELECT * FROM members ORDER BY gender ASC, full_names ASC;

→ This will list all Female members first, followed by Male members, sorted by name.


8. Summary

✅ MySQL ORDER BY: MySQL ORDER BY is used to sort query results in ascending (ASC) or descending (DESC) order.
✅ The default sorting order is ascending (ASC) if no order is specified.
✅ You can sort by multiple columns by separating them with commas.
NULL values appear first in ASC order and last in DESC order.
✅ ORDER BY is useful for listing recent transactions, organizing search results, and displaying data meaningfully.

By mastering MySQL ORDER BY, you can efficiently organize query results to match business needs and user preferences!

Click To Open

👉Tutorial-2: SQL GROUP BY and HAVING CLAUSE
👉Tutorial-3: MYSQL WILDCARD Tutorial
👉Tutorial-4: MYSQL REGEX Tutorial
👉Tutorial-5: Complete Guide to MySQL Functions
👉Tutorial-6: MySQL Aggregate Functions

MYSQL Delete Query

Download My SQL

Leave a Comment

Index