AlgoViz

SQL databases

easy 25 min

A 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

idnameemail

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

timeperson 1person 2
1seat 12 free?seat 12 free?
2yesyes

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

    critical

    Two requests can pass the check before either writes.

    Wrong
    SELECT 1 FROM users WHERE email = $1;-- nothing found, so insert
    Right
    ALTER TABLE users ADD UNIQUE (email);-- insert, and handle the violation
  • Calling another service inside a transaction

    major

    Locks are held while a slow network answers, so others queue behind you.

    Wrong
    BEGIN;UPDATE stock SET n = n - 1 WHERE id = 7;-- call the payment company, 3 secondsCOMMIT;
    Right
    BEGIN; -- reserve stockUPDATE stock SET n = n - 1 WHERE id = 7 AND n > 0;COMMIT;-- call payment, then confirm or release
  • A function on the indexed column

    major

    The plain index is sorted by email, not by the result of a function.

    Wrong
    SELECT * FROM usersWHERE lower(email) = 'bob@ex.com';
    Right
    CREATE INDEX ON users (lower(email));-- or store emails in one form
  • One query per row

    minor

    A page of 50 orders makes 51 round trips when each order loads its customer.

    Wrong
    SELECT * FROM orders LIMIT 50;-- then 50 times:SELECT * FROM users WHERE id = $1;
    Right
    SELECT 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.