ORDER BY clause
The ORDER BY clause is a fundamental SQL command that lets you control the sequence in which your query results are returned.
Overview
The ORDER BY clause is a fundamental SQL command that lets you control the sequence in which your query results are returned. It appears near the end of a SQL query and sorts the rows based on one or more columns you specify.
Basic Usage
In DuckDB, you can sort results in either ascending (ASC) or descending (DESC) order. If you don't specify a direction, ascending order is used by default. Here's a simple example:
Copy code
SELECT name, age
FROM users
ORDER BY age DESC;
Multiple Column Sorting
You can sort by multiple columns, creating a hierarchy of sort criteria. Each subsequent column is used to break ties in the previous columns:
Copy code
SELECT city, state, population
FROM cities
ORDER BY state ASC, population DESC;
DuckDB Extensions
DuckDB adds some helpful extensions to standard SQL sorting. You can use ORDER BY ALL to sort by all columns in the SELECT list from left to right:
Copy code
SELECT city, state, population
FROM cities
ORDER BY ALL;
You can also reference column aliases defined in the SELECT clause directly in your ORDER BY, which isn't always supported in other databases:
Copy code
SELECT
first_name || ' ' || last_name AS full_name,
age
FROM users
ORDER BY full_name;
Performance Considerations
DuckDB implements sorting using a vectorized quicksort algorithm, making it highly efficient for large datasets. However, sorting large result sets can still be memory-intensive, so it's good practice to combine ORDER BY with LIMIT when you only need a subset of sorted results:
Copy code
SELECT city, population
FROM cities
ORDER BY population DESC
LIMIT 10;
Related terms
GROUP BY organizes rows into groups for aggregate calculations like SUM, COUNT, and AVG. Learn single and multi-column grouping, HAVING filters, and GROUP BY ALL.
WHERE clause →The WHERE clause filters rows in a SQL query based on a boolean condition, keeping only rows for which the condition evaluates to true.
OVER clause →The OVER clause turns an aggregate or ranking function into a window function by defining the partition, order, and frame it operates on, without collapsing the result set.
LIMIT →The LIMIT clause restricts a query's result set to a maximum number of rows, often used with ORDER BY to fetch a top-N subset.
USING clause →The USING clause specifies join columns by name when both tables share identically named columns, joining on equality and returning a single copy of each shared column.
SQL query →A SQL query is a structured request written in Structured Query Language (SQL) that allows you to retrieve, analyze, or manipulate data stored in a database.
FAQS
It controls the sequence in which query results are returned, sorting rows by one or more columns you specify.
Use ORDER BY col ASC or ORDER BY col DESC; ascending is the default. You can sort by multiple columns, where each subsequent column breaks ties in the previous ones.
A DuckDB extension that sorts by all columns in the SELECT list from left to right. DuckDB also lets you order by column aliases defined in the SELECT clause.


