Module 3 · Sorting and summarising

Sort and limit results

By the end you will be able to sort a list and show its first rows.

About 12 min

0 of 52 lessons

Before you start, you need

Filter rows with WHERE

Get a short list in a stable order

To see the three most expensive products you need to sort first and choose afterwards. ORDER BY decides the order of the rows; LIMIT says how many to show.

ASC sorts from lowest to highest and is the default if you write nothing. DESC sorts from highest to lowest. So ORDER BY price DESC puts the highest prices first.

If two products have the same price, add another column to decide which comes first. In ORDER BY price DESC, id ASC, the id is used only when prices are tied.

LIMIT 3 keeps the first three rows of that list. Without ORDER BY, you do not say which ones you want to choose. The stored catalogue does not change, and the column order still depends on SELECT.

Minimal syntax
SELECT columns
FROM table_name
ORDER BY column_name DESC, id ASC
LIMIT 3;

See it in action

What are the three most expensive products?

SQL
SELECT id, name, price
FROM products
ORDER BY price DESC, id ASC
LIMIT 3;
Example result: 3 rows
idnameprice
4Pro Laptop999
7Monitor 27249
3Monitor 24179
  1. price DESC puts the highest prices first. id ASC fixes the order if two products cost the same.
  2. LIMIT 3 keeps the first three rows once they are sorted. It does not change the catalogue.
  3. LIMIT without ORDER BY does not say which three rows they should be. The ordering is written before the limit.
  4. In the challenge, LIMIT cuts right inside a tie: you will see why a second key is needed.

Practice with help

Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.

Guided practice

Choose the two lowest stock levels

Not started

Write a query that returns the two products with the least stock, showing name and stock. If they are tied, sort by name alphabetically.

Expected result: 2 rows with the columns name, stock, in that order. Keep the row order requested.

View tables and data

Open this panel to see the full data.

Your turn

Solve this variation. Spot the part that has changed before you look for help.

Practice

Cut the list inside a tie

Not started

Show id, name and price of the two most expensive products. If two prices tie, the lower id wins.

Expected result: 2 rows with columns id, name, price, in that order. Respect the requested row order.

This activity uses a copy of the data with extra edge cases. Open the full data before you predict the result.

View tables and data

Open this panel to see the full data.

Key takeaways

  • ORDER BY sets the order of the rows.
  • ASC goes from lowest to highest; DESC, from highest to lowest.
  • A second key breaks ties.
  • LIMIT takes the first rows after sorting.

Next: Avoid duplicates with DISTINCT

Go deeper

To read the next slice, LIMIT 3 OFFSET 3 skips the first three sorted rows. Keep the same ordering and tiebreaker between slices. Variants of this syntax depend on the engine.

Going further: read the next three prices, keeping the same order.

SQL
SELECT id, name, price FROM products ORDER BY price DESC, id ASC LIMIT 3 OFFSET 3;
Example result: 3 rows
idnameprice
8SSD Drive89
5Headphones69.9
10Speaker54
  1. OFFSET 3 skips the first three rows of the sorted result. LIMIT 3 takes the next ones. For another page, change the offset while keeping the ordering and the tiebreaker.