ORDER BY
Sorts query results by one or more columns.
SELECT columns FROM table_name ORDER BY column1 ASC, column2 DESC;This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.
What it does
Overview
Sorts query results by one or more columns.
The ORDER BY clause is a fundamental SQL statement used to sort the result set of a query by one or more columns in ascending (ASC) or descending (DESC) order. By default, queries return rows in an indeterminate order unless this clause is used. Technically, sorting is executed after row filtering (WHERE) and grouping (GROUP BY), making it one of the final steps in the query execution plan. For performance optimization, databases can utilize B-Tree indexes to avoid expensive 'filesort' operations; when the sort order matches the index order, the engine can read rows directly from the index. Unlike the JavaScript sort() method, which mutates the original array and returns the same reference, SQL ORDER BY only affects the presentation of the result set without altering the underlying table data. Handling of NULL values is an important edge case: while behavior varies by dialect, many systems allow NULLS FIRST or NULLS LAST modifiers. To ensure reliable paging (e.g., using LIMIT and OFFSET), always include a unique tie-breaker column to guarantee a deterministic, stable sort. For date-based ordering, ensure columns use proper DATE/TIMESTAMP types or ISO 8601 strings for reliable chronological results.
Quick reference
Syntax
SELECT columns FROM table_name ORDER BY column1 ASC, column2 DESC;
Inputs
Parameters
See it in practice
Examples
Basic Descending Sort
SELECT product_name, price FROM products ORDER BY price DESC;Laptop: 1200, Tablet: 600, Mouse: 25
Retrieves products sorted from the most expensive to the least expensive.
Multi-Column Sorting (Tie-Breaking)
SELECT last_name, first_name, department FROM employees ORDER BY department ASC, last_name ASC;Engineering: Adams, Engineering: Brown, Sales: Smith
Orders the results first by department name alphabetically, and then by last name for employees within the same department.
Sorting by Expression or Alias
SELECT item_id, (unit_price * quantity) AS total_value FROM inventory ORDER BY total_value DESC;101: 5000.00, 204: 1200.50, 305: 450.00
Calculates a derived value and sorts the entire result set based on that calculated alias.
Debug faster
Common Errors
PerformanceError
Cause: Attempting to sort millions of rows on a column that is not indexed, forcing the database to perform a disk-heavy 'filesort'.
Fix: Create a B-Tree index on the specific column used in the ORDER BY clause to allow the engine to retrieve rows in order.
SELECT * FROM logs ORDER BY timestamp; -- Slow on large tables without an indexLogicError
Cause: Non-deterministic sorting when using LIMIT. If the sort columns contain duplicate values, the database may return different rows for the same query across different executions.
Fix: Always include a unique column (like a Primary Key) as the final tie-breaker in your ORDER BY list.
SELECT name FROM users ORDER BY age LIMIT 10; -- May return different '25-year-olds' each timeRuntime support
Compatibility
Source: MDN Web Docs
Common questions
Frequently Asked Questions
Sorts query results by one or more columns.
column: Column(s) or expressions to sort by. ASC/DESC: Sort direction (ASC default, DESC for reverse).
PerformanceError: Create a B-Tree index on the specific column used in the ORDER BY clause to allow the engine to retrieve rows in order. LogicError: Always include a unique column (like a Primary Key) as the final tie-breaker in your ORDER BY list.