| Lesson 7 | An introduction to the ORDER BY clause |
| Objective | Learn how the ORDER BY clause works in SQL |
In the relational model, a table represents a set of rows, and sets have no intrinsic order. That means the rows returned by
a SELECT statement should be treated as unordered unless you explicitly request a sort. If you need results
in a predictable sequence (for reporting, exports, UI display, or “top N” queries), you use the ORDER BY clause.
ORDER BY is evaluated after rows are selected, filtered, grouped, and projected, so it appears at the end of a
standard SELECT statement.
You can sort by one column or multiple columns, and you can control sort direction using ASC or DESC.
SELECT columns
FROM table
ORDER BY sort_column [ASC | DESC];
Most SQL dialects allow sorting by a column that is not listed in the SELECT list, but it is a best practice to
keep your intent clear and consistent—especially when building queries that will be maintained by others.
If you omit the sort direction, SQL assumes ASC. In practice, many developers omit ASC and specify
DESC explicitly when needed.
SELECT au_fname, au_lname, city, state
FROM authors
ORDER BY au_lname ASC;
| au_fname | au_lname | city | state |
| --------- | ----------- | ------------- | ----- |
| Sarah | Buchman | Bronx | NY |
| Wendy | Heydemark | Boulder | CO |
| Hallie | Hull | San Francisco | CA |
| Klee | Hull | San Francisco | CA |
| Christian | Kells | New York | NY |
| | Kellsey | Palo Alto | CA |
| Paddy | O'Furniture | Sarasota | FL |
au_lname (last name).
To see how sorting changes what you see, consider the following Customer table. When you query it without
ORDER BY, you might observe a “natural” order, but you should not rely on it. The database engine is free to return
rows in any sequence unless you request a sort.
| Lastname | Firstname | PhoneNumber | ZIP Code |
| Wynsoup | Stephen | 520-555-1212 | 85744 |
| Brennan | Jeffrey | 123-321-5678 | 04325 |
| Caitlin | Elizabeth | 444-231-5642 | 95439 |
| Wynsoup | Julie | 201-453-7641 | 85744 |
| Andrews | Julie | 309-123-4567 | 85744 |
SELECT *
FROM Customer;
The result set contains the correct rows, but the ordering is not guaranteed.
If you want the list alphabetized by last name, add ORDER BY Lastname:
SELECT *
FROM Customer
ORDER BY Lastname;
The result will be:
| Lastname | Firstname | PhoneNumber | ZIP Code |
| Andrews | Julie | 309-123-4567 | 85744 |
| Brennan | Jeffrey | 123-321-5678 | 04325 |
| Caitlin | Elizabeth | 444-231-5642 | 95439 |
| Wynsoup | Stephen | 520-555-1212 | 85744 |
| Wynsoup | Julie | 201-453-7641 | 85744 |
This is clearer, but notice what happens with ties: both rows with Lastname = 'Wynsoup' appear together, yet
their Firstname values are not guaranteed to be alphabetized. If two rows compare equal on the sort key,
SQL is free to return those tied rows in any order.
If you want a stable, predictable order when last names repeat, add a secondary sort key. This is a common pattern in reports and UI grids:
SELECT *
FROM Customer
ORDER BY Lastname, Firstname;
Now rows are ordered by Lastname first, and for matching last names they are ordered by Firstname.
You can extend this pattern to three or more columns as needed.
Use DESC to reverse the order. For example, to list customers with “Z” last names first:
SELECT *
FROM Customer
ORDER BY Lastname DESC;
ORDER BY, even if results “look consistent” during testing.
In the next lesson, you will apply ORDER BY to more realistic queries, including sorting numeric values,
sorting with NULL values present, and combining sorting with filtering using WHERE.