PostgreSQL
SQL vs NoSQL Databases
When you decide to build an application that stores data, one of the very first decisions you make is: SQL or NoSQL? Both are legitimate, widely-used approaches — but they solve different problems. This lesson explains both clearly, shows you exactly how they differ, and helps you understand when to use each one. By the end you will have a solid, confident answer to this question.
Two Approaches to Storing Data
Over decades of building software, engineers developed two main philosophies for how to store and organise data. The first approach — SQL — organises everything into structured tables with strict rules. The second approach — NoSQL — stores data in flexible formats with fewer restrictions. Neither one is universally better. Each one fits certain situations better than the other.
SQL Databases — Structured and Relational
SQL databases are also called Relational Databases. They store data in tables — rows and columns — exactly like you learned in Lesson 1. Every table has a fixed structure called a schema that defines exactly which columns exist and what type of data each column holds. You cannot add a row that does not fit that structure.
SQL databases are built around relationships — tables link to each other using primary keys and foreign keys. You can combine data from multiple tables in a single query. They are also built around ACID — a set of rules that guarantee your data is always accurate and safe, even when things go wrong (you will learn about ACID in detail in Lesson 37).
Popular SQL databases: PostgreSQL, MySQL, Microsoft SQL Server, Oracle, SQLite
Best suited for:
- Financial systems — banks, payment platforms, accounting software
- E-commerce — products, orders, customers, inventory all linked together
- Healthcare — patient records, prescriptions, appointments
- Any application where data accuracy and consistency are critical
NoSQL Databases — Flexible and Scalable
NoSQL databases do not require a fixed table structure. Instead of rows and columns, data can be stored in several flexible formats depending on the type of NoSQL database. The most common format is a document — similar to a JSON object — where each record can have different fields and structures.
NoSQL databases were designed for situations where data is unstructured, changes shape frequently, or needs to scale to massive volumes across many servers very quickly. A social media platform might store millions of user posts per second — each post having a completely different structure depending on whether it contains text, images, videos, polls, or links.
Four types of NoSQL databases:
- Document — stores data as JSON-like documents. Example: MongoDB. Used for user profiles, content management, catalogues.
- Key-Value — stores simple key → value pairs, extremely fast. Example: Redis. Used for caching, sessions, leaderboards.
- Column-Family — stores data in column groups, optimised for huge datasets. Example: Apache Cassandra. Used for IoT data, analytics.
- Graph — stores data as nodes and edges (connections). Example: Neo4j. Used for social networks, recommendation engines, fraud detection.
SQL vs NoSQL — Side by Side
| Feature | SQL | NoSQL |
|---|---|---|
| Data structure | Tables with fixed columns | Documents, key-value, graph, column |
| Schema | Fixed — defined upfront | Flexible — can change anytime |
| Query language | SQL (standardised) | Varies by database |
| Relationships | Built-in with JOINs | Manual, embedded, or limited |
| Data consistency | Strong (ACID guaranteed) | Eventual consistency (usually) |
| Scaling | Scales vertically (bigger server) | Scales horizontally (more servers) |
| Speed at scale | Very fast with proper indexing | Extremely fast for simple lookups |
| Best for | Structured, related, critical data | Flexible, high-volume, varied data |
| Examples | PostgreSQL, MySQL, SQL Server | MongoDB, Redis, Cassandra, Neo4j |
How the Same Data Looks in SQL vs NoSQL
The best way to feel the difference is to see the same data stored both ways. Below is a customer record stored in a SQL table compared to the same record stored as a NoSQL document.
-- SQL: Customer stored in a fixed table structure
-- Every row MUST have all these columns
SELECT * FROM customers WHERE id = 1;
----+------------+-----------+--------------------+----------+------------
1 | Alice | Morgan | alice@example.com | New York | 2023-06-01
-- NoSQL (MongoDB-style): Same customer stored as a document
-- Fields can be added or removed per document — no fixed structure required
{
"_id": 1,
"first_name": "Alice",
"last_name": "Morgan",
"email": "alice@example.com",
"city": "New York",
"joined": "2023-06-01",
"preferences": ["dark mode", "email notifications"],
"last_order": {
"product": "Laptop",
"amount": 1200,
"date": "2024-01-15"
}
}
Key observations:
- The SQL row is clean, predictable, and consistent — every customer has exactly the same columns.
- The NoSQL document is flexible — Alice has a
preferencesarray and an embeddedlast_orderobject. Another customer's document could have completely different extra fields. - In SQL, the order details would be in a separate
orderstable linked by foreign key. In NoSQL, related data is often embedded directly inside the document. - Neither approach is wrong — they reflect different design philosophies and serve different use cases.
Vertical Scaling vs Horizontal Scaling
One of the biggest practical differences between SQL and NoSQL is how they handle growth. When your application gets more users and more data, your database needs to grow with it. There are two ways to do this.
Vertical scaling means upgrading the existing server — adding more RAM, a faster CPU, or larger storage. SQL databases typically scale this way. It is reliable and simple, but there is a physical limit to how big one server can get, and it can become expensive.
Horizontal scaling means adding more servers and spreading the data across all of them. NoSQL databases are designed for this. Instead of one powerful machine, you have many ordinary machines working together. This is how companies like Amazon, Netflix, and Facebook handle billions of requests per day.
It is worth noting that modern PostgreSQL can also scale horizontally using tools like Citus and read replicas — it is not limited to vertical scaling. But traditional SQL scaling starts vertical, while NoSQL is built horizontal from day one.
Choosing the Right Database for the Job
There is no single correct answer — the right choice depends entirely on what your application needs. Here is a practical guide to help you decide.
| Your Situation | Recommended Choice |
|---|---|
| Financial transactions, banking, payments | SQL — data accuracy is non-negotiable |
| E-commerce with products, orders, customers | SQL — relationships between tables are essential |
| Real-time chat, session management, caching | NoSQL (Redis) — speed is the priority |
| Content management, blogs, product catalogues | NoSQL (MongoDB) — flexible document structure fits well |
| Social network friend connections | NoSQL (Graph) — relationships between users are the data |
| Reporting, analytics, business intelligence | SQL — complex queries and aggregations are SQL's strength |
| IoT sensor data, millions of writes per second | NoSQL (Cassandra) — built for massive write throughput |
Many modern applications use both — for example, an e-commerce platform might use PostgreSQL for orders and customers (where accuracy matters) and Redis for the shopping cart and page caching (where speed matters). This is called a polyglot persistence architecture and it is very common in professional engineering teams.
Lesson Summary
| Concept | Key Point |
|---|---|
| SQL database | Tables, fixed schema, relationships, ACID, standardised SQL language |
| NoSQL database | Flexible schema, various formats (document, key-value, graph, column) |
| Schema | SQL = fixed upfront. NoSQL = flexible, changes anytime. |
| Scaling | SQL = vertical (bigger server). NoSQL = horizontal (more servers). |
| Consistency | SQL = strong ACID guarantees. NoSQL = eventual consistency. |
| Use SQL when | Data is structured, related, and accuracy is critical |
| Use NoSQL when | Data is flexible, high-volume, or needs rapid horizontal scaling |
| Real-world reality | Most large applications use both SQL and NoSQL together |
🧪 Practice Questions
Answer based on what you learned in this lesson.
1. The fixed structure that defines which columns a SQL table has is called a __________.
2. Adding more servers to handle more data is called __________ scaling.
3. MongoDB stores data as __________ — similar to JSON objects.
4. SQL databases guarantee data safety through a set of rules called __________.
5. Which NoSQL database is most commonly used for caching and session management due to its extreme speed?
🎯 Quiz — Test Your Understanding
Q1. Which type of database is best suited for storing real-time chat messages at massive scale?
Q2. A bank needs to guarantee that every transaction is 100% accurate and never lost. Which database type should it use?
Q3. Which type of NoSQL database is specifically designed for storing connections between people in a social network?
Q4. What is it called when an application uses both SQL and NoSQL databases together?
Q5. What is the main advantage of a flexible schema in NoSQL?
Next up: What is PostgreSQL and why does the world trust it?