The SQL ORDER BY Clause
The ORDER BY clause sorts the result set based on one or more columns. It is always placed at the end of a SELECT statement (before LIMIT if used). You learned the correct clause order in Chapter 2: SQL Syntax.
SELECT column1, column2
FROM table_name
ORDER BY column1 ASC|DESC;
ASC— Ascending order (A→Z, 1→100). This is the default.DESC— Descending order (Z→A, 100→1).
We will use this Employees table:
+----+-----------+------------+--------+-----------+
| ID | Name | Department | Salary | City |
+----+-----------+------------+--------+-----------+
| 1 | Priya K. | Sales | 55000 | Mumbai |
| 2 | John D. | Marketing | 62000 | London |
| 3 | Arun M. | IT | 78000 | Delhi |
| 4 | Emma W. | Sales | 51000 | Sydney |
| 5 | Ravi S. | IT | 72000 | Mumbai |
| 6 | Sara L. | HR | 58000 | New York |
+----+-----------+------------+--------+-----------+
Sort in Ascending Order (ASC)
Ascending is the default. Both queries below produce the same result:
SELECT Name, Salary
FROM Employees
ORDER BY Salary ASC;
+-----------+--------+ | Name | Salary | +-----------+--------+ | Emma W. | 51000 | | Priya K. | 55000 | | Sara L. | 58000 | | John D. | 62000 | | Ravi S. | 72000 | | Arun M. | 78000 | +-----------+--------+
Salaries are sorted from lowest to highest. Since ASC is the default, you can omit it: ORDER BY Salary gives the same result.
Sort in Descending Order (DESC)
SELECT Name, Salary
FROM Employees
ORDER BY Salary DESC;
+-----------+--------+ | Name | Salary | +-----------+--------+ | Arun M. | 78000 | | Ravi S. | 72000 | | John D. | 62000 | | Sara L. | 58000 | | Priya K. | 55000 | | Emma W. | 51000 | +-----------+--------+
Now the highest salary appears first. DESC must be written explicitly — it is never the default.
Sort by Text Column
ORDER BY works on text columns too. It sorts alphabetically:
SELECT Name, City
FROM Employees
ORDER BY City;
+-----------+-----------+ | Name | City | +-----------+-----------+ | Arun M. | Delhi | | John D. | London | | Priya K. | Mumbai | | Ravi S. | Mumbai | | Sara L. | New York | | Emma W. | Sydney | +-----------+-----------+
ORDER BY Multiple Columns
You can sort by multiple columns. SQL sorts by the first column, then uses the second column to break ties:
SELECT Name, Department, Salary
FROM Employees
ORDER BY Department ASC, Salary DESC;
+-----------+------------+--------+ | Name | Department | Salary | +-----------+------------+--------+ | Sara L. | HR | 58000 | | Arun M. | IT | 78000 | | Ravi S. | IT | 72000 | | John D. | Marketing | 62000 | | Priya K. | Sales | 55000 | | Emma W. | Sales | 51000 | +-----------+------------+--------+
Departments are sorted A→Z. Within each department, salaries are sorted highest first. Notice IT shows Arun (78K) before Ravi (72K), and Sales shows Priya (55K) before Emma (51K).
💡 Key Point
Each column in ORDER BY can have its own sort direction. ORDER BY Department ASC, Salary DESC sorts departments alphabetically but salaries highest-first within each department. You can mix ASC and DESC freely.
ORDER BY with WHERE
You can combine ORDER BY with WHERE to filter AND sort in the same query. WHERE always comes before ORDER BY. You can also add AND, OR, NOT operators for multiple conditions:
SELECT Name, Salary
FROM Employees
WHERE Salary > 55000
ORDER BY Salary DESC;
+-----------+--------+ | Name | Salary | +-----------+--------+ | Arun M. | 78000 | | Ravi S. | 72000 | | John D. | 62000 | | Sara L. | 58000 | +-----------+--------+
ORDER BY Column Position
Instead of column names, you can use the column number in the SELECT list. Column 1 is the first selected column, column 2 is the second, and so on:
SELECT Name, Salary
FROM Employees
ORDER BY 2 DESC;
ORDER BY 2 means “sort by the second column in the SELECT list” which is Salary. This produces the same result as ORDER BY Salary DESC.
📌 Best Practice
Always use column names instead of numbers in ORDER BY. Column numbers break if someone adds or removes columns from the SELECT list. Names are self-documenting and safer for production queries.
To limit the number of sorted rows returned, you can combine ORDER BY with SQL TOP / LIMIT / FETCH in Chapter 12. For a complete reference of the ORDER BY syntax, see the official MySQL ORDER BY documentation.
If you work with data regularly, explore our ready-made Dashboard Templates on NextGenTemplates that use SQL-powered data for business reporting.
📺 Visit our YouTube channel @NeoTechNavigators for step-by-step video tutorials and dashboard demos on this topic.
🧪 Try It Yourself
Write a query that shows employees sorted by City alphabetically, and within each city, by Name alphabetically.
SELECT Name, City, Department
FROM Employees
ORDER BY City ASC, Name ASC;
Expected Output:
+-----------+-----------+------------+ | Name | City | Department | +-----------+-----------+------------+ | Arun M. | Delhi | IT | | John D. | London | Marketing | | Priya K. | Mumbai | Sales | | Ravi S. | Mumbai | IT | | Sara L. | New York | HR | | Emma W. | Sydney | Sales | +-----------+-----------+------------+
Notice Mumbai has two employees — Priya comes before Ravi because the second sort column (Name) puts them in alphabetical order.
📝 What You Learned in This Chapter
ORDER BYsorts query results by one or more columnsASC= ascending (default) — smallest to largest, A to ZDESC= descending — largest to smallest, Z to A- You can sort by multiple columns — the second column breaks ties in the first
- Each column can have its own ASC or DESC direction
- ORDER BY always comes after WHERE (if WHERE is used)
- Use column names instead of numbers for clarity
Frequently Asked Questions
ORDER BY Salary without specifying ASC or DESC, SQL sorts from smallest to largest for numbers and A to Z for text. You only need to write DESC explicitly when you want reverse order.SELECT Name FROM Employees ORDER BY Salary even though Salary is not in the SELECT list. The database uses the column for sorting but does not display it in the output. This works in most database systems.NULLS FIRST or NULLS LAST in PostgreSQL to control this behavior explicitly. Learn more about NULLs in Chapter 9: SQL NULL Values.GROUP BY must come before ORDER BY. For example: SELECT Department, COUNT(*) AS Total FROM Employees GROUP BY Department ORDER BY Total DESC. This groups employees by department, then sorts departments by the count from highest to lowest.📖 Chapter 7 of 30 — SQL Tutorial on Neotech Navigators



