You've learned how 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 you explicitly ask for one. Often, you'll want to see your results sorted, perhaps to find the highest or lowest values, or simply to present the information in a more organized way. This is where the ORDER BY
clause comes in. It allows you to specify the column(s) based on which the result set should be sorted.
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 NULL
s 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?
© 2025 ApX Machine Learning