What Is ORDER BY in SQL?

SQL ORDER BY sorts the rows returned by a SELECT statement. Place ORDER BY after FROM/WHERE; list one or more columns and an optional ASC (default) or DESC keyword. Multiple columns create secondary sort levels. NULLS FIRST/NULLS LAST is supported in some dialects. Add LIMIT for top-n queries. Avoid ordering unnecessary rows for speed.

Learning
June 10, 2025
Galaxy Team
Sign up for the latest notes from our team!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
SQL ORDER BY sorts result rows by one or more columns using ASC or DESC. Add it after SELECT, list columns, and combine with LIMIT or expressions for precise, predictable output.

What Does ORDER BY Do in SQL?

ORDER BY sorts the final result set of a SELECT statement so rows appear in a predictable sequence. Without it, SQL returns rows in an arbitrary order based on the query plan.

How Is ORDER BY Placed in Query Syntax?

Write ORDER BY after FROM, JOIN, WHERE, GROUP BY, and HAVING but before LIMIT or OFFSET. This position guarantees the sort acts on the already filtered and aggregated rows.

Which Keywords Control Sort Direction?

Use ASC for ascending order and DESC for descending order. ASC is the default, so specifying it is optional unless you want DESC.

Can I Sort by Multiple Columns?

Yes. List columns separated by commas. SQL applies sorts from left to right, creating secondary, tertiary, and further ordering layers.

How Are NULL Values Ordered?

ANSI SQL treats NULL as unknown and places it after non-NULL values in ASC order. Many databases let you add NULLS FIRST or NULLS LAST for explicit control.

Why Does ORDER BY Affect Performance?

Sorting requires memory and possibly disk IO. Indexes on the ORDER BY columns or limiting rows with WHERE can reduce resource use.

When Should I Use ORDER BY with LIMIT?

Combine ORDER BY with LIMIT (TOP in T-SQL, FETCH FIRST) to fetch the "first n" rows in a defined order, such as the latest 10 orders.

Can I Order by an Expression or Alias?

You may sort by calculations, functions, or a SELECT column alias. Use the alias name, the expression again, or its ordinal position.

How Does ORDER BY Interact with GROUP BY?

GROUP BY clusters rows before ORDER BY. To sort on aggregated results, reference the aggregate expression or its alias in ORDER BY.

What Are Best Practices for ORDER BY?

Order only the rows you actually return, ensure supporting indexes, avoid sorting large blobs, and always use ORDER BY when deterministic output is required.

How Do Window Functions Compare to ORDER BY?

Window functions contain their own ORDER BY clause inside OVER() to define row order for the window. This internal order is independent of the final ORDER BY, so you can have two different sorts in one query.

Does ORDER BY Work the Same in All SQL Dialects?

Core syntax is portable, but options like NULLS FIRST/LAST, COLLATE, or case-insensitive ordering vary. Check your database documentation.

Key Takeaways

Always specify ORDER BY for predictable results, define direction with ASC/DESC, manage NULL placement explicitly, leverage indexes, and limit rows when possible for performance.

Frequently Asked Questions (FAQs)

Does ORDER BY slow down my query?

Sorting can add CPU, memory, and temporary disk usage. Indexes on the sorted columns or limiting the row set keeps overhead low.

Can I order by a column not in SELECT?

Yes. ORDER BY columns do not have to appear in the SELECT list unless you later reference them in client code.

How do I sort case-insensitively?

Add a COLLATE clause (e.g., COLLATE NOCASE in SQLite) or use a LOWER() function around the column in ORDER BY.

Why does my ORDER BY change after upgrading the database?

Without ORDER BY, row order is non-deterministic. Changes in optimizer, storage, or partitioning can reshuffle rows. Always specify ORDER BY when order matters.

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

Check out our other posts!

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie
Truvideo Logo