TutorialsSQLORDER BY
🟢 Free Demo
SQL TutorialTopic 5 of 20

ORDER BY

Sort results in ascending or descending order

✅ What You Will Learn

How to sort results ascending (A→Z, low→high) and descending (Z→A, high→low)
How to sort by multiple columns
How ORDER BY interacts with NULL values
How to use ORDER BY with column position numbers
Why ORDER BY is essential when using LIMIT for top-N queries

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_idcustomer_nameproductamountorder_date
1001Rahul SharmaLaptop450002026-01-15
1002Priya VermaMobile Phone180002026-01-16
1003Amit KumarHeadphones35002026-01-16
1004Sneha KapoorLaptop520002026-01-17
1005Vikram SinghTablet280002026-01-18

Syntax

SQL SYNTAX
SELECT column1, column2
FROM table_name
ORDER BY column1 ASC;   -- ascending (default)
ORDER BY column1 DESC;  -- descending

Examples

Example 1Sort by amount — highest first
SELECT customer_name, product, amount
FROM orders
ORDER BY amount DESC;
OUTPUT
customer_name | product      | amount
--------------|--------------|-------
Sneha Kapoor  | Laptop       | 52000
Rahul Sharma  | Laptop       | 45000
Priya Verma   | Mobile Phone | 18000
Amit Kumar    | Headphones   | 3500
Example 2Sort alphabetically
SELECT customer_name, product
FROM orders
ORDER BY customer_name ASC;
OUTPUT
customer_name | product
--------------|-------------
Amit Kumar    | Headphones
Priya Verma   | Mobile Phone
Rahul Sharma  | Laptop
Sneha Kapoor  | Laptop
💡

ASC is the default — you can omit it. Always write DESC explicitly when you want reverse order.

Example 3Sort by multiple columns
SELECT customer_name, product, amount
FROM orders
ORDER BY product ASC, amount DESC;
💡

First sorts by product alphabetically. Within each product group, sorts by amount highest to lowest.

Example 4ORDER BY with WHERE
SELECT customer_name, amount
FROM orders
WHERE amount > 10000
ORDER BY amount DESC;
💡

WHERE filters first, then ORDER BY sorts the filtered result. The order of clauses in the query matters.

📌 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

WRONGExpecting a default sort order without ORDER BY
FIXSQL tables have no guaranteed row order. Without ORDER BY, results can come back in any order and may differ between runs. Always use ORDER BY when order matters.
WRONGUsing ORDER BY without LIMIT for top-N queries
FIXORDER BY DESC alone returns all rows sorted. Add LIMIT N after ORDER BY to get only the top N rows.
WRONGSorting text columns that contain numbers
FIXText columns sort lexicographically: '10', '2', '20' sorts as '10', '2', '20' not 2, 10, 20. Cast to a number type for correct numeric sorting: ORDER BY CAST(column AS INT).
✏️Test Yourself

Which keyword sorts results from highest to lowest?

❓ 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.

← PreviousAND, OR, NOTNext →LIMIT and TOP
🎓 Level Up Faster

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.