ORDER BY
Learn how to sort query results in ascending or descending order, and how to sort by more than one column at a time.
Introduction
By default, MySQL does not guarantee any particular order for the rows a query returns — they may come back in whatever order happens to be convenient for the database engine. When the order matters to you, the ORDER BY clause lets you control it explicitly.
In this lesson, you will learn how to sort results ascending or descending, and how to sort by multiple columns at once, using the same students table from previous lessons.
- The basic syntax of ORDER BY.
- The difference between ASC (ascending) and DESC (descending).
- How to sort by more than one column.
- A worked example sorting students by grade, then by name.
Basic ORDER BY Syntax
ORDER BY comes after WHERE (if there is one) and names the column to sort by.
SELECT column1, column2
FROM table_name
ORDER BY column1;Here is the students table we will sort in the examples below.
| id | name | age | grade | marks |
|---|---|---|---|---|
| 1 | Alex | 16 | A | 92 |
| 2 | Sam | 17 | B | 74 |
| 3 | Priya | 16 | A | 88 |
| 4 | Jordan | 18 | C | 61 |
| 5 | Maya | 17 | B | 79 |
Ascending Order (Default)
Ascending order means smallest to largest for numbers, or A-to-Z for text. It is the default, so ASC is optional — leaving it off has the exact same effect as writing it explicitly.
SELECT name, marks
FROM students
ORDER BY marks ASC;+--------+-------+
| name | marks |
+--------+-------+
| Jordan | 61 |
| Sam | 74 |
| Maya | 79 |
| Priya | 88 |
| Alex | 92 |
+--------+-------+Writing "ORDER BY marks" and "ORDER BY marks ASC" produce identical results. Most developers omit ASC and only write DESC explicitly, since ascending is assumed.
Descending Order
Descending order reverses the sort: largest to smallest, or Z-to-A. Add DESC right after the column name to use it.
SELECT name, marks
FROM students
ORDER BY marks DESC;+--------+-------+
| name | marks |
+--------+-------+
| Alex | 92 |
| Priya | 88 |
| Maya | 79 |
| Sam | 74 |
| Jordan | 61 |
+--------+-------+Sorting by Multiple Columns
You can sort by more than one column by listing them separated by commas. The first column is the primary sort; the second column only breaks ties within groups of equal values in the first column.
SELECT name, grade
FROM students
ORDER BY grade ASC, name ASC;+--------+-------+
| name | grade |
+--------+-------+
| Alex | A |
| Priya | A |
| Maya | B |
| Sam | B |
| Jordan | C |
+--------+-------+Notice that within grade A, Alex comes before Priya because they are additionally sorted alphabetically by name. The same happens within grade B for Maya and Sam.
Worked Example
Suppose a teacher wants a class list grouped by grade, with students inside each grade listed alphabetically by name — exactly the query from above, applied for a real purpose.
SELECT name, grade, marks
FROM students
ORDER BY grade ASC, name ASC;+--------+-------+-------+
| name | grade | marks |
+--------+-------+-------+
| Alex | A | 92 |
| Priya | A | 88 |
| Maya | B | 79 |
| Sam | B | 74 |
| Jordan | C | 61 |
+--------+-------+-------+Each column can independently choose ASC or DESC. For instance, ORDER BY grade ASC, marks DESC would list grades alphabetically, but rank students within each grade from highest to lowest marks.
Common Mistakes
- Assuming rows come back in insertion order without an explicit ORDER BY — MySQL does not guarantee this.
- Forgetting that DESC only applies to the column it directly follows, not to every column in the list.
- Putting ORDER BY before WHERE — it must come after.
- Sorting by a column that was not selected, without realizing it is still valid — ORDER BY can reference any column in the table, not just selected ones.
Best Practices
- Add an explicit ORDER BY whenever row order matters to your application.
- Use DESC explicitly for descending order; leave ASC off since it is the default.
- Use multiple sort columns to break ties in a meaningful, predictable way.
- Put the most important sort criterion first in the ORDER BY list.
Frequently Asked Questions
Can I sort by a column I did not select?
Yes. ORDER BY can reference any column in the table, even one that does not appear in your SELECT list.
Can each column have its own sort direction?
Yes — write ASC or DESC directly after each column name; they are independent of one another.
Does ORDER BY change the table itself?
No. It only changes the order of the returned result; the underlying table data is untouched.
What order does text sort in by default?
Alphabetically, A to Z, though the exact rules depend on the column's collation settings.
Key Takeaways
- ORDER BY controls the order of rows in a result.
- ASC (ascending) is the default; DESC reverses it.
- Listing multiple columns sorts by the first, using later columns only to break ties.
- Without ORDER BY, row order is not guaranteed.
Summary
ORDER BY gives you full control over how query results are sequenced, whether by a single column or by several columns working together to break ties.
Next, you will learn how to limit the number of rows a query returns using LIMIT — useful for pagination and "top N" style queries.
- You can sort results in ascending or descending order.
- You can sort by multiple columns to break ties.
- You are ready to learn about limiting result size next.