stackademic

The leading education platform for anyone with an interest in software development.

SQL vs NoSQL

How relational and non-relational databases differ and when to use each

Overview

SQL databases store data in structured tables with a fixed schema and use the SQL language to query it. NoSQL databases cover a range of models, such as document, key-value, and graph stores, that trade a strict schema for flexibility and easy horizontal scaling. Choosing between them depends on how structured your data is, how you query it, and the consistency guarantees you need.

Syntax / Usage

SQL uses declarative queries over tables, while a document database stores flexible JSON-like records.

-- SQL: relational query across tables
SELECT users.email, orders.total
FROM orders
JOIN users ON users.id = orders.user_id
WHERE orders.total > 100;
// NoSQL (document): a self-contained record, queried by fields
db.orders.find({ total: { $gt: 100 } })
// { _id: 1, user: { email: "ada@example.com" }, total: 120 }

Examples

Relational data with clear relationships fits SQL well:

SELECT COUNT(*) FROM orders WHERE user_id = 1;

A flexible document avoids joins by nesting related data together:

db.users.insertOne({
  email: "ada@example.com",
  addresses: [{ city: "London" }, { city: "Berlin" }]
})

Common Mistakes

  • Choosing NoSQL for scale before you actually have a scaling problem
  • Assuming NoSQL means no schema; you still need consistent structure in application code
  • Expecting full multi-record ACID transactions from every NoSQL store by default
  • Recreating relational joins manually in a document store instead of modeling for your queries
  • Picking a database by hype rather than by your data shape and access patterns

See Also

databases-relational-basics databases-normalization databases-acid