Data6 min read10 interview questions
SQL or NoSQL: how to choose
Two families of databases, and a simple way to pick.
In one sentenceSQL databases store neat tables with strict rules and links between them. NoSQL databases store data in other shapes that are easier to spread and change.
Think of it like thisSQL is a well-organised filing cabinet with labelled folders. NoSQL is a set of boxes where each box can hold whatever suits it.
The big picture
Tap any step to see where to use it, how to use it and an example result.
What SQL gives you
Tables with set columns, relationships between them, powerful queries and transactions that keep data correct, such as moving money out of one account and into another or neither. PostgreSQL, MySQL and SQLite are common.
What NoSQL offers
Several kinds: document stores like MongoDB keep flexible JSON-like records, key-value stores like Redis are extremely fast, wide-column and graph stores suit special jobs. They often scale across many machines more easily and allow records that differ in shape.
A sensible rule
Most applications are well served by a SQL database, and modern ones handle JSON too. Choose NoSQL for a specific need: huge scale, very flexible data, or very fast simple lookups.
Side by side
| Feature | SQL | NoSQL |
|---|---|---|
| Data shape | Tables with fixed columns | Documents, keys, graphs and more |
| Relationships | Built in with joins | Usually handled in code |
| Correctness | Strong transactions | Varies by product |
| Scaling | Mostly one big machine, with effort to spread | Often designed to spread out |
| Examples | PostgreSQL, MySQL, SQLite | MongoDB, Redis, Cassandra |
Common mistakes
- Choosing NoSQL because it sounds modern.
- Choosing SQL but storing everything as one big text column.
- Not thinking about how the data will be queried before choosing.
Quick recap
- SQL: tables, relationships, strong correctness
- NoSQL: flexible shapes and easy scaling
- Start with SQL for most apps
- Choose by your data and your questions
Try it yourself
Write down the data of a to-do app, a chat app and a shop. For each one, decide which type fits and say why in one sentence.
Top 10 interview questions
Ordered from basic to advanced. Try answering out loud first, then open the answer.
What is a relational (SQL) database?Basic
Data is stored in tables of rows and columns with a defined structure, and tables are related through keys. You query it with SQL. Examples: PostgreSQL, MySQL.
What is a NoSQL database?Basic
A broad name for databases that do not use the table model. They include document, key-value, column and graph stores, for example MongoDB, Redis, Cassandra and Neo4j.
What are the main types of NoSQL databases?Basic
Document (JSON-like records), key-value (fast lookups by key), wide-column (huge, write-heavy data) and graph (relationships between things).
What is a schema?Basic
The defined structure of the data: tables, columns and their types. SQL databases enforce it; many NoSQL databases are flexible about it.
What are ACID properties?Intermediate
Atomicity (all or nothing), Consistency (rules always hold), Isolation (transactions do not disturb each other) and Durability (committed data survives crashes). Relational databases are known for them.
What is the CAP theorem in simple terms?Intermediate
When a network splits, a distributed system must choose between staying consistent and staying available. You cannot have both perfectly during a partition.
What is eventual consistency?Intermediate
After a write, all copies of the data may briefly disagree, but if there are no new writes they will all become the same. It trades immediate accuracy for speed and availability.
When would you pick SQL, and when NoSQL?Intermediate
SQL for structured data with relationships and transactions, such as orders and payments. NoSQL when the shape varies, the scale is very large or access is simple by key, such as sessions, caches or event logs.
How do the two approaches scale differently?Advanced
Relational databases traditionally scale up on a bigger machine, and scale out with read replicas, partitioning or sharding. Many NoSQL systems were designed to spread across many nodes from the start.
Can you use both in one system?Advanced
Yes, and many do. A relational database as the system of record, Redis for caching, a search engine for text, and a document store where data is flexible. The key is to know which one is the source of truth.
Guides are simplified on purpose. Tool names and details change, so check the official documentation before relying on a detail.