SQL vs NoSQL — Side by Side
Left: SQL structured tables with JOIN lines. Right: NoSQL document clusters. Fire a query on each side and watch the difference.
Code example
The same data operation — SQL JOIN versus MongoDB document query.
-- SQL: structured schema with JOIN
-- Tables defined upfront, schema is strict
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
role VARCHAR(50)
);
CREATE TABLE orders (
id INT PRIMARY KEY,
user_id INT REFERENCES users(id),
total DECIMAL(10,2)
);
-- Query: user with all their orders
SELECT u.name, o.total
FROM users u
JOIN orders o ON o.user_id = u.id
WHERE u.id = 42;
// MongoDB: flexible document store
// No schema — embed or reference freely
// A user document — orders embedded
const userDoc = {
_id: ObjectId("..."),
name: "Naser",
role: "admin",
orders: [ // embedded array
{ total: 99.99, date: "2026-07-01" },
{ total: 149.50, date: "2026-06-15" }
]
};
// Query: find user and all embedded orders
const user = await db.collection('users')
.findOne({ _id: ObjectId("...") });
// No JOIN needed — orders are already there
console.log(user.orders); // [{ total: 99.99 }, ...]
Key concepts
SQL
Structured Query Language. Data lives in tables with defined schemas. Foreign keys enforce relationships. Great for structured data and complex queries.
NoSQL
Catches all non-relational models: document (MongoDB), key-value (Redis), column-family (Cassandra), graph (Neo4j). Schema-optional, often horizontally scalable.
ACID
Atomic (all-or-nothing), Consistent (valid state), Isolated (concurrent transactions don't interfere), Durable (committed = persisted). SQL DBs guarantee this; most NoSQL don't.
CAP Theorem
A distributed system can guarantee at most 2 of: Consistency, Availability, Partition tolerance. NoSQL systems often choose AP (available + partition-tolerant) over CA.