After finding the correct rows, you often need to control their display order. Products may go from cheapest to most expensive, orders may show newest first, and rankings put the highest score first. SQL sorts with ORDER BY; LIMIT can then keep only the first few rows.
Sorting with ORDER BY
ORDER BY price sorts by price. Without a direction, many databases default to ascending; ORDER BY price ASC makes that intent explicit.
| id | name | price | category |
|---|---|---|---|
| 1 | Keyboard | 80.00 | Electronics |
| 2 | Mouse | 40.00 | Electronics |
| 3 | Coffee Mug | 15.00 | Home |
| 4 | Backpack | 60.00 | Fashion |
SELECT *
FROM products
ORDER BY price ASC;| Direction | Numbers | Dates |
|---|---|---|
ASC ↑ | Low to high | Early to late |
DESC ↓ | High to low | Late to early |
DESC from high to low
ORDER BY price DESC produces Keyboard, Backpack, Mouse, then Coffee Mug. Text follows the database's collation rules. Dates sort too, so messages, orders, and logs often use ORDER BY order_date DESC to put the newest records first.
Sorting by several columns
ORDER BY category ASC, price DESC compares category first. It compares price only when two rows share a category. Multi-column sorting is common in reports and administration lists.
SELECT *
FROM products
ORDER BY category ASC, price DESC;Limiting the row count
A table may contain millions of rows while a page needs only the three most expensive products. Sort by price descending, then add LIMIT 3. LIMIT without ORDER BY returns some rows, but you cannot rely on id, price, or any other stable order. Rows have no natural order you should assume.
SELECT *
FROM products
ORDER BY price DESC
LIMIT 3;MySQL, PostgreSQL, and SQLite commonly use LIMIT. SQL Server often uses TOP or pagination syntax. Product syntax varies; the idea remains: define the order, then choose how many rows to keep.
Combining WHERE, ORDER BY, and LIMIT
“Find products above 20, sort high to low, and show two” breaks into products, price > 20, price DESC, and LIMIT 2. A query is not one mysterious action; several simple rules shape its result set.
SELECT name, price
FROM products
WHERE price >= 40
ORDER BY price DESC
LIMIT 3;Reorder a real result set
Change ASC, DESC, sort columns, and LIMIT, then run the query to inspect the real row order.
| id | name | city | |
|---|---|---|---|
| 1 | Alice | [email protected] | Tokyo |
| 2 | Bob | [email protected] | Osaka |
| 3 | Chris | [email protected] | Tokyo |
| 4 | Diana | [email protected] | Kyoto |
Sandbox ready; no real database is connected.KNOWLEDGE QUIZ
Write stable top-N queries
Write two queries: (1) return the three most expensive products above 20, showing only name and price; (2) return all orders newest date first, then highest id first within the same date. Acceptance criteria: correct clause order, explicit descending rules, and LIMIT 3 in the first query.
Optional: portable practice data
DROP TABLE IF EXISTS orders;
DROP TABLE IF EXISTS products;
CREATE TABLE products (id INTEGER, name VARCHAR(100), price DECIMAL(10,2), category VARCHAR(50));
CREATE TABLE orders (id INTEGER, user_id INTEGER, product_id INTEGER, quantity INTEGER, order_date DATE);
INSERT INTO products VALUES
(1,'Keyboard',80.00,'Electronics'), (2,'Mouse',40.00,'Electronics'),
(3,'Coffee Mug',15.00,'Home'), (4,'Backpack',60.00,'Fashion');
INSERT INTO orders VALUES
(1,1,2,1,'2026-08-01'), (2,1,3,2,'2026-08-03'),
(3,2,1,1,'2026-08-05'), (4,3,4,1,'2026-08-06');You have completed your first querying toolkit. You can now choose columns, filter rows, express patterns and ranges, handle missing values, and arrange a useful result. Later queries will keep building on the same users, products, and orders data. The SQL will grow, but the method remains the same: break one question into clear rules.