SQL vs NoSQL

One small shop, kept three ways: tables, documents and keys. Run the same five jobs on each and see who joins, who copies, and who can't search at all. None of them wins every job.

Level: BasicWatch: 10 steps · ~1 minuteFive jobs, three storesRecall: 3 questions · 8 cardsCompare 3 storesSQL · mongosh · redis-cli

What you'll take away

  • Pick SQL, documents or key-value by the data's shape and the questions you'll ask
  • Know why key-value can't search inside a value, so you keep list keys yourself
  • Weigh what copies cost: a rename touches every order document, one row in SQL
  • Explain why a schemaless store really moves the schema into your app's code

The 1-minute version

Tap a point and the picture below plays that step.

4Compare them all
SQL (tables)Show order 1001
SQL · 3 tables
Document · whole orders
Key-value · key → a closed box
the app
SELECT … JOIN × 2 → …
Step 1 / 10 · step by step
4Records touched
2JOIN
–Places to update
✓Verdict

The picture: one small shop kept three ways, with the app's building on the left. At the back, three rows of small houses are the SQL tables customers, orders and order_lines, each row's key on its roof. In front, bigger teal houses are order documents, the whole order written on the roof. At the front, violet lockers are key-value keys; c1:orders is the list you keep yourself. The light is the app's request; what it touches lights up inside a ring: blue is straightforward, amber comes with a catch, red needs work of your own. Dashed lines are a JOIN; an amber room is the new gift field. A model on tiny data: no speeds are measured.

Step 4 · Every option at once, on one clock

Compare them all: SQL vs NoSQL

The same shop in every tile and five jobs in a row: show order 1001, find Mai's orders, add a gift field, Mai changes her name, total the sales. See how many records each touches, and which jobs come with a catch or extra work.

Show order 1001
SQL (tables)PostgreSQL, MySQL, SQLite✓ 0 · ! 0 · ✗ 0 · touched 0

Use it when Data with many relationships, asked from many angles, that needs transactions and rules the database keeps: money, orders, stock

Skip it when Data whose shape changes every day and differs row to row, or huge volumes read only by key

A model on tiny data (2 customers, 3 orders), no speeds measured. ✓ straightforward · ! a catch · ✗ extra work of your own. Press any tile to see it large above, with when it fits.

Which one?

SQLDocumentKey-value
ShapeTables, one shape per tableNested documents, shapes may differA key and an opaque value
Read one thingJOINsOne readOne GET
Ask a new questionAny column, any joinFields inside, with indexesOnly by key
Change the shapeA migrationJust write; the app copesJust write; the app copes
Same data in two placesRare: stored onceCommon: keep copies in stepCommon

"No schema" means the schema moved

A document database doesn't make you declare fields, but the code that reads them still expects them. The rules move from the database into your app, and every old document is a case your code has to handle.

Both scale, both have transactions

SQL databases scale out too (read replicas, sharding with tools like Citus or Vitess), and MongoDB has had multi-document transactions since 4.0. The real difference is the shape of the data and the questions you'll ask it. Not sure? Start with SQL; PostgreSQL's jsonb keeps the flexible parts.

In real code

shop.pg.sql
-- PostgreSQL 16: the shop as tables. The customer's name is stored once; an order is put together with JOINs.
CREATE TABLE customers (id text PRIMARY KEY, name text NOT NULL);
CREATE TABLE orders (id int PRIMARY KEY, customer_id text NOT NULL REFERENCES customers, day date NOT NULL);
CREATE TABLE order_lines (order_id int NOT NULL REFERENCES orders, item text NOT NULL, price numeric(8, 2) NOT NULL);
CREATE INDEX ON orders (customer_id);
INSERT INTO customers VALUES ('c1', 'Mai'), ('c2', 'Beam');
INSERT INTO orders VALUES (1001, 'c1', '2026-11-24'), (1002, 'c2', '2026-11-24'), (1003, 'c1', '2026-11-25');
INSERT INTO order_lines VALUES (1001, 'tea', 120), (1001, 'cup', 250), (1002, 'pot', 890), (1003, 'tea', 120);

