SQL queries allow you to select specific columns and filter rows using the WHERE clause. However, the rows returned by a query don't have a guaranteed order unless explicitly specified. Often, sorting results is necessary, perhaps to find the highest or lowest values, or to present information in an organized manner. The ORDER BY clause provides this functionality, allowing you to specify the column(s) for sorting the result set.
The ORDER BY clause is added towards the end of your SELECT statement, typically after the FROM and WHERE clauses (if present). You specify the column you want to sort by.
Let's imagine we have a products table with columns like product_name, category, and price. If you want to list the products sorted alphabetically by their name, you would write:
SELECT product_name, category, price
FROM products
ORDER BY product_name;
By default, ORDER BY sorts the data in ascending order (A to Z for text, smallest to largest for numbers).
While ascending order (ASC) is the default, you can explicitly state it, or you can request descending order (DESC).
ASC: Ascending order (lowest to highest, A to Z). This is the default if you don't specify anything.DESC: Descending order (highest to lowest, Z to A).To sort the products from the most expensive to the least expensive, you would use DESC:
SELECT product_name, category, price
FROM products
ORDER BY price DESC;
If you wanted to be explicit about sorting product names alphabetically (ascending), you could write:
SELECT product_name, category, price
FROM products
ORDER BY product_name ASC;
Although ASC is optional here, explicitly stating it can sometimes improve clarity.
What happens if you sort by a column where multiple rows have the same value? For instance, if you sort products by category, how are products within the same category ordered? By default, their order is not guaranteed.
You can specify a secondary (or tertiary, etc.) sort column to control the order within groups. List the columns in the ORDER BY clause, separated by commas. The sorting happens sequentially: the data is first sorted by the first column listed. Then, for any rows where the first column's value is the same, the data is sorted by the second column listed, and so on.
Let's sort the products first by category (alphabetically) and then, within each category, by price (highest to lowest):
SELECT product_name, category, price
FROM products
ORDER BY category ASC, price DESC;
In this query:
category in ascending order (e.g., 'Electronics' comes before 'Home Goods').price in descending order (the most expensive electronic item appears first within that category group).You can mix ASC and DESC for different columns in the same ORDER BY clause.
The ORDER BY clause works perfectly in conjunction with the WHERE clause. The database first filters the rows based on the WHERE condition, and then sorts the resulting rows according to the ORDER BY specification.
For example, to find all products in the 'Electronics' category that cost less than $500, sorted by price from lowest to highest:
SELECT product_name, category, price
FROM products
WHERE category = 'Electronics' AND price < 500
ORDER BY price ASC; -- ASC is optional here
Remember the standard order of clauses in a SELECT statement:
SELECT columns...FROM table...WHERE conditions...ORDER BY columns...LIMIT number... (if used, typically comes last or after ORDER BY)How NULL values are treated during sorting can vary between different database systems (like PostgreSQL, MySQL, SQL Server). Some systems place NULLs first when sorting ascending and last when sorting descending, while others might do the opposite or provide syntax like NULLS FIRST or NULLS LAST to explicitly control this. For basic sorting, just be aware that NULL values might appear at the beginning or end of your sorted results.
Sorting is a fundamental part of making query results interpretable. The ORDER BY clause gives you precise control over the presentation sequence, allowing you to arrange data logically based on one or more columns in either ascending or descending order. This is invaluable for finding extremes, grouping related items visually, or preparing data for reports.
Was this section helpful?
SELECT statement and its ORDER BY clause, including sorting direction, multiple column sorting, and specific handling of NULL values.ORDER BY clause, within the broader context of relational databases.ORDER BY clause with practical examples.© 2026 ApX Machine LearningAI Ethics & Transparency•