Kodewick
Lesson 4 of 21Contents

Sorting, LIMIT and DISTINCT

Lesson 4 of 21 · Part 1 · Querying data · about 15 minutes

So far, the database has decided the order of the rows in every result. It looks like they come by id, but nothing promises that: in another database, or with more data, the order can change. When you want a fixed order, add ORDER BY:

main.sql
1SELECT name, price2FROM products3ORDER BY price;
Output
nameprice
Notebook4.50
Pens (10)6.00
USB cable8.50
Coffee mug9.90
Tote bag11.00
Water bottle12.00
Phone case15.00
Desk lamp25.00
Backpack39.90
Headphones59.00
10 rows

ORDER BY

ORDER BY column sorts by that column from small to large. It goes after WHERE. Numbers compare by size, dates by earlier and later. Customers sorted by the date they joined, earliest first:

main.sql
1SELECT name, joined2FROM customers3ORDER BY joined;
Output
namejoined
Ana Silva2025-01-12
Ben Okafor2025-02-03
Chloe Martin2025-02-20
David Kim2025-03-15
Emma Novak2025-04-01
Farah Khan2025-05-18
George Brown2025-06-09
Hana Sato2025-07-21
Ivan Petrov2025-08-30
Julia Costa2025-09-14
10 rows

Small to large is ascending, written ASC. It's what you get when you write nothing, so ORDER BY joined ASC is the same as the query above.

Large to small: DESC

Put DESC after the column name for descending order, large to small. The newest orders first:

main.sql
1SELECT id, order_date, status2FROM orders3WHERE status = 'shipped'4ORDER BY order_date DESC;
Output
idorder_datestatus
112025-10-22shipped
102025-10-20shipped
82025-10-12shipped
62025-10-03shipped
52025-10-01shipped
32025-09-18shipped
22025-09-05shipped
12025-09-02shipped
8 rows

This query first picks the shipped orders with WHERE, then sorts them. The column you sort by doesn't have to be selected either: ORDER BY stock works fine while showing only name.

How text sorts

Text can be sorted too, letter by letter from A to Z:

main.sql
1SELECT name2FROM products3ORDER BY name;
Output
name
Backpack
Coffee mug
Desk lamp
Headphones
Notebook
Pens (10)
Phone case
Tote bag
USB cable
Water bottle
10 rows

From A to Z: the backpack comes first and the water bottle last. Capital letters or not makes no difference to the order in MySQL.

Sorting by several columns

ORDER BY can take several columns, separated by commas: rows are sorted by the first column, and rows with the same first column are sorted by the second. Each column gets its own ASC or DESC:

main.sql
1SELECT name, category, price2FROM products3ORDER BY category, price DESC;
Output
namecategoryprice
BackpackBags39.90
Tote bagBags11.00
HeadphonesElectronics59.00
Desk lampElectronics25.00
Phone caseElectronics15.00
USB cableElectronics8.50
Water bottleKitchen12.00
Coffee mugKitchen9.90
Pens (10)Stationery6.00
NotebookStationery4.50
10 rows

Products of the same category end up together, and within a category the priciest come first. Press Step to see ORDER BY put the rows in a new order.

Tip: When the first column has repeated values, add one more column (id, say) to fix the order; otherwise which of those rows comes first is still up to the database.

The top few: LIMIT

LIMIT 3 keeps only the first 3 rows of the result, and it goes last. Together with ORDER BY, it gives you "the top 3". The 3 priciest products:

main.sql
1SELECT name, price2FROM products3ORDER BY price DESC4LIMIT 3;
Output
nameprice
Headphones59.00
Backpack39.90
Desk lamp25.00
3 rows

Press Step: first the rows are sorted from the highest price down, then LIMIT keeps only the first 3. LIMIT 3 without ORDER BY just means "any 3 rows", which is rarely what you want.

LIMIT is how MySQL writes it (SQLite and PostgreSQL accept it too). Other databases may write FETCH FIRST 3 ROWS ONLY (the standard way), and SQL Server writes SELECT TOP 3 ….

Guess first

Which product is the result?

1SELECT name, stock2FROM products3ORDER BY stock4LIMIT 1;

Dropping repeats: DISTINCT

Which cities do the customers come from? Ask for city alone and the same city shows up several times:

main.sql
1SELECT city2FROM customers;
Output
city
London
Paris
London
Tokyo
Berlin
Paris
Tokyo
London
Berlin
Sydney
10 rows

SELECT DISTINCT keeps only one of each set of identical rows:

main.sql
1SELECT DISTINCT city2FROM customers3ORDER BY city;
Output
city
Berlin
London
Paris
Sydney
Tokyo
5 rows

Five cities, each once, from Berlin to Tokyo. With several columns after DISTINCT, repeats are judged on all of them together: two rows are repeats only if every column is the same.

Practice

Write the query, then press Check. A hint costs nothing; the answer is there when you need it.

Exercise 1

Top 3 by stock

Get the 3 products with the most stock, with two columns, name and stock, largest stock first.

It should print
namestock
USB cable200
Notebook120
Pens (10)80
3 rows
exercise1.sql
1SELECT name, stock2FROM products;
Output
Press Run or Ctrl+Enter
Exercise 2

Newest pending orders

Get the pending orders, with two columns, id and order_date, newest first.

It should print
idorder_date
122025-10-25
92025-10-15
72025-10-10
3 rows
exercise2.sql
1SELECT id, order_date2FROM orders3ORDER BY order_date;
Output
Press Run or Ctrl+Enter
Exercise 3

Customers who ordered

Which customer numbers appear in the orders table? Show one column, customer_id, each number once, smallest first.

It should print
customer_id
1
2
3
4
5
7
8
7 rows
exercise3.sql
1SELECT customer_id2FROM orders;
Output
Press Run or Ctrl+Enter

What you learned

  • Without ORDER BY, nothing promises the order of the rows; for a fixed order, write ORDER BY.
  • ASC sorts small to large (and is the default), DESC large to small; text goes A to Z, but accented letters and other scripts may not land where you expect.
  • ORDER BY a, b sorts by a, and rows with the same a by b; each column has its own direction.
  • LIMIT n keeps the first n rows, and with ORDER BY gives "the top n"; other databases may write FETCH FIRST n ROWS ONLY or TOP n.
  • SELECT DISTINCT drops identical rows.