SQL vs NoSQL: Choosing How to Model Your Data
Relational vs document, key-value, column-family, and graph databases — and the real criteria for picking one over another.
SQL vs NoSQL: Choosing How to Model Your Data
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A SQL database stores rows in tables with a fixed schema and relationships enforced by foreign keys; a NoSQL database trades some of that structure for flexibility or scale.
- "NoSQL" is not one thing — document stores, key-value stores, column-family stores, and graph databases each model data differently and suit different access patterns.
- The question "SQL or NoSQL" is really "how will this data be queried, and how often will its shape change" — the answer follows from the access pattern, not from which technology is newer.
- A rigid schema is a feature, not a limitation: it catches a malformed write before it reaches disk, which a schemaless store cannot do on its own.
- Picking the wrong model early is expensive to undo once an application has real data and real traffic, which is why this decision deserves more thought than "NoSQL scales better".
Two ways to shape the same data
Say you’re storing user profiles: a name, an email, a list of past orders. In a relational (SQL) database, that’s at least two tables — users and orders — linked by a foreign key, with a fixed set of columns and types enforced before a row is ever written. In a document database like MongoDB, it might be one JSON-like document per user, with the orders embedded directly inside it:
{
"_id": "u_8f2a",
"name": "Priya Shah",
"email": "priya@example.com",
"orders": [
{ "id": "o_1", "total": 42.50 },
{ "id": "o_2", "total": 17.00 }
]
}
Both represent the same information. The relational version normalizes it apart so an order always points at exactly one valid user and nothing is duplicated; the document version denormalizes it together so reading a user and their orders is one lookup instead of a join. Neither is “right” — they’re optimized for different things.
The four common NoSQL shapes
“NoSQL” covers several distinct models, and conflating them is where a lot of confused advice comes from:
- Document stores (MongoDB, Couchbase) keep semi-structured documents, usually JSON. Good fit when each record’s shape varies a little from the next, or when you want to read a whole related object in one request.
- Key-value stores (Redis, DynamoDB in its simplest mode) map a key straight to a blob of value. Nothing simpler or faster for lookups by a known key — a session token, a cache entry — but you generally can’t query by anything except that key.
- Column-family stores (Cassandra, HBase) organize data by column rather than by row, which suits workloads that write huge volumes and read by a narrow set of columns across many rows, like time-series metrics.
- Graph databases (Neo4j) store nodes and the relationships between them as first-class citizens, which makes “friends of friends” or “shortest path” style queries fast in a way a relational join chain usually isn’t.
A SQL database is a fifth, older shape: rows in tables, a schema the engine enforces, and a query language (SQL) built around joining tables together.
What actually decides the choice
Three questions do most of the work:
How fixed is the shape of your data? If every record genuinely has the same fields and that’s unlikely to change, a schema is free insurance, not overhead — it rejects bad writes before they become bad reads. If different records naturally have different fields (a product catalog where a “book” and a “laptop” have almost nothing in common), forcing one schema onto both means a lot of nullable columns or awkward sub-tables.
How will you query it? If your access pattern is “give me this entity and everything related to it” in one shot, a document store that stores those together avoids a join. If your access pattern is “find all orders over $100 placed last month across all users,” a relational engine’s query planner and indexes are built exactly for that kind of cross-cutting question.
What consistency do you actually need? A relational database’s transactions guarantee that a multi-step write either fully happens or fully doesn’t — essential for, say, moving money between two accounts. Many NoSQL stores relax that guarantee in exchange for availability or throughput at scale (the lesson on ACID and CAP goes into exactly this tradeoff).
A decision that’s cheap to get right early, expensive to fix later
None of this is about which technology is “modern.” Relational databases handle enormous scale in production every day, and plenty of NoSQL deployments enforce rigorous validation in the application layer. The expensive mistake is picking a model without asking what the data actually looks like and how it’s actually read — because migrating a live system’s data model, once there’s real data and real traffic depending on it, is one of the more painful projects in software engineering.
Check yourself
Why is "NoSQL" a misleading umbrella term?
It groups together document, key-value, column-family, and graph databases, which store and query data in genuinely different ways — a decision that's right for a document store may be wrong for a graph database.
A team's data has a fixed, well-understood shape and needs multi-row transactions across several related tables. Which model fits better, and why?
A SQL database fits better, because its schema enforcement and transactional guarantees are built for exactly that case — related, structured data that must stay consistent across multiple tables.
What's the tradeoff a document database makes to get schema flexibility?
It gives up the guarantee that every document in a collection has the same shape, which pushes validation work from the database into the application and makes some cross-document queries harder to express.
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.