What is the difference between SQL and NoSQL databases?
EasyConceptNoSQL, Replication & Sharding
Answer
SQL databases (relational databases such as MySQL and PostgreSQL) store data in tables with a fixed schema and are queried with SQL. NoSQL databases (such as MongoDB, Cassandra and Redis) use other data models, such as documents or key-value pairs, usually with a flexible schema.
- Schema: SQL needs a predefined schema; NoSQL lets each record have different fields.
- Scaling: SQL databases traditionally scale vertically (a bigger server), while most NoSQL databases are built to scale horizontally (more servers).
- Consistency: SQL databases provide ACID transactions and joins. Many NoSQL systems favour availability and follow BASE (basically available, soft state, eventually consistent), although MongoDB has supported multi-document ACID transactions since version 4.0.
- Choose SQL for structured, related data such as banking, and NoSQL for huge volumes, changing data shapes or very high write rates, such as activity feeds.
Example: The same user in both models:
SQL: users(id, name) 1 | Asha
phones(user_id, phone) 1 | 98xxx
1 | 99xxx
MongoDB: { "_id": 1, "name": "Asha", "phones": ["98xxx", "99xxx"] }
More in NoSQL, Replication & Sharding
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | What is the difference between SQL and NoSQL databases? | Concept | Easy | Read the answer to What is the difference between SQL and NoSQL databases? | |||
| 2 | What are the different types of NoSQL databases? | Concept | Easy | Read the answer to What are the different types of NoSQL databases? | |||
| 3 | What is the CAP theorem? | Concept | Medium | Read the answer to What is the CAP theorem? | |||
| 4 | What is database replication, and what are its types? | Concept | Medium | Read the answer to What is database replication, and what are its types? | |||
| 5 | What is sharding, and how is it different from partitioning? | Concept | Medium | Read the answer to What is sharding, and how is it different from partitioning? |