SQL vs NoSQL

Topic 6 of 10 in Week 3: Databases11 min readIntermediateFree course

Overview

“SQL or NoSQL?” is one of the most common interview questions, and one of the most misunderstood. It isn’t a contest between two camps; it’s a choice among several kinds of databases, each designed for particular data and access patterns. This lesson gives you a map and a way to choose.

The families

Six database families. Relational (PostgreSQL, MySQL): tables, joins and SQL with ACID transactions, good for orders, payments and users. Key-value (Redis, DynamoDB): get and put by key, extremely fast, good for sessions, caches and carts. Document (MongoDB, Firestore): JSON documents with a flexible schema, good for catalogues and profiles. Wide-column (Cassandra, Bigtable): partition and sort keys with huge write volume, good for messages and time series. Graph (Neo4j, Neptune): nodes and edges with multi-hop queries, good for social graphs and fraud. Search and analytics (Elasticsearch, ClickHouse): full-text search and aggregates, not the source of truth, good for search and dashboards. Most real systems use several.

Relational (SQL)

Data in tables with a fixed schema, related through keys, queried with SQL.

  • Strengths: joins, ad-hoc queries, strong constraints, ACID transactions, decades of tooling.
  • Scaling: a single node goes a long way (tens of thousands of queries per second on good hardware); then read replicas; then sharding, which you or an extension (Vitess, Citus) must manage.
  • Use for: most business data, anything involving money, inventory or complex relationships.

Key-value

A giant dictionary: get(key), put(key, value).

  • Strengths: extremely fast and simple; scales out easily because each key is independent.
  • Limits: no queries by value, no joins.
  • Use for: caches, sessions, feature flags, counters, shopping carts. Examples: Redis (in memory), DynamoDB (durable, managed; it also supports sort keys and indexes).

Document

Records are JSON-like documents that can nest data and vary in shape.

  • Strengths: flexible schema, natural mapping to application objects, reading a whole aggregate in one go.
  • Limits: joins are weak or absent; data duplicated across documents must be kept in sync.
  • Use for: product catalogues with varied attributes, content, user profiles. Examples: MongoDB, Firestore, Couchbase.

Wide-column

Rows grouped into partitions by a partition key and sorted by a clustering key (the access-patterns lesson).

  • Strengths: massive write throughput, linear scaling across many nodes, multi-region replication.
  • Limits: you must design tables per query; limited transactions.
  • Use for: messages, activity logs, time series, IoT. Examples: Cassandra, ScyllaDB, Bigtable, HBase.

Graph

Nodes and edges with properties, queried by traversing relationships.

  • Strengths: queries like “friends of friends who like X” or fraud rings that would need many joins elsewhere.
  • Use for: social graphs, recommendations, fraud detection, knowledge graphs. Examples: Neo4j, Amazon Neptune.

Search and analytics

Not usually the source of truth, but common in designs:

  • Search engines (Elasticsearch, OpenSearch) build inverted indexes for full-text search and filtering.
  • Columnar analytics stores (ClickHouse, BigQuery, Snowflake, Redshift) scan and aggregate billions of rows quickly.

They’re fed from the primary database through change streams or batch jobs.

How to choose

Ask these questions in order:

  1. What does the data look like? Highly related records → relational. Self-contained aggregates → document. Simple lookups by ID → key-value. Huge append streams → wide-column. Relationships themselves are the point → graph.
  2. How is it accessed? Ad-hoc and evolving queries favour SQL. A few known, high-volume patterns suit partitioned NoSQL stores.
  3. How strong must consistency be? Money and inventory want transactions. Feeds and analytics tolerate eventual consistency.
  4. How big and how fast? Millions of writes per second across regions push towards wide-column stores; most apps are well within what a relational database with replicas handles.
  5. What does the team know and operate well? A well-run PostgreSQL often beats a poorly run exotic store.

The lines are blurring

  • PostgreSQL has excellent JSON support, full-text search and extensions for time series and vectors.
  • Distributed SQL databases (Google Spanner, CockroachDB, YugabyteDB, TiDB) shard and replicate automatically while keeping SQL and transactions.
  • Many NoSQL stores have added transactions and secondary indexes.

So say why you chose a store, in terms of the questions above, not just its category.

In an interview

A safe pattern: “The source of truth for users, orders and payments is PostgreSQL, because we need transactions. Hot reads go through Redis. Messages go to Cassandra, partitioned by conversation, because of the write volume. Search uses Elasticsearch, fed by change events.” Each choice is tied to a requirement.

Recap

Relational databases give you schemas, joins and transactions, and scale further than many people assume. Key-value, document, wide-column and graph stores each relax something to gain scale, flexibility or a specialised query model, while search and analytics stores serve specific read patterns. Choose by data shape, access pattern, consistency and scale, and expect a real system to use several.

Exercise

Choose the stores

Pick a type of database (and an example product) for each part of a ride-sharing app, with one reason: (a) riders, drivers and payments; (b) the current location of every active driver, updated every 4 seconds; (c) the full history of trips and GPS points for analytics; (d) user sessions and rate-limit counters; (e) searching places by name ('airport', 'Phoenix Mall').

Text
(a)
(b)
(c)
(d)
(e)

Summary

  • Relational databases offer schemas, joins, ACID transactions and flexible queries; they scale up well and scale out with replicas and sharding.
  • NoSQL families trade some of that for scale or flexibility: key-value, document, wide-column and graph stores.
  • Choose by data shape, access patterns, consistency needs and scale — not by popularity.
  • Modern products blur the lines: PostgreSQL stores JSON, and distributed SQL databases shard automatically.
  • Most systems combine several stores, each fed from one source of truth.