Step 1 Understand it
Why you need it. A spreadsheet works for a few hundred and one person. An app with thousands of users needs data that many people can read and change at the same time, safely, without losing anything, and fast enough to answer in milliseconds. A does that job, and is how you talk to it.
New words in this topic (tap one to see what it means)
Watch it happen
Read the animation as text
- A table is like one sheet of a spreadsheet. Each row is one thing — one employee. Each column is one fact about it — here, the salary column.
- You never open the files. You write a query in SQL and send it. The database first checks that employees, name, salary and dept_id all exist.
- It checks every row: is dept_id = 1? Four rows pass. Gus's dept_id is NULL (unknown), so he does not.
- It sends back a new, temporary table: only the columns asked for, sorted by name.
- The stored table has not changed at all. A SELECT only reads, so it is always safe to run.
Picture it
flowchart LR
Y["You or your app"] -->|"1 a SQL query"| P["Database (DBMS)<br/>checks and plans it"]
P -->|"2 reads"| T[("Tables<br/>rows and columns")]
T --> P
P -->|"3 the result: a new table"| YThe idea
A database is an organised store of data, run by a program called a (database management system), such as , , or . The data lives in tables. A table is like one sheet in a spreadsheet: each row is one thing (one employee, one order) and each is one fact about it (name, salary). You never open the files yourself. You send the database a query — a request written in SQL — and it sends back rows.
The shape of it
SELECT column, other_column
FROM table_name
WHERE condition;Words in lower case are the parts you fill in.
How it works, step by step
- You write a query in SQL, for example: give me the name and salary of everyone in department 1.
- You send it to the database, from a SQL editor, a Python script, or your app's backend.
- The database checks the query makes sense: do these tables and columns exist?
- Its picks the fastest way to find the rows, for example by using an .
- It runs that plan and returns the result: a new, temporary table of rows and columns.
- Reading never changes the stored tables. A is always safe to run.
Analogy
A library with a librarian. The shelves are the tables, each book is a row, and the details on its catalogue card (title, author, year) are the columns. You don't walk the shelves yourself: you hand the librarian a request slip — "every book by this author, newest first" — and they bring back exactly those books.
Where the analogy breaks
A librarian fetches books one by one, in the order you asked. SQL is : you describe what you want, not how to get it, and the database picks its own way. That is why the order you write a query in is not the order it runs in.
Worked example
Your first query
SELECT name, salary
FROM employees
WHERE dept_id = 1
ORDER BY name;4 rows
| name | salary |
|---|---|
| Asha | 95000 |
| Ben | 70000 |
| Chen | 70000 |
| Hana | 80000 |
Read it like English: select the name and salary, from the employees table, where the department is 1, sorted by name. The answer is a small table of four rows.
Must-know points
- A table holds one kind of thing. A row is one of them; a column is one fact about each.
- SQL is declarative: you say what you want, and the database decides how to get it.
- The result of a query is itself a table. That is why a query can be put inside another query.
- SQL keywords are not case-sensitive (
selectworks), but writing them in capitals makes a query easier to read. - Every major database speaks SQL. The core is the same everywhere; small details such as dates and differ, and these notes point them out.
- How databases scale — SQL versus NoSQL, replication, sharding — is in the System Design tab, Phase 3.
Common mistakes
- Thinking SQL runs top to bottom like Python. It doesn't: the database reads first and SELECT near the end.
- Thinking a database is just a big spreadsheet. It also enforces rules, handles many users at once and survives crashes.
- Forgetting the semicolon between two statements. One statement on its own usually runs without it.
In other databases
- This page runs SQLite, a small database that lives inside one file — here, inside your browser. PostgreSQL, MySQL and SQL Server run as servers, but the SQL you learn here is the same for almost everything you will write.
Step 2 Go further (free, optional)
Optional. The notes above are written to be enough on their own. These free resources give a second explanation and more practice.
- Watch
-
Read
a free interactive book
-
Practice
SQLBolt — Lesson 1: SELECT queries 101
interactive exercises in the browser, free
Step 3 Practise
Try each one yourself first: out loud, or written and run in the practice box below, which saves every query you run. Then open the answer and compare.
-
ExplainSay what it does and when to use it, in plain words, with no notes.
Explain to a friend who uses Excel what a table, a row, a column and a query are, using the employees table.
Show answer
A table is one sheet in a spreadsheet. The employees table is a sheet with one line per person.
A row is one line: one employee, such as Asha with her id, department, manager, salary and hire date.
A column is one heading, one fact that every row has: name, salary, hire_date.
A query is a question you write down in SQL, such as "give me the name and salary of everyone in department 1". You don't scroll and filter by hand like in Excel: you send the question, and the database sends back just the rows and columns you asked for.
-
Writefrom a blank page, then run it
Write a query that returns the name and hire_date of every employee in department 2, sorted by name.
Show answer
Pick the two columns, name the table, keep department 2, and sort. Dara and Eli are the two people in Sales.
sqlSELECT name, hire_date FROM employees WHERE dept_id = 2 ORDER BY name;2 rows
name hire_date Dara 2020-02-20 Eli 2022-07-01 -
Predictsay it before you run it
Before running it, say how many rows
SELECT name FROM employees WHERE salary > 60000;returns, and which names.Show answer
4 rows: Asha (95,000), Ben (70,000), Chen (70,000) and Hana (80,000).
Dara earns exactly 60,000, and
>means strictly more than, so she is left out. Use>=to include her.The query has no , so the database may return the 4 names in any order. The query below adds ORDER BY name and the salary, so you can check.
sqlSELECT name, salary FROM employees WHERE salary > 60000 ORDER BY name;4 rows
name salary Asha 95000 Ben 70000 Chen 70000 Hana 80000 -
Edge casesSay what happens with NULLs, ties, duplicates or no rows at all, and fix it.
Gus has no department. Will he appear in
SELECT name FROM employees WHERE dept_id = 1;? Will he appear if there is no at all?Show answer
With
WHERE dept_id = 1: no. Gus's dept_id is (unknown), andNULL = 1is not true, it is unknown. WHERE keeps only rows where the condition is true, so Gus is dropped.With no WHERE at all: yes. Nothing filters the rows, so all 8 employees come back, Gus included.
The query below shows the test for every row. Gus gets NULL, not 0 (false): that is why he is in neither "department 1" nor "not department 1".
sqlSELECT name, dept_id, dept_id = 1 AS in_dept_1 FROM employees ORDER BY emp_id;8 rows
name dept_id in_dept_1 Asha 1 1 Ben 1 1 Chen 1 1 Dara 2 0 Eli 2 0 Fatima 3 0 Gus NULL NULL Hana 1 1
Practice box: write and run your answers here
Your queries in this box
Step 4 Interview-ready, then prove it
Your interview answer
About 20 to 40 seconds, said out loud.
A stores data in tables, where each row is one record and each column is one attribute, and the tables are linked through keys. SQL is the declarative language for querying and changing that data: I describe the result I want, and the database's optimiser decides how to get it. That separation is why the same query can become much faster just by adding an index.
Follow-up questions
What is the difference between a database and a DBMS?
The database is the data itself. The DBMS is the software that stores it and answers queries: PostgreSQL, MySQL, SQL Server, SQLite.
What does "declarative" mean?
You describe the result you want, not the steps to get it. The database chooses the steps, which is called its query plan.
When would you not use a relational database?
When the data has no fixed shape, or one simple lookup by key must scale across many machines — a key-value or document store can fit better. For most products, a relational database is the right default.
Tick each check when you can do it without notes
A topic is finished when you pass all four checks, not when you have read it. Be strict: if you had to look at the notes, it is not ticked yet.
- Explain Say what it does and when to use it, in plain words, with no notes.
- Write Write the query from a blank page, then run it to check.
- Predict Read a query and say which rows it returns before you run it.
- Edge cases Say what happens with NULLs, ties, duplicates or no rows at all, and fix it.
0 of 4 checks ticked.
Saved in this browser. Back it up or move it to another device.