SQL vs NoSQL
One small shop order stored two ways, side by side: what a relational database and a NoSQL store each do about schema, joins, transactions, scaling and consistency.
In 30 seconds
A SQL (relational) database keeps data in tables with fixed columns, links them with joins, and can change several rows as one all-or-nothing transaction.
A NoSQL store keeps self-contained records with flexible fields. It gives up some of those guarantees so that the data can be spread easily over many machines.
Neither is better in general. Data with many relationships and strict correctness needs suits SQL; simple lookups at very large scale suit NoSQL.
SQL vs NoSQL, step by step
NoSQL is shown here as a document store. The contrasts are typical defaults, not hard limits: relational databases can be sharded, and several NoSQL stores offer joins or transactions.
- Step 1: One order, two shapes. A relational (SQL) database splits data into tables and links the rows by ids: the order points at its customer. A NoSQL document store keeps everything one order needs in a single nested record. The same facts, arranged for different strengths.
- Step 2: Fixed or flexible schema. A table has a schema: a fixed list of columns that every row must fit, and the database rejects anything else. Adding a gift note means changing the table, after which every row has that column. A document store accepts the new field at once, but then the application must cope with records that lack it.
- Step 3: Joins or copies. Because SQL stores Ada once, a join looks her up whenever an order is read, and one changed row is seen by every order. The documents each carry their own copy of her city: reading an order needs no join and is one fetch, but a change must be written into every copy.
- Step 4: Transactions. A relational transaction wraps several changes so that all happen or none do, even across tables. Moving 30 from A to B cannot lose money halfway. Many NoSQL stores promise this only within one record, so a crash between two writes can leave them out of step. Several now offer multi-record transactions, at some cost in speed.
- Step 5: Scale up or scale out. Joins and transactions are simplest with all the data on one machine, so a relational database usually grows by moving to a larger server and adding read-only copies. Self-contained records divide easily by key across many machines, which is how most NoSQL stores grow. Relational databases can be split too, with more work.
- Step 6: Consistency guarantees. A relational database with one authoritative copy shows every reader the latest committed value. A store with several copies often confirms a write before all have it, so a read may briefly return the old value: eventual consistency. These are defaults: relational replicas can lag too, and many NoSQL stores offer strong reads.
- Step 7: How to choose. Start from the questions the data must answer. Many kinds of record that relate to each other, changes that must be all-or-nothing, and queries nobody has thought of yet favour SQL. Records that are read whole by a key, vary in shape, or outgrow one machine favour NoSQL. Plenty of systems run one of each.
Try it yourself
Learn this first
Learn next
- Database normalization (1NF to 3NF)One repetitive table split into three, one normal form at a time. Beginner
- Consistency models (strong vs eventual)What a read may return after a write when the data lives on three machines. Intermediate
- Database replicationCopies of a database kept in step, and what happens when one falls behind. Intermediate
Spotted a mistake on this page?