Lesson 5
Sorting and limiting: ORDER BY and LIMIT
For readers who finished lesson 4 and can filter rows with WHERE.
- Rows come in no set order
- ORDER BY
- Descending, and a second column
- LIMIT
- Ties
- 4 exercises
Every query runs on real PostgreSQL, in your browser. It downloads once, about 6 MB, then starts in a few seconds.
For a reference page on the same statements, read SQL ORDER BY: Syntax, ASC/DESC, Multiple Columns, and Pagination.
The idea
Without ORDER BY, rows come in no set order
select distinct country from customers;
The countries come in no visible order. PostgreSQL returns rows in whatever order is quickest to produce, and nothing promises that this order stays the same.
The idea
ORDER BY sorts the answer
ORDER BY comes after WHERE and names the column to sort by. It sorts from small to large, A to Z and early to late, unless you say otherwise.
Try it
Sort the products by price
select name, price from products order by price;
Cheapest first: Cable Organizer at 12.50. Ascending is the default.
Try it
DESC sorts from large to small
Write desc after the column to turn the order round.
select name, price from products order by price desc;
Most expensive first: Workstation Tower at 1899.00.
Try it
A second column sorts the rows that tie
Several orders share a date. A second column after a comma decides their order, and only theirs.
select order_id, order_date from orders order by order_date desc, order_id desc;
Orders 1737, 1097 and 457 share 2026-10-02; the higher id comes first.
Try it
LIMIT keeps the first rows of the answer
LIMIT comes last and keeps that many rows. After an ORDER BY, it gives the top of the list.
select name, price from products order by price limit 5;
The five cheapest products, from 12.50 to 42.00.
Watch out
Ties make a top three unpredictable
Six employees, the department heads, share the top salary of 95,000. Asked for the three best paid, PostgreSQL picks three of them:
select first_name, last_name, salary
from employees
order by salary desc
limit 3;
| first_name | last_name | salary |
|---|---|---|
| Olga | Costa | 95000.00 |
| Ben | Novak | 95000.00 |
| Hugo | Lopez | 95000.00 |
Nothing says which three, or in which order.
Try it
Break the tie with a second sort column
Sort the employees with the same salary by employee_id, which is different for every row, and the answer is the same on every run.
select first_name, last_name, salary from employees order by salary desc, employee_id limit 3;
Hugo, Olga and Ben, employees 1 to 3, in that order, every time.
Exercise 1 of 4
Row order countsShow every product's name and price, the most expensive first.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select name, price from products order by price desc
Exercise 2 of 4
Row order countsShow the name and price of the five cheapest products, the cheapest first.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select name, price from products order by price limit 5
Exercise 3 of 4
Row order countsShow the id and date of the ten most recent orders, the newest first. For orders on the same date, the higher id comes first.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select order_id, order_date from orders order by order_date desc, order_id desc limit 10
Exercise 4 of 4
Row order countsShow the first name, last name and salary of the three best-paid employees. For equal salaries, the lower employee_id comes first.
Expected result
It shows once PostgreSQL is ready.
Each check sends the exercise, your SQL and any PostgreSQL error to dbschema.com, without your IP address, so that the hints can improve.
Show answer
select first_name, last_name, salary from employees order by salary desc, employee_id limit 3