SQL: Start Here
After this lesson, you will be able to:
- Describe a database as a set of tables, and a table as rows and columns
- Say what a query is and what SQL, short for Structured Query Language, is for
- Act out a short query by hand, one step at a time, and predict the result
- Read the five parts of a first query: SELECT, FROM, WHERE, ORDER BY and LIMIT
- Run a query in the browser and compare it with your hand result
#What a Database Question Looks Like
Think of a school office with a wall of filing cabinets. Each cabinet holds one kind of paper: one for student cards, one for fee receipts, one for exam marks. If the principal asks, "Which students from class 8 have paid the fee?", a clerk walks to the right cabinet, pulls out the matching cards, and brings back only those.
Notice what the clerk did not do. The clerk did not bring back every card and ask you to look through them. The clerk did not change any card. The clerk answered the question you asked and nothing else. Most of the queries you write in this lesson work the same way: they look, they pick, they bring back a list, and they leave the cabinet exactly as it was.
The principal asks, 'Which class 8 students have paid the fee?' What should the clerk hand back?
In one sentence: a database is a set of tables, a query is a question about them, and SQL is the language you write the question in.
#Tables, Rows and Columns
employees. To save room, I show four of its six columns.| id | name | department | salary |
|---|---|---|---|
| 1 | Alice Johnson | Engineering | 95000 |
| 2 | Bob Smith | Engineering | 85000 |
| 3 | Carol Davis | Marketing | 78000 |
| 4 | David Wilson | Engineering | 92000 |
| 5 | Eve Brown | Marketing | 72000 |
| 6 | Frank Miller | Sales | 88000 |
| 7 | Grace Lee | Sales | 76000 |
| 8 | Henry Chen | Engineering | 98000 |
| 9 | Iris Patel | Marketing | 81000 |
| 10 | Jack Taylor | Sales | 70000 |
salary column holds numbers. The name and department columns hold text. A single box, such as 78000, is a cell.id column? Two people can share a name, but no two rows share an id. The id is a label that points at exactly one row, the way a roll number points at exactly one student.Look at the employees table. How many rows have department equal to Sales?
In one sentence: a table is rows (one per thing) and columns (one kind of fact each), and a database is a set of such tables.
#Be the Database: Tracing a Query by Hand
Here is the first full query. Read it slowly. It looks like a sentence, and it nearly is.
SELECT name, salary
FROM employees
WHERE department = 'Marketing'
ORDER BY salary DESC
LIMIT 2;
In plain words: "From the employees table, keep only the Marketing rows, show me the name and salary, put the biggest salary first, and give me just two."
WHERE is a filter. Three rows survive:| id | name | department | salary |
|---|---|---|---|
| 3 | Carol Davis | Marketing | 78000 |
| 5 | Eve Brown | Marketing | 72000 |
| 9 | Iris Patel | Marketing | 81000 |
Seven rows are gone and they stay gone for the rest of this query. Notice the quote marks around Marketing. Text goes inside single quotes, so the database can tell a word you typed from the name of a column.
id and department columns with your hand. Two columns remain.| name | salary |
|---|---|
| Carol Davis | 78000 |
| Eve Brown | 72000 |
| Iris Patel | 81000 |
DESC is short for descending, which means biggest first. Without it the sort goes the other way, smallest first. After sorting:| name | salary |
|---|---|
| Iris Patel | 81000 |
| Carol Davis | 78000 |
| Eve Brown | 72000 |
LIMIT 2 cut off.The diagram is a simple picture. You write the clauses in a fixed order: SELECT first, FROM second. That is why the query reads like a sentence. The database may do its inner work in a different order, as long as the answer matches what you got by hand. For learning, the five steps above are the order to picture.
In one sentence: a query is five small jobs on a table, which are take it, filter the rows, choose the columns, sort, and cut to the first few.
#Run It and Compare
employees table. Nothing you do here can break anything, because it is a private practice copy.Do: Press Run. Then changeLIMIT 2toLIMIT 3and run again. Then deleteLIMIT 3but keep the semicolon, and run a third time.Notice: The first run gives Iris Patel 81000 and Carol Davis 78000, exactly your hand result. LIMIT 3 adds Eve Brown 72000. With no LIMIT you also get all three Marketing rows.Why: LIMIT only cuts the end of the list. There are just three Marketing rows, so asking for three or for no limit gives the same rows.
Now take the clauses one at a time. Each of these is a query you can predict before you run it.
SELECT name FROM employees; gives one column, all 10 rows. The star, SELECT *, means "every column".WHERE salary > 90000 keeps rows whose salary is bigger than 90000. Numbers need no quotes. For this table, three rows qualify: Alice Johnson at 95000, David Wilson at 92000 and Henry Chen at 98000.What does SELECT name, salary FROM employees WHERE salary > 90000; return?
Do: Run the query. Then change>to>=and a number such as 92000, and run again.Notice: With> 90000you get three rows. With>= 92000you still get three rows, because David Wilson earns exactly 92000 and>=includes him. With> 92000he drops out and two rows remain.Why:>means strictly bigger and>=means bigger or equal. A boundary value belongs to the answer only when you say "or equal".
One more practice, this time with dates. Predict first, then run.
Do: Predict the order of the three Sales people from the hire dates, then press Run. AddDESCafterhire_dateand run again.Notice: The order starts with Frank Miller (2018-11-01), then Grace Lee (2022-01-10), then Jack Taylor (2022-06-01). With DESC the order flips.Why: Dates written year first sort correctly as plain text, because the biggest unit comes first. The earliest date is the smallest one.
In one sentence: write the query, act it out, run it, and compare, and any gap between your hand answer and the database answer is exactly what to study.
#Four Beginner Slips and How to Spot Them
Everyone makes these. The database tells you when a query is broken, and the message is usually short and useful.
WHERE department = Marketing makes the database look for a column called Marketing. There is no such column, so it stops with an error. Put text in single quotes: 'Marketing'.SELECT nmae FROM employees fails because there is no column called nmae. The error names the word it did not recognise. Check the spelling against the table header.WHERE salary > '90000' puts digits in quotes, which makes them text. Some databases quietly turn the text back into a number and some compare it by alphabet rules, where '9' comes after '10'. You cannot rely on either, so leave numbers bare.SELECT name salary FROM employees is read as "name, but call it salary", which is a different thing. Separate columns with commas.Here is one more worth knowing. A query that runs with no error can still be a wrong answer to your question. The database does what you wrote, not what you meant. If the principal asks for class 8 and you filter for class 7, you get a clean, confident, wrong list. The hand trace is your defence: if you can act out the query and predict the result, you can tell when the result is off.
You run SELECT name FROM employees WHERE department = 'Marketing'; and get 3 names. You change 'Marketing' to 'marketing' with a small m. What do you expect in this playground?
In one sentence: quote your text, spell your column names, and check an answer that came back with no error, because the database does what you wrote and not what you meant.
#Where to Go Next
You can now read a first query, act it out, and run it. The lessons after this one slow down and practise each clause with more tables.
- Next: Reading a Query Before You Run It. It teaches how to read someone else's query, one trap with missing values, and a short checklist for SQL that an AI tool wrote for you.
- Then: Your First SQL Query and Filtering & Sorting Data go deeper on SELECT, WHERE and ORDER BY.
- After that: Aggregate Functions & GROUP BY answers questions like "how many" and "what is the average".
This track has levels. This is the start, Level 0. After it come the core queries, then real data, then design and speed, then analytics and interviews.
In one sentence: read the next lesson on reading queries, then work through the core clauses in order.
A set of tables stored on a computer, like a wall of filing cabinets where each cabinet holds one kind of paper.
One tab of the spreadsheet picture: a header line and many lines of data about one kind of thing.
One line of a table, describing one thing, such as one employee.
One vertical strip of a table, holding one kind of fact about every row, such as name or salary.
A question you write for a database, which usually brings back some rows without changing anything.
Short for Structured Query Language, the language used to write queries for most databases.
One piece of a query that starts with a keyword and does one job, such as WHERE or ORDER BY.
One box in a table, where a row and a column meet.
#Key Takeaways
- A database is a set of tables, a table is rows and columns, and a query is a question about the rows
- A first query has five clauses you can act out in order: take the table, keep matching rows, choose columns, sort, and cut to the first few
- Text goes in single quotes and numbers do not
- A hand trace gives you a prediction, and the playground tells you whether you were right
- A query with no error can still answer the wrong question, so check that the rows are the rows you wanted
#Quick Check
In a table of employees, what is one row?