ORDER BY
Sort results in ascending or descending order
✅ What You Will Learn
By default, SQL returns rows in whatever order the database stores them — which is often unpredictable. ORDER BY sorts the result set by one or more columns.
Sorting is essential for reports: top 10 customers by revenue, latest orders first, employees sorted alphabetically. In data analyst work, you will use ORDER BY in almost every query that produces a report.
📋 The orders table used in examples
| order_id | customer_name | product | amount | order_date |
|---|---|---|---|---|
| 1001 | Rahul Sharma | Laptop | 45000 | 2026-01-15 |
| 1002 | Priya Verma | Mobile Phone | 18000 | 2026-01-16 |
| 1003 | Amit Kumar | Headphones | 3500 | 2026-01-16 |
| 1004 | Sneha Kapoor | Laptop | 52000 | 2026-01-17 |
| 1005 | Vikram Singh | Tablet | 28000 | 2026-01-18 |
Syntax
Examples
📌 Key Points to Remember
- ✓ASC = ascending (A–Z, 0–9, oldest–newest) — this is the default
- ✓DESC = descending (Z–A, 9–0, newest–oldest)
- ✓You can ORDER BY multiple columns — separate them with commas
- ✓ORDER BY goes after WHERE in the query
- ✓You can ORDER BY a column that is not in your SELECT list
🏢 Real-World Application
Every leaderboard, ranking, and sorted report uses ORDER BY. An HR team wants employees sorted by joining date — ORDER BY join_date ASC. A sales manager wants the top 10 revenue-generating customers — ORDER BY total_revenue DESC LIMIT 10. A logistics team wants shipments sorted by city then by weight — ORDER BY city ASC, weight DESC. Sorting is so fundamental that virtually every data analyst query ends with an ORDER BY clause to make results readable and meaningful.
⚠️ Common Mistakes to Avoid
❓ Frequently Asked Questions
What is ORDER BY in SQL?
ORDER BY sorts the result set of a query by one or more columns. ASC sorts ascending (smallest to largest, A to Z) and DESC sorts descending (largest to smallest, Z to A). ASC is the default if you do not specify.
Can you ORDER BY multiple columns in SQL?
Yes. ORDER BY col1 ASC, col2 DESC sorts first by col1, then for rows with the same col1 value, it sorts by col2. This is called a composite sort.
Can you ORDER BY a column that is not in SELECT?
Yes, in most databases. You can ORDER BY any column in the table even if it is not in your SELECT list. However, in queries with GROUP BY or DISTINCT, you can only ORDER BY columns that are in the SELECT list.
Where does ORDER BY go in a SQL query?
ORDER BY goes near the end of the query, after WHERE and GROUP BY and HAVING, but before LIMIT/OFFSET. The full clause order is: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT.
How does SQL ORDER BY handle NULL values?
By default, NULLs appear first when sorting ASC in PostgreSQL and last in MySQL and SQL Server. You can control this with NULLS FIRST or NULLS LAST in databases that support it.
✏️ Practice Exercise
Write a query to show all orders, sorted by order_date newest first, and for orders on the same date sorted by amount highest first.
Learn SQL with Live Trainer Guidance
These tutorials give you the theory. Our live SQL course at EVIKA Academy, Noida teaches you to apply SQL on real company datasets — with a trainer who uses it daily at MakeMyTrip.