SQL vs NoSQL Databases
"Which database would you pick, and why?" is a near-guaranteed follow-up in any design round. A crisp SQL-vs-NoSQL answer that ties choice to requirements โ not fashion โ signals real engineering judgment.
The core idea
SQL (relational) databases store data in tables of rows and columns with a fixed schema and relationships enforced by keys. They speak SQL and offer strong ACID transactions. NoSQL (non-relational) databases relax the rigid schema to gain flexibility and horizontal scale; they come in families โ document, key-value, wide-column, and graph. The right choice follows your data shape, consistency needs, and scale.
Beginner โ the two worlds
SQL examples: PostgreSQL, MySQL, Oracle. Data is normalized across related tables; you JOIN to combine them. NoSQL examples: MongoDB (document), Redis/DynamoDB (key-value), Cassandra (wide-column), Neo4j (graph). Data is often denormalized and stored together for fast reads.
Intermediate โ ACID vs BASE
SQL databases prize ACID (Atomicity, Consistency, Isolation, Durability) โ transactions are all-or-nothing and leave the DB consistent. Many NoSQL systems favor BASE (Basically Available, Soft state, Eventual consistency) โ they stay available and scalable, accepting that replicas may briefly disagree before converging. This is the fundamental trade: strong consistency vs availability at scale.
Advanced โ scaling and schema
SQL scales primarily vertically and via read replicas; sharding it is possible but complex because of cross-shard JOINs and transactions. Many NoSQL stores are built to scale horizontally from day one, partitioning data across nodes. SQL's fixed schema enforces integrity but makes changes heavier; NoSQL's flexible schema lets each record differ, great for evolving or varied data but pushing validation into the application. NoSQL is not "schema-less" so much as "schema-on-read."
Worked example
Designing an e-commerce app: orders, payments and inventory need multi-row transactions and strict integrity, so a relational database (PostgreSQL) fits. The product catalog with varied, nested attributes and huge read volume fits a document store (MongoDB), and the shopping-cart and session data fit a key-value store (Redis). Real systems mix databases โ polyglot persistence โ picking the right tool per workload.
When to use
Choose SQL when you need strong transactions, complex queries/JOINs, and well-structured, relational data (finance, orders, bookings). Choose NoSQL when you need massive horizontal scale, flexible/evolving schemas, or a specific access pattern (key lookups, documents, graphs, time series).
Tricks and mnemonic
Anchor it as "SQL = Structure + Strong consistency; NoSQL = Not-only-SQL, Scale + flexibility." If the interviewer stresses transactions and integrity, lean SQL; if they stress scale and flexible data, lean NoSQL.
Don't claim "NoSQL scales, SQL doesn't." SQL databases scale well vertically and with replicas and handle enormous workloads; the real difference is horizontal scaling and schema flexibility, not raw capability.
- โ- SQL = tables, fixed schema, JOINs, ACID transactions (PostgreSQL, MySQL).
- โ- NoSQL families: document, key-value, wide-column, graph.
- โ- ACID (strong consistency) vs BASE (eventual consistency, high availability).
- โ- SQL scales vertically/replicas; many NoSQL stores scale horizontally natively.
- โ- Choose by requirements; real systems mix both (polyglot persistence).
- โSQL databases give structure and strong ACID transactions; NoSQL databases give flexible schemas and native horizontal scale, usually with eventual consistency. Pick based on data shape, consistency, and scale โ and don't be afraid to use both.
SQL vs NoSQL Databases โ Revision Notes
Fast revision of choosing a database.
- SQL (relational): tables, fixed schema, JOINs, ACID transactions (PostgreSQL, MySQL, Oracle).
- NoSQL families: document (MongoDB), key-value (Redis/DynamoDB), wide-column (Cassandra), graph (Neo4j).
- ACID = strong consistency, all-or-nothing; BASE = basically available, eventually consistent.
- SQL scales vertically + read replicas (sharding is hard); many NoSQL scale horizontally natively.
- SQL enforces schema integrity; NoSQL is flexible ("schema-on-read"), pushing validation to the app.
- Choose SQL for transactions/integrity; NoSQL for scale/flexibility/specific access patterns.
- Real systems mix both = polyglot persistence.
SQL vs NoSQL Databases โ Flashcards
Cover the answer, recall, then check. 8 cards.
Q1. What does ACID stand for?
A1. Atomicity, Consistency, Isolation, Durability โ the guarantees of SQL transactions.
Q2. Name the four main NoSQL families.
A2. Document, key-value, wide-column, and graph.
Q3. What does BASE emphasize over ACID?
A3. Basically Available, Soft state, Eventual consistency โ availability and scale over strict consistency.
Q4. How does SQL typically scale vs NoSQL?
A4. SQL scales vertically + read replicas (sharding is complex); many NoSQL scale horizontally natively.
Q5. Give a SQL and a NoSQL example database.
A5. SQL: PostgreSQL/MySQL. NoSQL: MongoDB (document) or Cassandra (wide-column).
Q6. When should you prefer SQL?
A6. When you need strong transactions, complex JOINs, and structured, relational data.
Q7. What is polyglot persistence?
A7. Using multiple database types in one system, each for the workload it fits best.
Q8. Is NoSQL truly "schema-less"?
A8. No โ it uses flexible "schema-on-read"; validation moves into the application.
SQL vs NoSQL Databases โ Worked Example
Worked Example
Problem: Choose a database type for (a) a bank's transaction ledger and (b) a social app's activity feed taking ~1,000,000 writes/second. Justify each choice.
Solution: Match the data model and consistency needs to the workload.
(a) Banking ledger โ SQL (relational). Money movement demands ACID transactions and strong consistency: a debit and its matching credit must commit atomically. Structured, related tables (accounts, transactions) benefit from joins and constraints. Correctness outweighs raw write throughput here.
(b) High-volume activity feed โ NoSQL (e.g. Cassandra). A firehose of 1M writes/s needs horizontal write scalability and a flexible schema. Feeds tolerate eventual consistency (a like appearing a second late is fine), and wide-column/key-value stores partition writes across many nodes without cross-row transactions.
Answer: SQL for the ledger (ACID, strong consistency); NoSQL for the feed (scale-out writes, eventual consistency).
- โ- SQL: structured schema, ACID transactions, strong consistency, powerful joins.
- โ- NoSQL: flexible schema, horizontal scale, high write throughput, often eventual consistency.
- โ- Pick by the workload โ transactional integrity vs massive scale-out.