0Pricing
SQL Academy · Lesson

Sorting by Multiple Columns

Break ties with secondary sort keys.

Sorting by Multiple Columns is a free SQL Academy lesson on CoddyKit — lesson 2 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.

The Problem of Ties

When you sort by a single column, rows with the same value have no defined order among themselves. Three products all priced at 20 could come back in any sequence.

To make the order fully predictable, add a second sort key.

-- Ties on price are ordered arbitrarily
SELECT name, price
FROM products
ORDER BY price;

Listing Multiple Sort Keys

ORDER BY accepts a comma-separated list of columns. The database sorts by the first column, then breaks ties with the second, and so on.

Here, products are sorted by price; ties are settled alphabetically by name.

SELECT name, price
FROM products
ORDER BY price, name;

Order of the Keys Matters

The sequence of columns changes the result. ORDER BY price, name is not the same as ORDER BY name, price.

The first key is the primary sort; later keys only matter when earlier ones tie.

-- Primary sort by category, then by name within each category
SELECT category, name
FROM products
ORDER BY category, name;

Per-Column Direction

Each sort key can have its own direction. Mix ASC and DESC freely.

This groups by category alphabetically, but inside each category shows the most expensive items first.

SELECT category, name, price
FROM products
ORDER BY category ASC, price DESC;

DESC Applies Per Column

A common mistake: thinking DESC at the end reverses everything. It only affects the column it directly follows.

In ORDER BY category, price DESC, category is still ascending; only price is descending.

-- category: ASC, price: DESC
SELECT category, price
FROM products
ORDER BY category, price DESC;

Three or More Keys

You can chain as many sort keys as you need. Each one only kicks in when all the keys before it are equal.

Sort orders by date, then by customer, then by total within the same customer and date.

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

A Stable, Deterministic Sort

To get a fully deterministic result, end your sort keys with a unique column such as the primary key. Then no two rows can ever tie.

This is important for pagination, which you'll see in the next lesson.

SELECT id, name, price
FROM products
ORDER BY price DESC, id;

Mixing Expressions and Columns

Sort keys can be a blend of plain columns and expressions. Each key is independent.

This sorts by inventory value descending, then alphabetically by name as a tiebreaker.

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

Sorting by a Boolean to Float a Group

A handy trick: sort by a boolean expression to push a group to the top or bottom. In PostgreSQL, false sorts before true.

This shows in-stock items (stock > 0) first, then cheapest within each group.

SELECT name, stock, price
FROM products
ORDER BY (stock = 0), price;

Controlling NULL Placement

By default in PostgreSQL, NULL sorts last in ascending order and first in descending. Override with NULLS FIRST or NULLS LAST on any key.

SELECT name, discount
FROM products
ORDER BY discount DESC NULLS LAST;

Putting It Together

A realistic multi-key sort might combine direction, expressions and NULL handling. Read it left to right: each key refines the one before it.

SELECT category, name, price, rating
FROM products
ORDER BY category ASC,
         rating DESC NULLS LAST,
         price ASC;

Quick Check

Consider ORDER BY category, price DESC.

Recap

Multi-column sorting in a nutshell:

  • List sort keys comma-separated; earlier keys win
  • Each key has its own ASC/DESC
  • End with a unique column for a deterministic order
  • Control missing values with NULLS FIRST/NULLS LAST

Next, you'll limit how many rows come back with LIMIT and OFFSET.

SELECT category, name, price
FROM products
ORDER BY category ASC, price DESC, id;

Frequently asked questions

Is the “Sorting by Multiple Columns” lesson free?

Yes — the full text of “Sorting by Multiple Columns” 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 by Multiple Columns”?

Break ties with secondary sort keys. 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 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sorting by Multiple Columns” 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