Picking rows with WHERE
Keep only the books of one genre.
WHERE keeps the rows that pass a test, like a filter in Excel. Text goes in single quotes.
Show the title and genre of the Mystery books.
| book_id | title | author | genre | year | pages |
|---|---|---|---|---|---|
| 1 | Angels & Demons | Dan Brown | Thriller | 2000 | 616 |
| 2 | The Fellowship of the Ring | J.R.R. Tolkien | Fantasy | 1954 | 423 |
| 3 | War and Peace | Leo Tolstoy | Fiction | 1869 | 1225 |
| 4 | Animal Farm | George Orwell | Fiction | 1945 | 112 |
| 5 | The Shining | Stephen King | Horror | 1977 | 447 |
| 6 | Gone Girl | Gillian Flynn | Thriller | 2012 | 419 |
| 7 | The Sign of the Four | Arthur Conan Doyle | Mystery | 1890 | 122 |
| 8 | Harry Potter and the Philosopher's Stone | J.K. Rowling | Fantasy | 1997 | 223 |
| 9 | Sense and Sensibility | Jane Austen | Romance | 1811 | 400 |
| 10 | The Hound of the Baskervilles | Arthur Conan Doyle | Mystery | 1902 | 256 |
| 11 | The Great Gatsby | F. Scott Fitzgerald | Fiction | 1925 | 180 |
Run your query to see the result table.
Press the green Run button or Ctrl+Enter.
How WHERE works
YOU WRITE
- SELECT
- FROM
- WHERE
- GROUP BY
- HAVING
- ORDER BY
- LIMIT
SQL WORKS THROUGH IT
- 1. FROM take the table
- 2. WHERE keep some rows
- 3. GROUP BY make piles
- 4. HAVING keep some piles
- 5. SELECT pick and work out columns
- 6. ORDER BY sort
- 7. LIMIT keep the first few
WHERE picks the rows before SELECT picks the columns.
What does
WHEREdo?WHEREkeeps only the rows where a condition is true. Every other row is left out of the result (the table itself doesn't change).Where does
WHEREgo?After
FROM:SELECT title, genre FROM books WHERE genre = 'Horror';Why single quotes?
Text values go in single quotes:
'Horror'. Without quotes, SQL thinks Horror is the name of a column. Numbers don't need quotes:WHERE year = 1997.Do capitals matter in the text?
In SQLite, yes:
'horror'doesn't match'Horror'. Spell the value as it's stored. Some databases ignore capitals; the table below shows which.How is this like Excel?
It's the same idea as a filter on one column, or the
FILTERfunction:=FILTER(B2:D12, D2:D12="Horror").
How other databases do it
The lessons run on SQLite. Most of SQL is the same everywhere; these are the places where this lesson's SQL differs.
| Database | How | Note |
|---|---|---|
| SQLite (these lessons) | genre = 'Horror' | Double quotes mean a column name, but when there is no such column SQLite quietly reads "Horror" as text. Don't rely on it. |
| PostgreSQL | genre = 'Horror' | Double quotes are always a column name, so "Horror" is an error. |
| MySQL / MariaDB | genre = 'Horror' | Double quotes also work for text, unless the ANSI_QUOTES mode is on. |
| SQL Server | genre = 'Horror' | Double quotes are a column name (QUOTED_IDENTIFIER is on by default). |
| Oracle | genre = 'Horror' | Double quotes are always a column name. |
| Sybase ASE | genre = 'Horror' | Double quotes also work for text, because quoted_identifier is off by default. |
| Database | How | Note |
|---|---|---|
| SQLite (these lessons) | No | = cares about capitals. LIKE ignores them (for A to Z). |
| PostgreSQL | No | = cares about capitals. Use ILIKE, or LOWER(genre) = 'horror'. |
| MySQL / MariaDB | Yes | The usual collations ignore capitals. |
| SQL Server | Yes | The default collation ignores capitals (a case-sensitive one can be chosen). |
| Oracle | No | = cares about capitals. Use UPPER(genre) = 'HORROR'. |
| Sybase ASE | No | The default binary sort order cares about capitals. |
Know someone fighting with a spreadsheet? Send them this page.