-- 1. One order: three tables, two JOINs.
SELECT o.id, c.name, string_agg(l.item, ', ' ORDER BY l.item) AS items
FROM orders o JOIN customers c ON c.id = o.customer_id JOIN order_lines l ON l.order_id = o.id
WHERE o.id = 1001 GROUP BY o.id, c.name;

-- 2. Mai's orders: ask by any column.
SELECT string_agg(id::text, ', ' ORDER BY id) FROM orders WHERE customer_id = 'c1';

-- 3. A new field: one migration, every row gets it (instant in PostgreSQL 11+ for a constant default).
ALTER TABLE orders ADD COLUMN gift boolean NOT NULL DEFAULT false;
SELECT 'orders without gift: ' || count(*) FROM orders WHERE gift IS NULL;

-- 4. Mai becomes Maya: one row, and every order shows it.
UPDATE customers SET name = 'Maya' WHERE id = 'c1';
SELECT string_agg(o.id || ' ' || c.name, ', ' ORDER BY o.id) FROM orders o JOIN customers c ON c.id = o.customer_id;

-- 5. Sales per day: one GROUP BY.
SELECT o.day, sum(l.price) FROM orders o JOIN order_lines l ON l.order_id = o.id GROUP BY o.day ORDER BY o.day;
Run: psql -f shop.pg.sql (checked against PostgreSQL 16 in Docker)It printed:
1001|Mai|cup, tea
1001, 1003
orders without gift: 0
1001 Maya, 1002 Beam, 1003 Maya
2026-11-24|1260.00
2026-11-25|120.00

Three tables with foreign keys, then the five jobs: one order with two JOINs, Mai's orders, ALTER TABLE for gift, a one-row rename that every order sees, a GROUP BY. Really run on PostgreSQL 16 and MySQL 8.4 in Docker and on SQLite's CLI; below is what each printed.

Common questions

What's the difference between SQL and NoSQL?

SQL keeps tables, stores each fact once and joins them when you read, so you can ask from any angle. NoSQL is a family: documents nest what's read together, key-value answers by key alone, and there are others. The real difference is the shape of your data and the questions you'll ask it.

Table: which one?

Can a key-value store search inside the values?

No. Redis only knows keys. To list Mai's orders you keep a set of her order keys yourself (SADD) and update it with every new order. A total across many keys means reading every key, or keeping running totals as you write.

What does adding a field take in SQL?

A migration first: ALTER TABLE … ADD COLUMN, and every row has the field at once. Many such changes are instant now; some, like changing a type, still rewrite the whole table, and the deploy has to be ordered so old and new code both work.

Is NoSQL really schemaless?

The database doesn't make you declare fields, but the code that reads them still expects them. The schema moved from the database into your app, and every old document missing a field is a case your code has to handle. Many teams add validation, like MongoDB's $jsonSchema.

Read: the schema moved

The customer's name is copied into every order. What happens when they change it?

You update every document that holds it yourself (updateMany), or you keep the name as it was when ordered, on purpose, as history. In SQL the name is one row in customers: change it once and every order shows the new name.

Doesn't SQL only scale up?

No. SQL scales out too: read replicas, and sharding with tools like Citus or Vitess. Joins and transactions across shards just get harder. Many NoSQL stores were built to shard from day one, in exchange for more limited joins and transactions.

Is it true NoSQL has no transactions?

Some don't, but many do now: MongoDB has had multi-document transactions since 4.0. So transactions alone rarely settle the choice anymore; look at the shape of the data and the questions you'll ask it.

When should I pick SQL, and when documents?

SQL when the data links up many ways, you'll ask questions you don't know yet, and you need transactions and constraints: money, orders, stock. Documents when things are read and written as one piece and each one looks different. Not sure? Start with SQL; PostgreSQL's jsonb keeps the flexible parts.

Table: which one?

Next lesson

ACIDLevel: Intermediate

SQL's tables come with transactions that finish completely or not at all; next, how they save a transfer in a power cut.