How do you choose between a SQL and a NoSQL database?
Choose SQL when data is relational, you need joins, transactions and flexible queries, and the schema is fairly stable. Choose NoSQL when you need a flexible schema, very high scale for simple access patterns known in advance, or a data model like documents, key-value or graphs. Many systems use both.
Explain it simply
A SQL database is like a set of neat Excel sheets with fixed columns, linked to each other by IDs. The students sheet links to marks by roll number. Because the structure is fixed and linked, you can ask almost any question later: "average marks of girls from Ranchi in maths".
A NoSQL database is a family of other shapes. A document store is like keeping one folder per student with everything inside, and folders can look different from each other. A key-value store is like a locker: give the key, get the value, very fast. Others store wide columns or graphs of connections.
SQL gives you structure and guarantees. NoSQL gives you flexibility and easier scale-out, if you know how you will read the data.
Example
An online learning app:
SQL (PostgreSQL)
users, courses, payments, enrollments
- payment and enrollment must succeed or fail together -> transaction
- admin reports change every month -> ad hoc SQL
Document store (MongoDB)
course content: lessons with varied fields (video, quiz, code exercise)
- each lesson has a different shape -> flexible schema
Key-value (Redis / DynamoDB)
sessions, rate-limit counters, "continue watching" position
- lookup by user ID only, huge volume -> simple and fastHow it works
| SQL (relational) | NoSQL | |
|---|---|---|
| Data model | Tables, rows, fixed columns | Documents, key-value, wide-column, graph |
| Schema | Defined up front, changed with migrations | Flexible, often per record |
| Queries | Rich SQL, joins, aggregations | Usually by key or index; joins limited or absent |
| Transactions | Strong ACID across rows and tables | Varies; often per item, some support multi-item |
| Scaling | Scale up, read replicas; sharding takes effort | Many are designed to shard and scale out |
| Examples | PostgreSQL, MySQL, SQL Server | MongoDB, Cassandra, DynamoDB, Redis, Neo4j |
Questions to ask before choosing
- Is the data naturally related (users, orders, products)? That points to SQL.
- Do I need multi-row transactions, like debit one account and credit another? SQL is the safer default.
- Do I know all my query patterns up front, and are they simple lookups at huge scale? NoSQL fits well.
- Does each record have a different shape, or does the shape change often? A document store helps.
- Is the data about connections, like "friends of friends"? Consider a graph database.
Real-world use
Payments, banking, orders and inventory almost always live in SQL databases because correctness matters most. Chat messages, activity feeds, IoT readings and analytics events often go to NoSQL stores built for high write volume. A UPI-scale system may keep the ledger in a relational store while sending event streams and notifications through NoSQL and queues. This mix is called polyglot persistence.
Common mistakes
- Picking NoSQL "because it scales" for an app with ten thousand users and complex relational data.
- Thinking NoSQL means "no schema". The schema still exists; it just lives in your code instead of the database.
- Thinking SQL cannot scale. Relational databases run some of the largest systems in the world with replicas, caching and sharding.
- Choosing a database before listing the queries the app will run.
What to say in the interview
"I start from the data and the queries. If the data is relational, needs transactions across records, or the queries will change over time, I pick a SQL database like PostgreSQL. If the access patterns are simple and known, like lookups by user ID, the volume is very high, or the records vary in shape, I'd consider NoSQL: a document store for flexible records, key-value for sessions and counters, wide-column for heavy write workloads, a graph database for relationships. In many real systems I'd use SQL as the source of truth and add NoSQL for specific high-scale paths."
Follow-up questions
- What does ACID stand for?
- What is eventual consistency, and when is it acceptable?
- How would you scale a PostgreSQL database that has become slow?
- Would you store an e-commerce product catalogue in SQL or a document store?
Go deeper
Read these once the basics above feel easy.
PostgreSQL supports JSON columns with indexes, so a relational database can hold some flexible data without a separate store. Denormalisation in NoSQL means duplicating data so one read gets everything, at the cost of keeping copies in sync on updates.
At large scale the real trade-offs are around consistency, partitioning and operational cost. Distributed SQL databases (such as CockroachDB, Spanner or YugabyteDB) offer SQL and transactions with horizontal scaling, using consensus underneath, at the cost of higher write latency. Data model choice also shapes the CAP behaviour you get during failures.