0Pricing
SQL Academy · Lesson

ORDER BY Single and Multiple Columns

Sort results ascending or descending, sort by multiple columns, and control where NULLs land with NULLS FIRST / NULLS LAST.

ORDER BY Single and Multiple Columns is a free SQL Academy lesson on CoddyKit — lesson 3 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 You Need ORDER BY

A SQL table is an unordered set. Without ORDER BY, the database may return rows in any order — and that order can change between runs. If order matters in your UI or report, sort explicitly.

Sorting by One Column

Default sort is ascending:

SELECT id, email FROM users ORDER BY created_at;       -- oldest first
SELECT id, email FROM users ORDER BY created_at ASC;
SELECT id, email FROM users ORDER BY created_at DESC;  -- newest first

Sorting by Multiple Columns

List columns in priority order. The second column breaks ties on the first:

SELECT first_name, last_name, age
FROM users
ORDER BY last_name ASC, first_name ASC, age DESC;

Sorting by Expression

You can sort by any expression:

SELECT email, full_name
FROM users
ORDER BY LENGTH(email) DESC, email ASC;

Sorting by Alias or Position

You can refer to a SELECT-list alias or its 1-based position:

SELECT id, total * 0.9 AS discounted
FROM orders
ORDER BY discounted DESC;     -- by alias

SELECT id, total
FROM orders
ORDER BY 2 DESC;               -- by position (avoid in code — fragile)

NULLS FIRST / NULLS LAST

Control where NULLs land:

-- Default: NULLs LAST when ASC, FIRST when DESC
SELECT * FROM users ORDER BY last_login_at DESC NULLS LAST;
SELECT * FROM users ORDER BY last_login_at ASC  NULLS FIRST;

Stable Sorts and Tiebreakers

If multiple rows tie on the sort key, the database may return them in any order. Add the primary key as a tiebreaker for deterministic results:

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

ORDER BY and Indexes

If an index exists on the ORDER BY column(s), the database can read rows in order without a sort step.

-- With index on created_at DESC, this can use the index directly:
SELECT * FROM events ORDER BY created_at DESC LIMIT 100;

CASE in ORDER BY

Custom ordering with CASE:

-- Show priority orders first:
SELECT * FROM orders
ORDER BY
  CASE status
    WHEN 'urgent'   THEN 0
    WHEN 'normal'   THEN 1
    ELSE 2
  END,
  created_at DESC;

ORDER BY Runs After WHERE

ORDER BY operates on the result of WHERE/GROUP BY/HAVING. Filtering happens first, then sorting.

Don't Sort Huge Tables Without LIMIT

Sorting a billion-row result is expensive. If you only need the top N, add LIMIT — the planner can switch to a Top-N strategy.

SELECT * FROM events ORDER BY created_at DESC LIMIT 50;

Recap

ORDER BY makes results deterministic.

  • ASC / DESC per column
  • Multiple columns for tiebreakers
  • NULLS FIRST / LAST when relevant
  • Always pair big sorts with LIMIT

Quick Check

What is the default sort direction in SQL?

Frequently asked questions

Is the “ORDER BY Single and Multiple Columns” lesson free?

Yes — the full text of “ORDER BY Single and 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 “ORDER BY Single and Multiple Columns”?

Sort results ascending or descending, sort by multiple columns, and control where NULLs land with NULLS FIRST / NULLS LAST. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “ORDER BY Single and 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. Selecting Columns and Expressions
  2. WHERE Filters: Comparison and Logic Operators
  3. ORDER BY Single and Multiple Columns
  4. LIMIT, OFFSET and Pagination Basics
← Back to SQL Academy