SQL is how you ask a database a question. Almost every question starts with the same three words: SELECT (which columns you want), FROM (which table they live in), and WHERE (which rows to keep).
SELECT picks the columns, FROM names the table, WHERE keeps only the rows that match a condition. Master these three and you can already answer most everyday questions.
SELECT and FROM
The simplest query asks for some columns from a table. Here's a tiny employees table. Run it and see every row come back:
SELECT * is shorthand for "every column". Handy when exploring, but in real queries you usually name the columns you actually need.
WHERE keeps only the rows you want
Before the syntax, the picture. WHERE tests its condition against every row and keeps only the ones where it is true. Filter the five employees to just the engineers and three rows fall away:
| name | department | salary |
|---|---|---|
| Ada | Engineering | 62000 |
| Bo | Sales | 48000 |
| Cy | Engineering | 71000 |
| Di | Sales | 52000 |
| Eli | Marketing | 45000 |
WHERE department = 'Engineering'| name | department | salary |
|---|---|---|
| Ada | Engineering | 62000 |
| Cy | Engineering | 71000 |
Try it now: write the query the visual just showed: return the name and salary of every employee in Engineering.
Your turn · Exercise
Write the query the visual showed: name and salary of every employee in the Engineering department.
The data
employees
5 rows returned
| name | department | salary |
|---|---|---|
| Ada | Engineering | 62,000 |
| Bo | Sales | 48,000 |
| Cy | Engineering | 71,000 |
| Di | Sales | 52,000 |
| Eli | Marketing | 45,000 |
Add a WHERE clause to filter. The condition is tested against every row; only the rows where it's true come back. Run it, then change Engineering to Sales:
A few things to notice:
- Text values go in single quotes:
'Engineering'. Numbers don't:salary > 50000. =tests equality. You also have<,>,<=,>=, and<>(not equal).
Watch out
The single biggest beginner trip-up: using = to compare text but forgetting the quotes, or quoting a number. WHERE department = Engineering (no quotes) makes the database look for a column named Engineering and errors. Text → quotes; numbers → no quotes.
Putting it together
Read a query in the order the database cares about it: FROM a table, WHERE some rows match, then SELECT these columns. Even though you write SELECT first, it helps to think "which table, which rows, which columns."
From practice
On real systems you almost never want SELECT * in production code; naming columns makes the query faster, clearer, and safe against someone adding a giant column to the table later. Get the habit early.
Your turn
Write the query yourself. Return the name and salary of every employee in the Sales department.
Your turn · Exercise
Return the name and salary of every employee in the Sales department.
The data
employees
5 rows returned
| name | department | salary | city |
|---|---|---|---|
| Ada | Engineering | 62,000 | Antwerp |
| Bo | Sales | 48,000 | Ghent |
| Cy | Engineering | 71,000 | Antwerp |
| Di | Sales | 52,000 | Bruges |
| Eli | Marketing | 45,000 | Ghent |
Next: once you can pull the right rows, you'll want to sort them, take just the top few, and drop duplicates.