SQL Keywords

SQL ORDER

What does SQL ORDER BY do?

The ORDER BY clause sorts query result rows by one or more columns or expressions in ascending or descending order.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL ORDER: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, Redshift, BigQuery and all ANSI-compliant systems

SQL ORDER Full Explanation

ORDER BY is the final logical step of a SELECT statement. After rows are filtered, grouped, and projected, ORDER BY arranges the resulting set according to the specified columns or expressions. If multiple sort keys are listed, the second key is applied when preceding keys compare as equal, and so on. The default direction is ASC (ascending). DESC reverses the order. Numeric positions can reference output columns, but explicit column names are clearer and safer. NULLS FIRST and NULLS LAST, supported in many dialects, control null ordering when direction alone is insufficient. Because ORDER BY forces the database to compare every returned row, it can add significant cost, especially without supporting indexes. Some databases require ORDER BY columns to appear in the SELECT list unless DISTINCT or UNION is used; Standard SQL does not. When combined with LIMIT/OFFSET (or FETCH FIRST), ORDER BY enables robust pagination.

SQL ORDER Syntax

SELECT column_list
FROM table_name
[WHERE condition]
[GROUP BY group_list]
ORDER BY sort_key1 [ASC | DESC] [NULLS FIRST | NULLS LAST],
         sort_key2 [ASC | DESC] [NULLS FIRST | NULLS LAST];

SQL ORDER Parameters

  • sort_key (column or expression) - Required. One or more columns or expressions that define the sort order.
  • ASC (keyword) - Optional. Sorts in ascending order (lowest to highest). Default.
  • DESC (keyword) - Optional. Sorts in descending order (highest to lowest).
  • NULLS FIRST / NULLS LAST - Optional. Specifies where NULL values appear in the ordering. Support varies.

Example Queries Using SQL ORDER

-- Basic ascending sort
SELECT id, email
FROM users
ORDER BY email ASC;

-- Multiple keys with mixed directions
SELECT id, first_name, last_name, created_at
FROM users
ORDER BY last_name ASC, created_at DESC;

-- Sorting by expression
SELECT id, price, price * quantity AS total
FROM orders
ORDER BY total DESC;

-- Pagination pattern (PostgreSQL / MySQL 8+)
SELECT *
FROM events
ORDER BY occurred_at DESC
LIMIT 20 OFFSET 40;

Expected Output Using SQL ORDER

  • Rows are returned to the client sorted exactly as specified
  • No table data is modified; only the presentation order changes

Use Cases with SQL ORDER

  • Displaying lists to end users in a predictable order
  • Generating ranked reports such as top 10 customers by revenue
  • Ensuring deterministic pagination when combined with LIMIT/OFFSET
  • Ordering time-series data for analytics or dashboards

Common Mistakes with SQL ORDER

  • Omitting ORDER BY and assuming the database returns rows in a natural order (SQL result order is undefined without ORDER BY)
  • Using column numbers that become incorrect when the SELECT list changes
  • Forgetting tie-breaker columns, leading to non-deterministic pagination
  • Assuming ASC sorts NULLs last (behavior differs across dialects)
  • Ordering by non-indexed columns on large tables, causing slow queries

Related Topics

GROUP BY, LIMIT, FETCH FIRST, DISTINCT, UNION, WINDOW FUNCTIONS, INDEXES

First Introduced In

ANSI SQL-86

Frequently Asked Questions

Does SQL guarantee row order without ORDER BY?

No. Without an ORDER BY clause, row order is undefined and can change between runs.

How can I put NULL values at the end?

Use NULLS LAST if your database supports it: `ORDER BY column ASC NULLS LAST`. In MySQL, add `ORDER BY column IS NULL, column`.

Where does ORDER BY occur in the query lifecycle?

After SELECT, FROM, WHERE, GROUP BY, and HAVING have produced the final set, ORDER BY sorts that set for output.

Can ORDER BY use column positions?

Yes, `ORDER BY 1` refers to the first SELECT column, but explicit names are clearer and safer.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!