🔢 ORDER BY – Sort Your Results in SQL

🔢 ORDER BY – Sort Your Results in SQL

🔧 Quick Syntax

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

This is how you tell SQL:

“Give me the results, but sort them — either from smallest to biggest (ASC) or biggest to smallest (DESC).”

🧾 Here’s the Table You’re Working With

Let’s say you have a table called orders:

order_id customer_name total_amount city
1Brill Venn15.99Fogtown
2Larka Tove45.00Fogtown
3Brill Venn15.99Dustvale
4Milo Kresh72.50Fogtown
5Vesh Luro10.00Windmere
6Tola Jinn29.99Fogtown

✅ Suppose You Want to See Orders from Lowest to Highest Total

SELECT customer_name, total_amount
FROM orders
ORDER BY total_amount ASC;

💡 Output:

Vesh Luro     10.00
Brill Venn    15.99
Brill Venn    15.99
Tola Jinn     29.99
Larka Tove    45.00
Milo Kresh    72.50

SQL sorted the results based on total_amount, from lowest to highest. The default is ASC (ascending), but it’s good to be explicit.


✅ Suppose You Want to Sort from Highest to Lowest

SELECT customer_name, total_amount
FROM orders
ORDER BY total_amount DESC;

💡 Output:

Milo Kresh    72.50
Larka Tove    45.00
Tola Jinn     29.99
Brill Venn    15.99
Brill Venn    15.99
Vesh Luro     10.00

DESC (descending) gives you the highest totals first — perfect for top spenders or ranking results.


✅ Suppose You Want to Sort by City, Then by Amount

You want to group results by city alphabetically, and inside each city, sort by the order total.

SELECT customer_name, city, total_amount
FROM orders
ORDER BY city ASC, total_amount DESC;

💡 Output:

Brill Venn   Dustvale   15.99
Brill Venn   Fogtown    15.99
Larka Tove   Fogtown    45.00
Milo Kresh   Fogtown    72.50
Tola Jinn    Fogtown    29.99
Vesh Luro    Windmere   10.00

SQL first sorted by city (A–Z), and within each city, sorted by total_amount in descending order. This is great when you need organized, grouped results.


🧃 Recap – What You Learned

  • ORDER BY sorts your query results
  • Use ASC for ascending order (default)
  • Use DESC for descending order
  • You can sort by multiple columns to control how rows are ordered
  • Perfect for reports, rankings, dashboards, or cleaning up messy tables

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top