Q1Which of the following correctly describes SELECT DISTINCT?
DISTINCT, ORDER BY, LIMIT — Shaping the Result Set
Learn SQL DISTINCT, ORDER BY, LIMIT, and OFFSET. Cover deduplication, sorting, row limiting, and pagination with OFFSET — all running live in your browser against a CSV-loaded score dataset.
Data we'll use — the score table loaded from CSV
This article uses a `score` table loaded automatically from a CSV file to step through the four constructs that shape SELECT output — DISTINCT (deduplication), ORDER BY (sorting), LIMIT (row count), and OFFSET (skip from the top).
The score table has 5 columns — id / name / subject / score / recorded_on — with 15 rows total: 5 students (Alice / Bob / Carol / Dave / Eve) each took 3 subjects (Math / English / Science).
DISTINCT — return only unique rows
A SELECT result can contain the same row multiple times. For example, pulling just the name column from score returns 15 rows — Alice / Bob / Carol / Dave / Eve, three subjects each.
Writing `SELECT DISTINCT col, ...` collapses duplicate rows into one in the result set. With multiple columns, rows are considered duplicates when the combination of columns matches.
-- List unique student names
SELECT DISTINCT name FROM score;
-- List unique subjects
SELECT DISTINCT subject FROM score;
-- Dedup on the (name, subject) combination (= all 15 rows are unique)
SELECT DISTINCT name, subject FROM score;
ORDER BY — sort the result
SQL does not guarantee row order by default — SELECT * FROM score; lets the DB implementation decide what order rows come back in. When you want a specific order, add `ORDER BY col [ASC|DESC]`.
- ASC (ascending / small → large) is the default and can be omitted
- DESC (descending / large → small) must be written explicitly
- A comma-separated list of multiple columns gives you a multi-level sort: rows tied on the first column get ordered by the second
-- 1) Single-column ascending (ASC can be omitted)
SELECT name, score FROM score ORDER BY score;
-- 2) Single-column descending
SELECT name, score FROM score ORDER BY score DESC;
-- 3) Multi-column sort (by subject, then highest score first within a subject)
SELECT name, subject, score FROM score ORDER BY subject ASC, score DESC;
Fine print on ORDER BY
- ORDER BY goes after `WHERE` and before `LIMIT`. Memorize the order SELECT cols FROM table WHERE condition ORDER BY col DESC LIMIT N; and you won't get tripped up.
- The right-hand side of ORDER BY can take a column number (ORDER BY 2 DESC sorts by the 2nd selected column), but it hurts readability — the convention is to use column names.
- In this course's console, string sorting is uppercase → lowercase (ASCII order). Some DBs treat 'A' and 'a' as equivalent.
LIMIT and OFFSET — narrow the count and paginate
When result sets get large — 100 rows, 10,000 rows — dumping everything to the screen is heavy and the user can't take it in. Adding `LIMIT N` returns only the first N rows. Combine it with `OFFSET M` to skip the first M rows and then take N, which gives you pagination for things like "items 6–10" or "items 11–20."
-- 1) Take the top 5 (ranking TOP 5)
SELECT name, subject, score FROM score
ORDER BY score DESC
LIMIT 5;
-- 2) Take items 6 through 10 (page 2)
SELECT name, subject, score FROM score
ORDER BY score DESC
LIMIT 5 OFFSET 5;
-- 3) Pagination formula: page N, K rows per page
-- LIMIT K OFFSET (N - 1) * K
-- e.g. page 3, 5 per page → LIMIT 5 OFFSET 10
| Page | Rows per page | OFFSET | LIMIT | Target rank |
|---|---|---|---|---|
| 1 | 5 | 0 | 5 | 1–5 |
| 2 | 5 | 5 | 5 | 6–10 |
| 3 | 5 | 10 | 5 | 11–15 |
| 1 | 10 | 0 | 10 | 1–10 |
| 2 | 10 | 10 | 10 | 11–20 |
| 1 | 20 | 0 | 20 | 1–20 |
LIMIT without ORDER BY doesn't guarantee order
Writing just `LIMIT` and skipping `ORDER BY` like SELECT * FROM score LIMIT 5; leaves the choice of which 5 rows come back up to the DB implementation. Even on a DB that tends to return rows in insert order (like SQLite), the SQL standard says order is undefined. On MySQL / PostgreSQL, indexes and execution plans can swap which rows you get.
For anything where order matters — rankings, pagination, "first N items" — always write `ORDER BY` before `LIMIT`.
Knowledge Check
Answer each question one by one.
Q2What does SELECT name, score FROM score ORDER BY score DESC; return?
Q3What rows does SELECT * FROM score ORDER BY score DESC LIMIT 5 OFFSET 10; return?