0PricingLogin
SQL Academy · Lesson

Sorting with ORDER BY

Order rows ascending and descending.

Why Order Matters

By default, a SELECT returns rows in no guaranteed order. The database is free to hand them back in whatever sequence is convenient.

When you need predictable output, like an alphabetical list or newest-first feed, you must ask for it explicitly with ORDER BY.

-- Without ORDER BY, row order is NOT guaranteed
SELECT name, price
FROM products;

The ORDER BY Clause

ORDER BY goes near the end of a query, after WHERE and before LIMIT. You name the column to sort on.

This sorts products alphabetically by name.

SELECT name, price
FROM products
ORDER BY name;

All lessons in this course

  1. Sorting with ORDER BY
  2. Sorting by Multiple Columns
  3. LIMIT and OFFSET
  4. Finding Top-N Records
← Back to SQL Academy