ORDER BY Clause

SQL · Sorting Rows

Theory

Tables have no guaranteed row order, so without ORDER BY the database can return rows in any order and that order can change between runs. ORDER BY sorts the final result by one or more columns or expressions. ASC (ascending, the default) goes smallest to largest or A to Z; DESC reverses it.

With several sort keys, the first key decides the order and later keys only break ties. ORDER BY dept ASC, salary DESC groups rows by department and, inside each department, puts the highest salary first. Because ORDER BY runs after SELECT, it can use column aliases. NULLs sort first in some engines (SQLite, MySQL, SQL Server) and last in others (PostgreSQL, Oracle); many engines support NULLS FIRST / NULLS LAST to control it.

Why we use it

Reports, top-N lists, pagination and readable output all need a predictable order. ORDER BY is the only way to guarantee that order; relying on how rows happen to come back leads to flaky results.

When to use it

Use ORDER BY whenever row order matters to the reader or to LIMIT/FETCH. Skip it inside subqueries or views where order is ignored anyway, and avoid sorting huge results you do not need, because sorting costs time.

How it works

  1. Result rows are ready after SELECT
  2. Compare rows on the first sort key
  3. Do two rows tie on that key? Yes: Compare on the next sort key. No: This key decides the order.
  4. Apply ASC or DESC per key
  5. Place NULLs first or last as the engine (or NULLS FIRST/LAST) says
  6. Return sorted rows

Syntax

SELECT columns
FROM table
ORDER BY column [ASC|DESC];

Example

SELECT name, salary FROM employees ORDER BY salary DESC;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic