Source: SQLBolt — Lesson 4
Query results can contain duplicate values across rows. Use DISTINCT to discard rows with duplicate column values.
SELECT DISTINCT column, another_column, …
FROM mytable
WHERE condition(s);Data in a database is rarely stored in a useful order. Use ORDER BY to sort results alphanumerically by a column, ascending or descending.
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC;Use LIMIT to cap the number of rows returned, and OFFSET to skip a number of rows before starting to count.
SELECT column, another_column, …
FROM mytable
WHERE condition(s)
ORDER BY column ASC/DESC
LIMIT num_limit OFFSET num_offset;Note:
LIMITandOFFSETare applied last, after all other clauses. This is covered in detail in Lesson 12.
This lesson uses a scrambled version of the movies table to mimic real-world data. Note that the column is named
directorhere.
| id | title | director | year | length_minutes |
|---|---|---|---|---|
| 1 | A Bug's Life | John Lasseter | 1998 | 95 |
| 2 | Ratatouille | Brad Bird | 2007 | 115 |
| 3 | Brave | Brenda Chapman | 2012 | 102 |
| 4 | Cars | John Lasseter | 2006 | 117 |
| 5 | Monsters, Inc. | Pete Docter | 2001 | 92 |
| 6 | Toy Story 2 | John Lasseter | 1999 | 93 |
| 7 | Finding Nemo | Andrew Stanton | 2003 | 107 |
| 8 | Toy Story | John Lasseter | 1995 | 81 |
| 9 | WALL-E | Andrew Stanton | 2008 | 104 |
| 10 | Up | Pete Docter | 2009 | 101 |
| 11 | The Incredibles | Brad Bird | 2004 | 116 |
| 12 | Cars 2 | John Lasseter | 2011 | 120 |
| 13 | Toy Story 3 | Lee Unkrich | 2010 | 103 |
| 14 | Monsters University | Dan Scanlon | 2013 | 110 |
SELECT DISTINCT director FROM movies
ORDER BY director ASC;Output:
| director |
|---|
| Andrew Stanton |
| Brad Bird |
| Brenda Chapman |
| Dan Scanlon |
| John Lasseter |
| Lee Unkrich |
| Pete Docter |
SELECT title, year FROM movies
ORDER BY year DESC
LIMIT 4;Output:
| title | year |
|---|---|
| Monsters University | 2013 |
| Brave | 2012 |
| Cars 2 | 2011 |
| Toy Story 3 | 2010 |
SELECT title FROM movies
ORDER BY title ASC
LIMIT 5;Output:
| title |
|---|
| A Bug's Life |
| Brave |
| Cars |
| Cars 2 |
| Finding Nemo |
SELECT title FROM movies
ORDER BY title ASC
LIMIT 5 OFFSET 5;Output:
| title |
|---|
| Monsters University |
| Monsters, Inc. |
| Ratatouille |
| The Incredibles |
| Toy Story |
DISTINCTremoves duplicate values from results — useful when selecting a single column like a list of directorsORDER BY column ASCsorts A→Z or lowest→highest;DESCreverses itLIMIT nreturns at mostnrows;OFFSET nskips the firstnrows before counting- Combining
ORDER BY+LIMIT+OFFSETis the standard pattern for pagination