← Back to System Design, page 1

system-design-0014 · Easy fresher

SQL vs NoSQL: what are the real differences, and how do you decide which to use?

SQL databases store data in tables with a fixed schema, support joins and multi-row ACID transactions, and are flexible for ad hoc queries. NoSQL is a family of stores (key-value, document, wide-column, graph) that trade some of those features for flexible schemas, simple horizontal scaling or a data model that fits a specific access pattern. Choose by data shape, query patterns, consistency needs and scale.

Full answer

Relational (SQL)

Data lives in tables with typed columns. Relations are expressed with foreign keys and queried with joins. Transactions are ACID: a fee payment and the receipt row are saved together or not at all. You can ask new questions later ("average score by school by subject") without redesigning storage. Examples: PostgreSQL, MySQL, SQL Server.

NoSQL families

TypeModelGood forExamples
Key-valuekey -> blobSessions, caches, feature flagsRedis, DynamoDB (also document)
DocumentJSON-like documentsRecords read as a whole, varied fieldsMongoDB, Firestore
Wide-columnRows with many columns grouped by partition keyHuge write volume, time series, messagesCassandra, ScyllaDB, Bigtable
GraphNodes and edgesRelationship traversalNeo4j

Many NoSQL stores are designed around a known access pattern: you model data for the queries you will run. That makes them fast and easy to scale out for those queries, and awkward for unplanned ones.

Myths to avoid

  • "NoSQL has no schema." The schema still exists; it lives in application code instead of the database.
  • "SQL cannot scale." Relational databases handle very large loads with replicas, partitioning and sharding tools, and distributed SQL databases scale horizontally.
  • "NoSQL is never transactional." MongoDB and DynamoDB support transactions with limits.

How to decide

Ask:

  1. Is the data relational with many joins (students, courses, enrolments, payments)? Start with SQL.
  2. Do you need multi-row transactions and constraints? SQL.
  3. Is the access pattern simple and known, with very high write volume (chat messages by conversation, sensor readings by device)? A wide-column or key-value store fits.
  4. Do records have very different shapes (product catalogue with category-specific fields)? Document store, or SQL with a JSON column.

Most products use both: PostgreSQL as the system of record, Redis for caching and sessions, perhaps Cassandra or a time-series store for one high-volume stream.

By level

Fresher: Explain tables, joins and transactions versus key-value and document stores, with one example use for each. Choosing SQL for payments and explaining why is a good test at this level.

Mid: Model one entity both ways, explain access-pattern-driven design and the transaction and consistency differences. Expect one entity modelled both relationally and for a NoSQL store.

Senior: Discuss polyglot persistence costs, data migration between stores, distributed SQL options, operational maturity, and how consistency and query flexibility affect long-term product changes. The answer should consider the long-term cost of running several kinds of database.

Key points

  • SQL: tables, joins, ACID transactions, ad hoc queries
  • NoSQL is several models: key-value, document, wide-column, graph
  • NoSQL often designs storage around known queries
  • Schema-less really means schema in application code
  • Many systems combine both

Common mistakes

  • Saying SQL cannot scale
  • Choosing NoSQL only because the schema "might change"
  • Ignoring the need for transactions in payments or inventory
  • Treating all NoSQL databases as the same

Follow-ups

  • How would you model chat messages in Cassandra?
  • When would a JSON column in PostgreSQL beat a document database?
  • What does it cost to add a new query pattern to a key-value design?

Tips

  • Name the four NoSQL families with one example each
  • Decide from access patterns, not trends
  • Mention that combining stores is normal

Evaluation rubric

  • Accurate description of relational strengths
  • Knows the NoSQL families and their fits
  • Corrects at least one common myth
  • Gives a decision process tied to requirements