SQL Tutorial

SQL ORDER BY Keyword – Sort Data in Ascending & Descending Order with Examples (2026-27)

The SQL ORDER BY keyword is used to sort the result set of a query by one or more columns — either in ascending (ASC) or descending (DESC) order. Without ORDER BY, SQL returns rows in no guaranteed sequence. Once you add ORDER BY, your results appear in a predictable, sorted order every time. In this step-by-step guide you will learn the ORDER BY syntax, see real working examples — sort by salary, by name, by multiple columns — and understand how to combine ORDER BY with WHERE and other clauses for powerful real-world queries.



✅ What is SQL ORDER BY?

✅ The SQL ORDER BY keyword sorts the result set returned by a SELECT query.

✅ By default (without ASC or DESC), SQL sorts in ascending order.

✅ You can sort by one or more columns, and each column can have its own sort direction.

💡 Real-world use: Imagine you query 1,000 employees. Without ORDER BY, rows appear in unpredictable order. With ORDER BY Salary DESC, the highest-paid employee always appears first — exactly what you want for a payroll report.

✅ SQL ORDER BY Syntax

SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC | DESC], column2 [ASC | DESC], ...;

SELECT:- Columns you want to retrieve.

FROM:- The table to query data from.

ORDER BY:- The column(s) to sort the result by.

ASC:- Ascending order — lowest to highest, A to Z. This is the default.

DESC:- Descending order — highest to lowest, Z to A.

Sample Employees Table:-

Employees
IDNameDepartmentSalary
1ANNIIT10000
2SUJANHR8000
3BOULTIT5000
4MEENASales12000
5RAHULHR9000

✅ Example – Sort by Salary (Ascending)

SELECT Query:-

SELECT * FROM Employees
ORDER BY Salary ASC;

Result:-

Result — Sorted by Salary Low to High
IDNameDepartmentSalary
3BOULTIT5000
2SUJANHR8000
5RAHULHR9000
1ANNIIT10000
4MEENASales12000
💡 Note: Writing ORDER BY Salary without ASC gives the same result — ASC is the default when no direction is specified.

✅ Example – Sort by Name (Descending)

SELECT Query:-

SELECT * FROM Employees
ORDER BY Name DESC;

Result:-

Result — Sorted by Name Z to A
IDNameDepartmentSalary
2SUJANHR8000
5RAHULHR9000
4MEENASales12000
3BOULTIT5000
1ANNIIT10000

✅ Example – ORDER BY Multiple Columns

You can sort by more than one column. SQL sorts by the first column first. If two rows have the same value in the first column, it uses the second column to break the tie.

SELECT Query:-

SELECT * FROM Employees
ORDER BY Department ASC, Salary DESC;

Result:-

Result — Sorted by Department A–Z, then Salary High–Low
IDNameDepartmentSalary
5RAHULHR9000
2SUJANHR8000
1ANNIIT10000
3BOULTIT5000
4MEENASales12000
ℹ️ Explanation: HR comes first alphabetically, IT second, Sales third. Within HR, RAHUL (9000) appears before SUJAN (8000) because of Salary DESC. Same logic applies within IT.

✅ Example – ORDER BY with WHERE

You can combine ORDER BY with WHERE to filter rows first, then sort the filtered result. Always place WHERE before ORDER BY.

SELECT Query:-

SELECT * FROM Employees
WHERE Department = 'IT'
ORDER BY Salary DESC;

Result:-

Result — IT Department Only, Sorted by Salary High to Low
IDNameDepartmentSalary
1ANNIIT10000
3BOULTIT5000
💡 Correct clause order: SELECT → FROM → WHERE → ORDER BY. Writing ORDER BY before WHERE causes a syntax error.

✅ Key Points to Remember

✅ ASC is the default — if you omit ASC/DESC, SQL sorts ascending automatically.

✅ ORDER BY goes at the end of the query, after WHERE, GROUP BY, and HAVING.

✅ Multiple columns — each column can have its own ASC or DESC direction.

✅ Text columns — ASC sorts A→Z, DESC sorts Z→A.

✅ Number columns — ASC sorts 1→100, DESC sorts 100→1.

✅ NULL values — in most databases, NULLs appear first in ASC order and last in DESC order.

⚠️ Performance note: ORDER BY on very large tables without an index can slow down queries significantly. Add an index on the column(s) you sort by most often in production.

✅ Common SQL ORDER BY Mistakes to Avoid

❌ Mistake 1: Placing ORDER BY before WHERE
Wrong: SELECT * FROM Employees ORDER BY Salary WHERE Department = 'IT';
Correct: SELECT * FROM Employees WHERE Department = 'IT' ORDER BY Salary;
❌ Mistake 2: Sorting by a column not in the SELECT list (with DISTINCT)
When using SELECT DISTINCT, you cannot ORDER BY a column that is not in your SELECT list. Always include the sort column in your SELECT when using DISTINCT.
❌ Mistake 3: Forgetting that ASC is the default
Writing ORDER BY Salary ASC and ORDER BY Salary produce identical results. Knowing this keeps your SQL cleaner and easier to read.
❌ Mistake 4: Using ORDER BY inside a subquery
In most databases, ORDER BY inside a subquery is ignored unless paired with LIMIT/TOP. Do not rely on subquery ordering — apply ORDER BY to the outer query only.

✅ Frequently Asked Questions (FAQ)

What does SQL ORDER BY do?
SQL ORDER BY sorts the result set of a query by one or more columns in ascending (ASC) or descending (DESC) order. Without ORDER BY, SQL returns rows in no guaranteed order.
What is the difference between ASC and DESC in SQL?
ASC (ascending) sorts from lowest to highest — A to Z for text, 1 to 100 for numbers. DESC (descending) sorts from highest to lowest — Z to A for text, 100 to 1 for numbers. ASC is the default if you omit the keyword.
Can I ORDER BY multiple columns in SQL?
Yes. Separate columns with commas: ORDER BY Department ASC, Salary DESC. SQL sorts by the first column first, then by the second column within each group of equal values in the first column.
Can I use ORDER BY with WHERE?
Yes. Always place WHERE before ORDER BY: SELECT * FROM Employees WHERE Department = 'IT' ORDER BY Salary DESC; — this filters rows first, then sorts the filtered result.
Does ORDER BY slow down SQL queries?
Sorting adds overhead, especially on large datasets without indexes. To improve performance, add an index on the ORDER BY column. Avoid unnecessary ORDER BY in subqueries where sort order is irrelevant.
What is the correct position of ORDER BY in a SQL query?
ORDER BY must come after WHERE, GROUP BY, and HAVING. The correct full clause order is: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT.

✍️ About the Author: Pramod Behera

Pramod Behera is a SAP and SQL educator with 10+ years of experience in enterprise software and database systems. He founded LearnToSAP.com to help beginners learn SAP and SQL concepts through clear, practical, real-world examples. His tutorials have helped thousands of students and IT professionals across India and beyond.