mediumSystem Design

When would you choose SQL vs NoSQL?

1.1k views
01

Understand the problem

Compare relational and NoSQL databases.

databasesql-nosql
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Stuck? AI Nudge Available

Get a conceptual hint to guide your logic without spoiling the final implementation.

03

Study the solution

The solution is waiting

Give it an honest attempt first — then compare your thinking with the full walkthrough.

04

Read the code

Relational schema (SQL)
-- Normalized: users and orders in separate tables, joined by user_id
CREATE TABLE users (
  id    BIGINT PRIMARY KEY,
  email TEXT UNIQUE NOT NULL,
  name  TEXT NOT NULL
);

CREATE TABLE orders (
  id          BIGINT PRIMARY KEY,
  user_id     BIGINT NOT NULL REFERENCES users(id),
  total_cents INT    NOT NULL,
  created_at  TIMESTAMPTZ DEFAULT now()
);

-- Read needs a join:
SELECT u.name, o.total_cents
FROM orders o JOIN users u ON u.id = o.user_id
WHERE u.email = 'ada@example.com';
Document model (NoSQL)
// Same data denormalized into ONE document — no joins, one read.
{
  "_id": "user_42",
  "email": "ada@example.com",
  "name": "Ada",
  "orders": [
    { "id": "o_1", "totalCents": 4999, "createdAt": "2024-01-04T10:00:00Z" },
    { "id": "o_2", "totalCents": 1200, "createdAt": "2024-02-11T08:30:00Z" }
  ]
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.