0Pricing
SQL Academy · Lesson

Sorting with ORDER BY

Order rows ascending and descending.

Sorting with ORDER BY is a free SQL Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the SQL Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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;

Ascending Is the Default

If you don't say otherwise, sorting is ascending: A to Z for text, smallest to largest for numbers, earliest to latest for dates.

You can write ASC to make this explicit, but it changes nothing.

-- These two queries are identical
SELECT name, price FROM products ORDER BY price;
SELECT name, price FROM products ORDER BY price ASC;

Sorting Descending

Add DESC to reverse the order: Z to A, largest to smallest, newest to oldest.

This lists the most expensive products first.

SELECT name, price
FROM products
ORDER BY price DESC;

Sorting Dates

Dates and timestamps sort chronologically. DESC gives you the most recent rows first, the classic pattern for activity feeds and logs.

SELECT id, total, created_at
FROM orders
ORDER BY created_at DESC;

Sorting by a Column You Don't Select

The sort column does not have to appear in the SELECT list. You can order by something the user never sees.

Here we show only the name, but order by price under the hood.

SELECT name
FROM products
ORDER BY price DESC;

Sorting by a Column Position

PostgreSQL lets you sort by the position of a column in the SELECT list. ORDER BY 2 means "the second selected column".

It's handy for quick queries, but using real column names is clearer and safer.

-- 2 refers to price (the 2nd column)
SELECT name, price
FROM products
ORDER BY 2 DESC;

Sorting by an Expression

You can sort by a computed value, not just a stored column. The expression is evaluated for each row, then used to order.

This ranks products by their total inventory value.

SELECT name, price, stock
FROM products
ORDER BY price * stock DESC;

Sorting by an Alias

If you give an expression an alias in the SELECT, you can reuse that alias in ORDER BY. It keeps the query readable.

SELECT name, price * stock AS inventory_value
FROM products
ORDER BY inventory_value DESC;

Case Sensitivity in Text Sorts

Text sorting follows the database collation, and uppercase and lowercase may sort differently than you expect.

To force case-insensitive ordering, sort by a lowered copy of the column with LOWER().

SELECT name
FROM products
ORDER BY LOWER(name);

Where ORDER BY Fits

Remember the logical order of clauses:

  • FROM picks the table
  • WHERE filters rows
  • ORDER BY sorts what's left
  • LIMIT trims the result

Sorting always happens after filtering.

SELECT name, price
FROM products
WHERE price > 10
ORDER BY price DESC;

Quick Check

Which query lists the cheapest products first?

Recap

You now control row order:

  • ORDER BY column sorts the result
  • ASC (default) goes smallest to largest; DESC reverses it
  • You can sort by columns, positions, expressions or aliases
  • Use LOWER() for case-insensitive text sorting

Next, you'll break ties by sorting on multiple columns at once.

SELECT name, price
FROM products
ORDER BY price DESC;

Frequently asked questions

Is the “Sorting with ORDER BY” lesson free?

Yes — the full text of “Sorting with ORDER BY” is free to read here on the web, and the SQL Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the SQL Academy course, upgrade to CoddyKit PRO.

What will I learn in “Sorting with ORDER BY”?

Order rows ascending and descending. You practise SQL Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start SQL Academy?

No prior experience is required. SQL Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sorting with ORDER BY” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this SQL Academy lesson?

Yes. Every SQL Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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