Module 1: SQL Foundations20 min

Sorting with ORDER BY

Progress Tracking

Log in to save this lesson and continue from where you left off.

Log in

Controlling the Order of Results

Without ORDER BY, SQL returns rows in no guaranteed order. You might get them in the order they were inserted, or by primary key, or seemingly random. If you care about the order, you need to specify it.

Your First ORDER BY

SQL
SELECT *
FROM orders
ORDER BY total_order_cost;
Table: techcorp_workforce
idfirst_namelast_namedepartmentsalaryphone_numberjoining_date
1SarahMitchellHR95000555-01012021-03-15
2MichaelChenHR88000555-01022022-06-01
3EmilyRodriguezHR825002021-09-20
4DavidParkHR80000555-01042023-01-10
5LisaThompsonHR650002021-04-05
1
Sort Employees by Salary

Sort `techcorp_workforce` by salary from lowest to highest.

Tables: techcorp_workforce
DESC for Top N Queries

DESC is what you want for "top N" queries: top earners, most recent orders, highest scores.

Sorting Text

Text sorts alphabetically:

SQL
SELECT
  first_name,
  last_name,
  department
FROM techcorp_workforce
ORDER BY last_name ASC;

This returns employees in alphabetical order by last name.

Sorting Dates

Dates are sorted chronologically. Use DESC for the most recent first.

2
Find the Most Recently Hired Employees

Sort employees to show the most recently hired first.

Tables: techcorp_workforce

The most recently hired employees appear first.

Sorting by Multiple Columns

You can sort by multiple columns. SQL sorts by the first column, then breaks ties with the second column, and so on.

3
Sort by Department then Salary

Sort departments alphabetically, and within each department show the highest earners first.

Tables: techcorp_workforce
Each Column Gets Its Own Sort Direction

Each column can have its own ASC or DESC. You might want department A-Z, but salary highest first within each department.

Combining ORDER BY with WHERE

ORDER BY comes after WHERE. Write a query that filters orders, then sorts by cost from highest to lowest:

Table: orders
idcust_idorder_dateorder_detailstotal_order_cost
132019-03-04Coat100
232019-03-01Shoes80
332019-03-07Skirt30
472019-02-01Coat25
572019-03-10Shoes80
4
Filter and Sort Orders

Find all orders and sort them by total cost from highest to lowest.

Tables: orders

Sorting by Column Position

You can sort by column number instead of name:

SQL
SELECT
  first_name,
  last_name,
  salary
FROM techcorp_workforce
ORDER BY 3 DESC; -- Sorts by the 3rd column (salary)

This is shorter but less readable. Use it sparingly, mainly in quick ad-hoc queries.

Column Positions Are Fragile

The number refers to the column's position in your SELECT list, not the table schema. Column positions can break if someone adds or removes columns from the SELECT list. Named columns are safer for saved queries.

Sorting by Alias

You can sort by a column alias, which is useful when sorting by a calculated column:

5
Try Sorting by Name Length

Sort employees by the length of their first name (longest first). Include the length as a column.

Tables: techcorp_workforce

NULLs in Sorting

Where do NULLs appear when sorting? It depends on the database:

  • PostgreSQLNULLs last in ASC, first in DESC
  • MySQLNULLs first in ASC, last in DESC
  • SQL ServerNULLs first in ASC, last in DESC
  • OracleNULLs last in ASC, first in DESC

PostgreSQL lets you control this explicitly:

PostgreSQL
ORDER BY column_name ASC NULLS FIRST
ORDER BY column_name DESC NULLS LAST
NULL Sort Order Varies by Database

If NULL ordering matters for your query, check your database's default behavior or use NULLS FIRST / NULLS LAST explicitly.

Key Takeaways

  • ORDER BY controls the sort order of your results.
  • ASC (ascending) is the default. Use DESC for descending.
  • Sort by multiple columns to break ties — each column gets its own direction.
  • ORDER BY comes after WHERE in the query.
  • NULL behavior in sorting varies by database — use NULLS FIRST / NULLS LAST when it matters.

What's Next

Now that you can sort your results, you'll often want to return only the top N rows. That's where LIMIT comes in, which you'll learn in the next lesson.