SQL Keywords

SQL ORDER BY

What is the SQL ORDER BY clause?

ORDER BY sorts query results 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 BY: Supported by PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, Redshift, BigQuery, and all ANSI-compliant databases. NULLS FIRST/LAST and COLLATE availability varies by engine.

SQL ORDER BY Full Explanation

ORDER BY determines the sequence in which rows are returned from a SELECT statement. Without it, the database may return rows in any arbitrary order. You can sort by one or many columns, by column aliases, or by expressions. The default direction is ASC (ascending). DESC reverses the order. Most dialects also support NULLS FIRST or NULLS LAST to control where null values appear, a COLLATE clause to apply a specific collation, and positional sorting (ORDER BY 1). Because ORDER BY is executed after SELECT and GROUP BY, you may reference aliases created in the SELECT list. Limiting clauses such as LIMIT, OFFSET, or FETCH FIRST work after ORDER BY, enabling reliable pagination. Sorting requires memory or disk space; large sorts can impact performance unless indexes supporting the requested order exist.

SQL ORDER BY Syntax

SELECT column_list
FROM table_name
[WHERE condition]
[GROUP BY group_list]
ORDER BY order_item [, order_item ...];

order_item := expression [ASC | DESC] [NULLS FIRST | NULLS LAST] [COLLATE collation_name]

SQL ORDER BY Parameters

  • expression (Any) - Column name, alias, or scalar expression to sort on.
  • ASC (Keyword) - Sort smallest to largest. Default.
  • DESC (Keyword) - Sort largest to smallest.
  • NULLS FIRST (Keyword) - Place nulls before non-nulls.
  • NULLS LAST (Keyword) - Place nulls after non-nulls.
  • COLLATE (Keyword) - Apply a specific collation for string comparison.
  • collation_name (Identifier) - Name of the collation to use.

Example Queries Using SQL ORDER BY

-- 1. Sort users by signup date, newest first
SELECT id, email, created_at
FROM users
ORDER BY created_at DESC;

-- 2. Alphabetical list with case-insensitive collation (PostgreSQL)
SELECT name
FROM customers
ORDER BY name COLLATE "en_US" ASC;

-- 3. Multi-column sort: price ascending, then product_name ascending
SELECT product_name, price
FROM products
ORDER BY price, product_name;

-- 4. Deterministic top-N query
SELECT order_id, total
FROM orders
ORDER BY total DESC
LIMIT 5;

Expected Output Using SQL ORDER BY

  • Rows appear sorted according to the specified columns
  • In example 1 the newest users come first; example 4 returns the five highest order totals

Use Cases with SQL ORDER BY

  • Present data to users or reports in a meaningful order.
  • Retrieve top-N or bottom-N records combined with LIMIT/OFFSET.
  • Ensure stable pagination in web applications.
  • Sort aggregated results after GROUP BY.
  • Order analytic query results before exporting to CSV or Excel.

Common Mistakes with SQL ORDER BY

  • Assuming rows are naturally ordered without ORDER BY.
  • Forgetting to include all columns needed for deterministic ordering when using LIMIT.
  • Using ordinal positions (ORDER BY 1) which can break if the SELECT list changes.
  • Expecting ORDER BY to work inside subqueries not materialized by the outer query.
  • Ignoring performance impact when no supporting index exists.

Related Topics

GROUP BY, HAVING, LIMIT, OFFSET, FETCH FIRST, DISTINCT, INDEXES, COLLATE

First Introduced In

ANSI SQL-86

Frequently Asked Questions

What does ORDER BY do in SQL?

ORDER BY sorts the result set returned by a SELECT statement on one or more columns or expressions.

Can I sort by multiple columns?

Yes. List each column separated by commas. Ordering is applied left to right.

How do I force NULL values to appear last?

Use NULLS LAST if the dialect supports it, or sort by an IS NULL expression before the column.

Does ORDER BY affect query performance?

Sorting can be expensive on large datasets. Proper indexing or limiting the result set helps mitigate cost.

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!