SELECT & WHERE
Having your data organized into tables is only half the story — you also need a way to ask
questions of it. "Show me every book" is easy. "Show me only the books published after 1970"
is where SQL actually starts to earn its keep. SELECT is how you ask for data;
WHERE is how you narrow it down to just the rows you care about.
💡 The Bottom Line
SELECT columns FROM table retrieves data; adding
WHERE condition filters it down to matching rows only. Stack
ORDER BY to control the sequence and LIMIT to cap how many rows
come back. Master these four keywords and you can already answer most everyday questions
about your data.
The basic shape of a query
Every query starts by naming the columns you want and the table to pull them from:
SELECT title, author_id
FROM books;
Want every column instead of naming them one by one? Use *:
SELECT * FROM books;
Filtering with WHERE
Without a WHERE clause, you get every row in the table. Add one to keep only
rows that match a condition:
SELECT title
FROM books
WHERE author_id = 1;
Comparison operators work the way you'd expect: =, != (or
<>), <, >, <=,
>=. Combine multiple conditions with AND / OR:
SELECT title
FROM books
WHERE author_id = 1 AND published_year > 1970;
🙋 There Are No Dumb Questions
Q: Why does the order matter — SELECT, then FROM, then WHERE?
A: SQL reads mostly like English: "select these columns, from this table, where this
condition holds." Under the hood the database actually evaluates FROM and
WHERE before it picks the columns you asked for, but you always
write the keywords in this SELECT → FROM → WHERE order.
Sorting with ORDER BY
Rows come back in no guaranteed order unless you ask for one. ORDER BY sorts
the results; add DESC to reverse it (default is ascending, ASC):
SELECT title, published_year
FROM books
ORDER BY published_year DESC;
⚠️ Watch Out
Don't assume rows come back "in the order they were inserted." Without an explicit
ORDER BY, a database is free to return rows in whatever order is fastest for it
— which can even change between runs. If order matters to your result, always say so with
ORDER BY.
Capping results with LIMIT
Only need the top few rows? LIMIT caps how many come back — handy paired with
ORDER BY for "top N" style questions:
-- the 3 most recently published books
SELECT title, published_year
FROM books
ORDER BY published_year DESC
LIMIT 3;
🔧 Try It Yourself
Using the books table from the last lesson (columns: id,
title, author_id, and imagine a published_year column
too), write a query that returns the titles of every book published before 1950, sorted
alphabetically by title.
✅ Quick Check
Which query returns the 5 oldest books by publication year, oldest first?
Next up: what happens when the data you need is split across two tables, like books and their authors? That's what JOINs are for.