Skip to content
We areTOP 1%

What is the difference between SQL and NoSQL databases?

EasyConceptNoSQL, Replication & Sharding
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

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

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account