SQL databases
easy 25 minA SQL (relational) database keeps data in tables with declared columns, joins those tables by key when you ask a question, and enforces rules such as unique emails and valid references.
See it run
Tables being made, filled, joined, indexed and changed safely.
Operations
Life of a table
Define the schema first, then insert, query, change and remove rows.
Step 1 of 6
Create the table
| id | name | |
|---|---|---|
0 rows
The schema comes first: three named columns and a primary key. There are no rows yet.
Step 1: Create the table
CREATE TABLE users (id INT PRIMARY KEY, name TEXT, email TEXT);INSERT INTO users VALUES (1, 'Alice', 'alice@ex.com');INSERT INTO users VALUES (2, 'Bob', 'bob@ex.com'), (3, 'Carol', 'carol@ex.com');SELECT * FROM users WHERE name = 'Bob';UPDATE users SET email = 'bob@new.com' WHERE id = 2;DELETE FROM users WHERE id = 3;- Cost
- 1 row each
- You get
- Typed, keyed rows
Run the WHERE of an UPDATE or DELETE as a SELECT first. Without WHERE, every row changes.
In the real world
The same sql databases doing real jobs. Pick one and step through it.
Two people press Book for the same seat at the same moment.
Step 1 of 3
Both see it free
| time | person 1 | person 2 |
|---|---|---|
| 1 | seat 12 free? | seat 12 free? |
| 2 | yes | yes |
Each server checks, and each sees seat 12 free. The check alone protects nothing.
Step 1: Both see it free
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Checking uniqueness only in code
criticalTwo requests can pass the check before either writes.
WrongSELECT 1 FROM users WHERE email = $1;-- nothing found, so insertRightALTER TABLE users ADD UNIQUE (email);-- insert, and handle the violationCalling another service inside a transaction
majorLocks are held while a slow network answers, so others queue behind you.
WrongBEGIN;UPDATE stock SET n = n - 1 WHERE id = 7;-- call the payment company, 3 secondsCOMMIT;RightBEGIN; -- reserve stockUPDATE stock SET n = n - 1 WHERE id = 7 AND n > 0;COMMIT;-- call payment, then confirm or releaseA function on the indexed column
majorThe plain index is sorted by email, not by the result of a function.
WrongSELECT * FROM usersWHERE lower(email) = 'bob@ex.com';RightCREATE INDEX ON users (lower(email));-- or store emails in one formOne query per row
minorA page of 50 orders makes 51 round trips when each order loads its customer.
WrongSELECT * FROM orders LIMIT 50;-- then 50 times:SELECT * FROM users WHERE id = $1;RightSELECT o.*, u.name FROM orders oJOIN users u ON u.id = o.user_idORDER BY o.id DESC LIMIT 50;
Interview tips
How to explain it
- Tables with declared columns, linked by keys.
- Describe the answer; the planner finds the way.
- Constraints and transactions keep it correct.
Memory trick: S, Q, L
- Schema first.
- Query with SELECT.
- Links between tables.
Reach for it when
- Wrong data costs more than slow data.
- Questions cross several facts.
- One strong machine can hold it for years.
Watch out for
- Missing indexes and too many indexes.
- Long transactions.
- Replica lag after a write.