SQL databases store data in tables with a fixed schema and relationships. NoSQL databases store data in other shapes – documents, key-value pairs, wide columns or graphs – with flexible schemas. Both are widely used; the right choice depends on your data and access patterns.
What You Will Learn
- The four main NoSQL types
- Key differences from SQL
- ACID vs BASE
- How to choose
- Using both together
Prerequisites
Types of NoSQL Databases
| Type | Stores | Examples | Good for |
|---|---|---|---|
| Document | JSON-like documents | MongoDB, Firestore | Product catalogues, user profiles, content |
| Key-value | Value looked up by a key | Redis, DynamoDB | Caching, sessions, counters |
| Wide-column | Rows with flexible columns in column families | Cassandra, HBase | Huge write-heavy data, IoT, logs |
| Graph | Nodes and relationships | Neo4j | Social networks, recommendations, fraud rings |
SQL vs NoSQL
| SQL | NoSQL | |
|---|---|---|
| Schema | Fixed, defined first | Flexible |
| Data model | Tables and rows | Documents, key-value, columns, graphs |
| Relationships | JOINs and foreign keys | Embedding or application-side |
| Transactions | Strong ACID | Varies; many offer ACID per document or BASE |
| Scaling | Mainly vertical (+ replicas) | Designed for horizontal scaling |
| Query language | Standard SQL | Database-specific APIs |
| Examples | MySQL, PostgreSQL, SQL Server | MongoDB, Redis, Cassandra, Neo4j |
ACID vs BASE
- ACID (Atomicity, Consistency, Isolation, Durability): every transaction is all-or-nothing and immediately consistent – essential for payments and bookings.
- BASE (Basically Available, Soft state, Eventually consistent): prioritises availability and scale; replicas may briefly disagree.
Same Data, Two Models
-- SQL: two tables joined by a key
SELECT o.id, c.name, o.amount
FROM orders o JOIN customers c ON c.id = o.customer_id;
// MongoDB: one document with embedded data
{ "_id": 101, "customer": { "name": "Riya", "city": "Jalandhar" },
"items": [ {"product": "Laptop", "qty": 1, "price": 55000} ],
"amount": 55000 }
Explanation: SQL avoids duplication and joins at query time; the document stores what is read together in one place, so reading an order needs no join.
How to Choose
| Choose SQL when… | Choose NoSQL when… |
|---|---|
| Data is structured and related | Structure varies or changes often |
| You need complex queries and reports | Access is by key or document |
| Strong consistency is critical (finance) | Massive scale or very high write rates |
| Your team knows SQL | You need caching (Redis) or graph queries (Neo4j) |
Use Both
Most real systems are polyglot: PostgreSQL for orders and payments, Redis for caching, MongoDB for a product catalogue and a vector database for AI search. PostgreSQL itself also handles JSON well with JSONB – see PostgreSQL.
Series
SQL series: SQL Basics · Advanced SQL · Views · Indexing · Stored Procedures · Database Design · SQL for Analytics · MySQL · PostgreSQL · MongoDB · NoSQL vs SQL · Interview Questions
Interview Questions
Q: What is ACID?
Atomicity, Consistency, Isolation, Durability – guarantees for reliable transactions.
Q: Name the four NoSQL types.
Document, key-value, wide-column and graph.
Q: Is NoSQL always faster?
No; it is faster for the access patterns it is designed for, while SQL excels at complex queries and joins.
Practice Exercises
- Design an e-commerce order both as SQL tables and as a MongoDB document.
- Pick a database for: a bank ledger, a chat app’s online-status cache, a friend-recommendation feature.
Summary
SQL offers fixed schemas, joins and strong ACID transactions; NoSQL offers flexible models and horizontal scale. Choose by data shape and access pattern – and often use both.
Next: SQL Interview Questions
Machine Learning Tutorials, Courses and Certifications