The Beginner's Checklist for Writing a SELECT Query
SELECT queries are built in layers. This checklist follows the order most people find easiest: decide what you want, where it lives, which rows to keep, and how to present them. Tick each item as you go.
Before you type
- [ ] Say the question out loud. "Which members joined this year, newest first?" A clear question gives a clear query.
- [ ] Look at the table structure. Check the exact column names; a typo in a column name is the most common error.
Build the query in layers
- [ ] Start with the table.
SELECT * FROM members;Run it to see what the data looks like. - [ ] Pick the columns you need. Replace
*with names, such asname, joined_on. Fewer columns are easier to read. - [ ] Add WHERE to keep the right rows.
WHERE joined_on >= '2025-01-01'. Run it again and check. - [ ] Quote text and dates; do not quote numbers.
'Paris'and'2025-01-01'need quotes;42does not. - [ ] Use brackets when mixing AND and OR. Brackets make the meaning certain.
- [ ] Add ORDER BY if order matters. Without it, the database may return rows in any order.
- [ ] Add LIMIT when you only need a few rows. Handy for "top 10" lists and for testing on big tables.
Watch out for NULL
- [ ] Use IS NULL and IS NOT NULL.
= NULLnever matches anything. - [ ] Remember that conditions skip unknown values.
WHERE age > 30leaves out rows where age is NULL. - [ ] Remember that COUNT(column) skips NULLs while
COUNT(*)counts every row.
When you need totals
- [ ] Use GROUP BY with COUNT, SUM or AVG.
SELECT city, COUNT(*) FROM members GROUP BY city; - [ ] Every non-total column must be in GROUP BY. Otherwise the database does not know which value to show, and many databases will give an error.
- [ ] Filter groups with HAVING, rows with WHERE.
HAVING COUNT(*) > 5keeps only big groups.
When you need another table
- [ ] Join on the linking columns. Usually a foreign key to a primary key.
- [ ] Prefix column names with the table name.
members.nameavoids "ambiguous column" errors. - [ ] Choose INNER or LEFT JOIN on purpose. Our comparison of JOIN types helps.
Check the result
- [ ] Does the number of rows make sense? Predict it first.
- [ ] Spot-check a row or two against the raw table.
- [ ] Save the query in a notes file with the question it answers.
In your app
- [ ] Pass user values as parameters, never by gluing text into the SQL. This prevents SQL injection.
The query notebook template is a nice place to save your finished queries, and the visual query builder can show you the shape of each clause.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.