SQL vs NoSQL
Relational, document, key-value, wide-column and graph databases — match the store to the access pattern.
Relational (SQL) databases store data in tables with a fixed schema, joins and ACID transactions. They are the right default for most business systems.
NoSQL databases trade some of that structure for flexible schemas, horizontal scale or a data model that fits a specific access pattern. The question is not “which is better” but “how will this data be read and written?”.
Diagram
Key concepts
- Schema-on-write
- SQL validates structure when data is written. Errors are caught early.
- Schema-on-read
- Document stores accept varying shapes; the application interprets them.
- Aggregate
- A cluster of data read and written together, such as an order and its lines — a natural document.
- Polyglot persistence
- Using different stores for different needs in one system, e.g. SQL for orders, Redis for sessions, a search engine for products.
- NewSQL
- Distributed databases with SQL and ACID that scale horizontally, such as CockroachDB or Spanner.
Trade-offs
Benefits
- SQL: integrity, rich queries, mature tooling
- Document: flexible schema, natural fit for aggregates
- Key-value and wide-column: massive scale and predictable latency
Costs
- SQL: harder to shard, schema changes need migrations
- NoSQL: weaker ad-hoc querying and joins
- NoSQL: consistency and transactions vary by product
Use it when
- SQL for transactional business data with relationships
- Document stores for content and per-entity aggregates
- Key-value for sessions, caching, feature flags
- Wide-column for time-series, IoT and event logs
- Graph for social networks, recommendations, fraud rings
Common mistakes
- Choosing NoSQL “for scale” when the dataset fits easily on one SQL server
- Modelling relational data in a document store and re-implementing joins in code
- Ignoring how you will query the data before picking the store
Interview questions
Q1An e-commerce system needs orders, a product search and shopping carts. Which stores would you use?
Q2What does denormalisation mean in NoSQL?
Q3Can relational databases scale horizontally?
Related lessons
CAP Theorem & Consistency
During a network partition, a distributed store must choose between consistency and availability.
Data & StorageIndexing & Query Performance
How indexes turn full table scans into fast lookups — and the other habits that keep queries quick.
Data & StorageSharding Strategies
Range, hash, consistent hashing and directory-based sharding — how data is split across servers.