SQL · Module 1 · Lesson 21 min read

Relational vs NoSQL Databases

Relational (SQL) databases

Data lives in tables with a fixed structure (schema), linked by keys.

  • Examples: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite
  • Strong consistency and transactions (ACID) – perfect for money, orders, bookings
  • Powerful queries with JOINs
  • Schema changes need planning (migrations)

NoSQL databases

"Not only SQL" – flexible structures, built to scale out across many servers.

TypeStoresExamplesGood for
DocumentJSON-like documentsMongoDB, Firestoreflexible app data, content
Key-valuekey → valueRedis, DynamoDBcaching, sessions, counters
Wide-columnrows with dynamic columnsCassandrahuge write volumes, time series
Graphnodes + edgesNeo4jsocial networks, recommendations

How to choose

Choose SQL when:

  • Data has clear relationships (users → expenses → categories)
  • You need accuracy and transactions (payments, inventory)
  • You need flexible reporting and analytics

Choose NoSQL when:

  • The data shape changes often or varies a lot per record
  • You need massive scale with simple lookups
  • Use cases like caching (Redis) or real-time sync (Firestore)

The modern reality

Many teams use both (PostgreSQL + Redis cache). And PostgreSQL supports JSONB, so you get flexible document storage inside a relational database — the best of both worlds. That's one reason PostgreSQL is so popular.

Why this course uses PostgreSQL

  • Free and open source, very standards-compliant
  • Powers Supabase, which your app can use as its backend
  • Rich features: JSONB, arrays, full-text search, window functions, extensions

Example

SQL
-- Relational: structured rows with relationships
SELECT u.name, e.title, e.amount
FROM expenses e
JOIN users u ON u.id = e.user_id
WHERE u.name = 'Asha Sharma'
LIMIT 3;

-- PostgreSQL can ALSO store flexible JSON documents (NoSQL-style)
CREATE TABLE app_settings (
  user_id  INT PRIMARY KEY REFERENCES users(id),
  settings JSONB NOT NULL DEFAULT '{}'
);

INSERT INTO app_settings VALUES
  (1, '{"theme": "dark", "currency": "NPR", "notifications": {"bills": true}}'),
  (2, '{"theme": "light", "language": "ne"}');      -- different shape is fine

-- Query inside the JSON
SELECT user_id, settings->>'theme' AS theme
FROM app_settings
WHERE settings @> '{"theme": "dark"}';
--  user_id | theme
-- ---------+-------
--        1 | dark

DROP TABLE app_settings;