ORDER BY sorts the result, LIMIT caps the rows and DISTINCT removes duplicates.
The 5 most expensive products. DESC is descending, ASC ascending (the default). Open in SQL Lab
Sorting by several columns: category first, then price. Open in SQL Lab
The distinct cities with no repeats. Open in SQL Lab
OFFSET skips rows and is used for paging. Open in SQL Lab
For advanced readers NULLs and performance
NULL ordering differs: SQLite and MySQL treat NULL as smallest; PostgreSQL and Oracle put NULLs last by default and let you set NULLS FIRST/LAST. Large OFFSET is slow because rows are read then discarded; for deep paging use keyset paging (WHERE id > last id).
Exercise 1 — Show the name and price of the 3 most expensive products.
Sort by price descending, then LIMIT 3.
Exercise 2 — Show the distinct customer cities in alphabetical order.
DISTINCT then ORDER BY.
Exercise 3 — Show id and order_date of the latest 5 orders by date, ties broken by id descending.
Two sort columns: order_date DESC, id DESC.
- ORDER BY sorts; DESC is descending.
- LIMIT and OFFSET cap rows and page.
- DISTINCT removes duplicates.
Want an expert to do it for you?
Gomaa Intelligence turns your data into clear reports and decisions. Tell us your problem and we will reply.