Beginner

496. JOIN with ORDER BY

Learn how ORDER BY can sort results produced by JOIN queries.

📘 Lesson 496 of 728 ⏱ 10 min 🎯 Beginner

ORDER BY sorts the final result of a SQL query and can use columns from any table included in the query.

Sorted results make joined reports easier to read and help analysts identify highest, lowest, newest, or oldest records.

Display customers and their orders with the highest order amount first.

SQL
SELECT
    c.CustomerName,
    o.OrderID,
    o.OrderAmount
FROM Customers c
INNER JOIN Orders o
    ON c.CustomerID = o.CustomerID
ORDER BY o.OrderAmount DESC;

Common in sales reports, transaction analysis, customer reports, and management dashboards.

  • ORDER BY sorts the final JOIN result.
  • Columns from joined tables can be used.
  • ASC sorts ascending.
  • DESC sorts descending.
  • Multiple sorting columns can be specified.
Can ORDER BY use a column from a joined table?

Yes. ORDER BY can sort using columns available from the tables in the query.

Does ORDER BY affect how JOIN matches rows?

No. ORDER BY sorts the final result after the JOIN and other query processing.

Have you finished this lesson?

Introduction to SQL – All Lessons