SQL ORDER BY

Sort the result set.

✍️ 3 exercises + code challenge in this lesson

ORDER BY sorts results by one or more columns.

  • ASC: ascending (the default), A→Z, 0→9.
  • DESC: descending.
  • Several columns: ORDER BY Country, CustomerName sorts by country, then by name within each country.

Key Takeaways: SQL ORDER BY

Mastering SQL ORDER BY helps you write clean and reliable SQL code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

SQL ORDER BY Code Example

SQL
SELECT ProductName, Price FROM Products
ORDER BY Price DESC;

SELECT CustomerName, Country, City FROM Customers
ORDER BY Country ASC, City DESC;
Free sign-in required · Google login available

SQL ORDER BY Practice Exercises

Check what you learned in “SQL ORDER BY”.

/ 3 solved
Fill in the blank

Sort from highest to lowest price.

SELECT * FROM Products ORDER BY Price ;
✓ Correct! Not quite. Try again.
Multiple choice

What is the default sort direction?

✓ Correct! Not quite. Try again.
Code challenge

Select CustomerName from Customers sorted by CustomerName A to Z.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services