Lesson 6 � Beginner

SQL vs NoSQL

Database choose karna system design ka important decision hai. SQL ya NoSQL � dono ke apne pros aur cons hain. Use case ke hisaab se choose karo.

? 20 min✓ Beginner✓ Database basics

SQL vs NoSQL kya hai?

SQL

Structured Query Language � relational databases. Fixed schema, tables, rows, columns. ACID transactions support karte hain. MySQL, PostgreSQL, Oracle, SQL Server.

NoSQL

Not Only SQL � non-relational databases. Flexible schema, different data models. Horizontal scaling easy hai. MongoDB, Redis, Cassandra, DynamoDB.

Kab kya use karo?

Structured data + complex queries + ACID ✓ SQL. Unstructured data + high scale + flexibility ✓ NoSQL. Most production systems dono use karte hain (polyglot persistence).

Detailed Comparison

# SQL vs NoSQL

| Feature | SQL | NoSQL |
|------------------|--------------------------|---------------------------|
| Schema | Fixed schema | Dynamic/flexible schema |
| Data Model | Tables (rows, columns) | Documents, Key-Value, |
| | | Column-family, Graph |
| Scaling | Vertical (scale up) | Horizontal (scale out) |
| ACID | Full support | Varies (eventual consistency) |
| Joins | Complex joins easy | Joins expensive/nahi hote |
| Query Language | SQL (standardized) | Database-specific APIs |
| Use Case | Transactions, reporting | Real-time, big data |
| Examples | MySQL, PostgreSQL | MongoDB, Redis, Cassandra |

# Key Decision Factors:
1. Data structure fixed hai ✓ SQL
2. Data structure change hota rahega ✓ NoSQL
3. ACID transactions chahiye ✓ SQL
4. Massive scale chahiye ✓ NoSQL
5. Complex queries/JOINs ✓ SQL
6. Simple key-value access ✓ NoSQL

SQL Databases Detail

# SQL Database: MySQL / PostgreSQL

# Example Schema:
CREATE TABLE users (
 id INT PRIMARY KEY,
 name VARCHAR(100),
 email VARCHAR(100) UNIQUE
);

CREATE TABLE orders (
 id INT PRIMARY KEY,
 user_id INT,
 amount DECIMAL,
 FOREIGN KEY (user_id) REFERENCES users(id)
);

# Query: User ke saare orders
SELECT u.name, o.amount 
FROM users u 
JOIN orders o ON u.id = o.user_id
WHERE u.id = 1;

# Pros:
✓ ACID compliant (Atomicity, Consistency, Isolation, Durability)
✓ Complex queries with JOINs
✓ Data integrity enforced
✓ Standard SQL � portable across databases

# Cons:
✓ Vertical scaling limit
✓ Schema changes mushkil (ALTER TABLE)
✓ Not great for unstructured data

NoSQL Databases Detail

# 1. Document Database (MongoDB)
✓ JSON-like documents store karte hain
✓ Flexible schema � har document alag ho sakta hai

{
 "_id": 123,
 "name": "Vaibhav",
 "email": "vaibhav@example.com",
 "orders": [
 {"item": "laptop", "amount": 50000},
 {"item": "phone", "amount": 25000}
 ]
}

# 2. Key-Value (Redis, DynamoDB)
✓ Sabse simple � key se value lo
✓ Blazing fast � O(1) access

SET user:123 '{"name": "Vaibhav"}'
GET user:123

# 3. Column-Family (Cassandra, HBase)
✓ Large-scale data, write-heavy
✓ Horizontal scaling bahut achi

# 4. Graph Database (Neo4j)
✓ Relationships important hain
✓ Social networks, recommendations
Mental Model: SQL jaiseExcel spreadsheet � fixed columns, structured data. NoSQL jaise Google Docs � flexible, kuch bhi likh sakte ho. Spreadsheet mein formulas (queries) powerful hain, Docs mein freedom zyada hai.

Real-world Examples

# Netflix:
✓ User data: Cassandra (NoSQL) � massive scale
✓ Billing: PostgreSQL (SQL) � ACID transactions
✓ Recommendations: Neo4j (Graph) � user connections
✓ Caching: Redis (Key-Value) � fast access

# Uber:
✓ Ride data: MySQL (SQL) � transactions
✓ Real-time tracking: Redis (Key-Value) � low latency
✓ Analytics: Cassandra (NoSQL) � write-heavy

# Instagram:
✓ User data: PostgreSQL (SQL) � relationships
✓ Feed: Redis (Key-Value) � caching
✓ Media metadata: Cassandra (NoSQL) � scale

# Lesson: Most systems use POLYGLOT PERSISTENCE
# Different data ✓ Different database

Exercise

Question: SQL databases ka sabse bada advantage kya hai jo transactions mein data consistency guarantee karta hai? (Full form ya acronym)

Question: NoSQL databases primarily kaise scale hote hain � vertical ya horizontal? (1-2 words)

Common mistakes

Lesson complete?

SQL vs NoSQL samajh aa gayi✓ Ab Message Queues seekhte hain � async communication kaise hoti hai systems ke beech.