SQL and NoSQL databases solve different data-storage requirements. In system design interviews, the important part is understanding their data model, consistency, scalability, and when to choose each one.
flowchart LR
A["Application"] --> SQL["SQL Database"]
A --> NOSQL["NoSQL Database"]
SQL --> R["Structured Relational Data"]
NOSQL --> F["Flexible / Distributed Data"]
1. SQL Database
SQL databases store data in tables containing rows and columns. Relationships between tables are represented using keys.
erDiagram
CUSTOMER ||--o{ ORDER : places
CUSTOMER {
int CustomerId
string Name
}
ORDER {
int OrderId
int CustomerId
decimal Amount
}
Examples include:
- SQL Server
- PostgreSQL
- MySQL
- Oracle Database
2. NoSQL Database
NoSQL databases use models such as document, key-value, column-family, or graph storage.
flowchart LR
A["Application"] --> N["NoSQL Database"]
N --> D["Document"]
N --> K["Key-Value"]
N --> C["Column Family"]
N --> G["Graph"]
Examples include:
- MongoDB
- Amazon DynamoDB
- Cassandra
- Redis
- Neo4j
3. SQL vs NoSQL
| Feature | SQL | NoSQL |
|---|---|---|
| Data Model | Tables / Rows / Columns | Document / Key-Value / Column / Graph |
| Schema | Usually predefined | Often more flexible |
| Relationships | Strong relational support | Often modeled differently or denormalized |
| Joins | Strong support | Often avoided or limited depending on database |
| Transactions | Strong transaction support | Depends on database |
| Scaling | Often scale-up; horizontal scaling also possible | Often designed for horizontal scaling |
| Best suited for | Structured relational data | Flexible or highly distributed workloads |
4. When Should You Choose SQL?
SQL is a natural choice when the application has complex relationships, structured data, strong transactional requirements, and frequent joins.
flowchart LR
A["Banking Application"] --> DB["SQL Database"]
DB --> C["Customer"]
DB --> A1["Account"]
DB --> T["Transaction"]
C --> A1
A1 --> T
Examples include banking transactions, order management, accounting, and many enterprise applications.
5. When Should You Choose NoSQL?
NoSQL can be useful when the workload requires flexible data models, very large-scale distributed storage, or access patterns that fit a particular NoSQL model.
flowchart LR
A["Application"] --> DB["NoSQL"]
DB --> U1["User Data"]
DB --> U2["User Activity"]
DB --> U3["Events"]
DB --> U4["Large Distributed Dataset"]
6. Horizontal Scaling
Many NoSQL systems are designed around distributing data across multiple nodes.
flowchart LR
A["Application"] --> N["NoSQL Cluster"]
N --> N1["Node 1"]
N --> N2["Node 2"]
N --> N3["Node 3"]
N --> N4["Node 4"]
However, SQL databases can also scale horizontally using techniques such as replication, partitioning, and sharding. Therefore, "SQL cannot scale" is an incorrect interview statement.
7. Don't Choose Based Only on Scale
A common interview mistake is saying: "Huge traffic means NoSQL."
Instead, first understand the application's data model, query patterns, consistency requirements, transaction requirements, and scaling needs.
flowchart TD
R["Database Requirement"] --> D["Data Model"]
R --> Q["Query Pattern"]
R --> C["Consistency"]
R --> T["Transactions"]
R --> S["Scale"]
D --> DB["Choose Database"]
Q --> DB
C --> DB
T --> DB
S --> DB
8. Can We Use Both?
Yes. A system can use different databases for different workloads.
flowchart LR
A["Application"] --> SQL["SQL Server"]
A --> N["NoSQL"]
A --> R["Redis"]
SQL --> T["Transactional Data"]
N --> E["High-Scale / Flexible Data"]
R --> C["Cache"]
This is commonly called polyglot persistence.
Important Interview Questions
- SQL vs NoSQL?
- When would you choose SQL?
- When would you choose NoSQL?
- Can SQL databases scale horizontally?
- Why are relationships easier in SQL?
- What are the major types of NoSQL databases?
- What is polyglot persistence?
- Would you choose NoSQL just because the system has high traffic?
- How would you select a database for a new system?
Key Takeaways
- SQL → structured relational data and strong transaction support.
- NoSQL → flexible data models and distributed workloads.
- Database choice should depend on requirements, not popularity.
- SQL can also scale horizontally using appropriate architecture.
- A system can use both SQL and NoSQL for different workloads.
Conclusion
In interviews, don't answer "SQL or NoSQL?" immediately. First analyze data model → queries → consistency → transactions → scale, and then choose the database that fits those requirements.