← Back to PostgreSQL Course | Chapter 1: Introduction | Lesson 2 of 7

SQL vs NoSQL

Relational databases like PostgreSQL use fixed tables and SQL, while NoSQL databases trade structure for flexibility.

In this page:

  1. SQL vs NoSQL

SQL vs NoSQL

A relational database stores data in tables with a defined schema, enforces relationships with keys, and uses SQL and joins.

NoSQL systems (document, key-value, graph, column) choose flexible schemas and often scale horizontally. PostgreSQL blurs the line with JSONB, so you can mix structured columns and flexible documents.

Note: Use a relational database when data has clear relationships and you need transactions.

Example: SQL vs NoSQL

sql
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT, city TEXT);
CREATE TABLE orders (id INTEGER PRIMARY KEY, customer_id INTEGER, total INTEGER);
INSERT INTO customers VALUES (1, 'Ada', 'London'), (2, 'Bob', 'Oslo');
INSERT INTO orders VALUES (10, 1, 50), (11, 1, 20), (12, 2, 75);
-- Relational: related data lives in separate tables and is combined with a JOIN
SELECT c.name, o.total FROM customers c JOIN orders o ON o.customer_id = c.id ORDER BY o.id;

-- Output:
-- name | total
-- Ada | 50
-- Ada | 20
-- Bob | 75
Related Topics
Common Mistakes
  1. Choosing NoSQL only because it is trendy
  2. Storing everything as JSON in a relational database
  3. Assuming SQL cannot scale
Chapter Summary
  • SQL databases use tables, schemas and joins
  • NoSQL trades structure for flexibility
  • PostgreSQL supports JSONB too
  • Choose by data shape and consistency needs
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.