Without instructions, a database may return rows in any order it likes. ORDER BY sorts the result, LIMIT keeps only the first few rows, and DISTINCT removes duplicates. Together they answer everyday questions such as “who are the top three students?” or “which cities do our students come from?” and power page-by-page lists on websites.
In this lesson you will learn
- How to sort ascending and descending
- How to sort by more than one column
- Where
NULLvalues end up - How to return the top N rows with
LIMIT - Pagination with
OFFSET, andDISTINCT
Sorting with ORDER BY
-- Highest CGPA first
SELECT name, cgpa FROM students ORDER BY cgpa DESC;
-- By department, then by name within each department
SELECT dept, name FROM students ORDER BY dept, name;
Output
+--------+------+
| name | cgpa |
+--------+------+
| Tania | 3.95 |
| Nusrat | 3.82 |
| Mitu | 3.67 |
| Sabbir | 3.55 |
| Arif | 3.41 |
| Imran | 3.1 |
| Rafi | 2.98 |
| Nadia | NULL |
+--------+------+
(8 rows)
+------+--------+
| dept | name |
+------+--------+
| BBA | Imran |
| BBA | Sabbir |
| CSE | Mitu |
| CSE | Nadia |
| CSE | Nusrat |
| CSE | Tania |
| EEE | Arif |
| EEE | Rafi |
+------+--------+
(8 rows)
ASC(ascending, the default) sorts from small to large and A to Z;DESCreverses it.- With several columns, the second is used only to break ties in the first. Students are grouped by department, then sorted by name inside each one.
- Where
NULLappears depends on the database. SQLite and MySQL treat it as the smallest value, so Nadia is last in descending order; PostgreSQL does the opposite. AddNULLS LASTorNULLS FIRSTwhere supported to be explicit.
LIMIT, OFFSET and DISTINCT
-- Top 3 students
SELECT name, cgpa FROM students WHERE cgpa IS NOT NULL ORDER BY cgpa DESC LIMIT 3;
-- Page 2 of a list, 3 rows per page
SELECT id, name FROM students ORDER BY id LIMIT 3 OFFSET 3;
-- Unique cities, sorted
SELECT DISTINCT city FROM students ORDER BY city;
Output
+--------+------+
| name | cgpa |
+--------+------+
| Tania | 3.95 |
| Nusrat | 3.82 |
| Mitu | 3.67 |
+--------+------+
(3 rows)
+----+-------+
| id | name |
+----+-------+
| 4 | Imran |
| 5 | Mitu |
| 6 | Rafi |
+----+-------+
(3 rows)
+------------+
| city |
+------------+
| Chattogram |
| Dhaka |
| Khulna |
| Rajshahi |
| Sylhet |
+------------+
(5 rows)
ORDER BY ... DESC LIMIT 3is the standard “top 3” pattern. TheWHERE cgpa IS NOT NULLkeeps missing values out of a ranking.LIMIT 3 OFFSET 3skips 3 rows and returns the next 3, which is page 2 when each page shows 3 rows. In general,OFFSET = (page - 1) × page_size.DISTINCTremoves duplicate rows from the result, so each city appears once.
Same Idea, Different Syntax
| Database | Top 3 rows |
|---|---|
| SQLite, MySQL, PostgreSQL | ... ORDER BY cgpa DESC LIMIT 3 |
| SQL Server | SELECT TOP 3 ... ORDER BY cgpa DESC |
| Standard SQL, Oracle 12c+ | ... ORDER BY cgpa DESC FETCH FIRST 3 ROWS ONLY |
Clause Order
Clauses must be written in this order: SELECT → FROM → WHERE → GROUP BY → HAVING → ORDER BY → LIMIT. Putting ORDER BY before WHERE is a syntax error.
Common Mistakes
- Using
LIMITwithoutORDER BY. You get “some” rows, not the top ones, and the result can change between runs. - Forgetting
DESCwhen asking for the highest values. - Large
OFFSETvalues on big tables, which get slow because the database still reads the skipped rows.
Practice Exercises
Exercise 1. List the two lowest CGPAs (ignore missing values).
Show answer
SELECT name, cgpa FROM students
WHERE cgpa IS NOT NULL
ORDER BY cgpa ASC LIMIT 2;
-- Rafi 2.98, Imran 3.1
Exercise 2. List each department only once, in alphabetical order.
Show answer
SELECT DISTINCT dept FROM students ORDER BY dept; -- BBA, CSE, EEE
Frequently Asked Questions
Can I sort by a column that is not in the SELECT list?
Yes, in most databases. SELECT name FROM students ORDER BY cgpa DESC works. The exception is with DISTINCT, where the sort column usually has to be selected.
Can I sort by column position?
Yes, ORDER BY 2 sorts by the second selected column, but naming the column is clearer and safer.
Summary
ORDER BY col ASC|DESCsorts; extra columns break ties.LIMIT nkeeps the first n rows;OFFSETskips rows for paging.DISTINCTremoves duplicate rows.- Always pair
LIMITwithORDER BY.
Next: INSERT, UPDATE and DELETE.
