ORDER BY, LIMIT & DISTINCT
Sort your results, cap how many rows come back, and remove duplicates — three tools you will use in almost every query.
ORDER BY sorts, LIMIT caps, DISTINCT deduplicates. Together they give you clean, controlled output.
EXERCISE
1ORDER BY — Sorting Your Results
ORDER BY controls the order rows come back in. Without it, the database returns rows in whatever order it finds them — which is unpredictable.
Basic ORDER BY:
-- Sort by price, cheapest first (ASC is the default):
SELECT name, price
FROM products
ORDER BY price ASC;
-- Sort by price, most expensive first:
SELECT name, price
FROM products
ORDER BY price DESC;
Sorting by multiple columns:
-- Sort by category A-Z, then by price cheapest-first within each category:
SELECT name, category, price
FROM products
ORDER BY category ASC, price ASC;
-- Sort by salary highest-first, then by name A-Z for ties:
SELECT name, department, salary
FROM employees
ORDER BY salary DESC, name ASC;
Sorting by column position (less readable but valid):
-- Column 2 = price, column 1 = name:
SELECT name, price FROM products ORDER BY 2 DESC;
-- Avoid this in production code — position can change if query changes
Sorting by calculated values:
-- Sort by total value of stock (price × quantity):
SELECT name, price, stock_qty, price * stock_qty AS stock_value
FROM products
ORDER BY stock_value DESC;
-- Or reference the calculation directly (MySQL supports this):
ORDER BY price * stock_qty DESC;
NULL values in ORDER BY:
In MySQL, NULLs appear first when sorting ASC and last when sorting DESC.
Pro Tip: ORDER BY is processed last in SQL (after WHERE, GROUP BY, HAVING). You can reference alias names from SELECT in ORDER BY, which is a useful shortcut.
EXERCISE
2LIMIT & OFFSET — Controlling How Many Rows
LIMIT caps how many rows come back. OFFSET skips the first N rows. Together, they power pagination — the "next page" functionality in every app.
LIMIT — get the top N rows:
-- Top 5 most expensive products:
SELECT name, price
FROM products
ORDER BY price DESC
LIMIT 5;
-- Single most recent order:
SELECT * FROM orders
ORDER BY created_at DESC
LIMIT 1;
OFFSET — skip rows (for pagination):
-- Page 1: rows 1-10
SELECT * FROM products ORDER BY id LIMIT 10 OFFSET 0;
-- Page 2: rows 11-20
SELECT * FROM products ORDER BY id LIMIT 10 OFFSET 10;
-- Page 3: rows 21-30
SELECT * FROM products ORDER BY id LIMIT 10 OFFSET 20;
Shorthand syntax (LIMIT rows OFFSET skip):
-- Same as LIMIT 10 OFFSET 20:
SELECT * FROM products ORDER BY id LIMIT 20, 10;
-- Format: LIMIT skip, rows_to_return
Real-world pagination:
-- Given: page_number = 3, items_per_page = 20
-- Offset formula: (page_number - 1) * items_per_page = (3-1) * 20 = 40
SELECT id, name, price
FROM products
WHERE is_active = 1
ORDER BY name ASC
LIMIT 20 OFFSET 40;
Pro Tip: Always use ORDER BY with LIMIT. Without ORDER BY, the "top 5" you get back is arbitrary — the database returns whatever it found first, which changes as the table changes.
EXERCISE
3DISTINCT — Removing Duplicate Rows
DISTINCT returns only unique rows — de-duplicating based on all the columns you selected. It is essential when you want to know "what values exist" in a column.
Basic DISTINCT:
-- Without DISTINCT — every row, including duplicates:
SELECT city FROM customers;
-- Output: Mumbai, Mumbai, Delhi, Bangalore, Delhi, Mumbai...
-- With DISTINCT — each unique value once:
SELECT DISTINCT city FROM customers;
-- Output: Mumbai, Delhi, Bangalore
DISTINCT across multiple columns:
-- Unique combinations of city + status:
SELECT DISTINCT city, status
FROM customers;
-- Returns each unique PAIR, not each unique city AND each unique status separately
Common use cases:
-- What categories exist in products?
SELECT DISTINCT category FROM products ORDER BY category;
-- Which customers have placed orders? (unique customer_ids in orders table)
SELECT DISTINCT customer_id FROM orders ORDER BY customer_id;
-- What years have orders been placed?
SELECT DISTINCT YEAR(order_date) AS year
FROM orders
ORDER BY year;
DISTINCT vs GROUP BY:
-- These return identical results:
SELECT DISTINCT city FROM customers ORDER BY city;
SELECT city FROM customers GROUP BY city ORDER BY city;
-- DISTINCT is cleaner for simple deduplication.
-- GROUP BY is better when you also need aggregation (COUNT, SUM, etc.).
Pro Tip: DISTINCT applies to the entire row of selected columns, not just the first column.
SELECT DISTINCT city, countrygives unique city+country combinations, not unique cities.
