Relational vs NoSQL Databases
Not all databases are relational. SQL databases (RDBMS) store data in structured tables with fixed schemas. NoSQL databases use flexible formats like JSON documents, key-value pairs, or graphs. Choosing the right type depends on your data structure and application needs.
Comparison
- Relational (SQL): MySQL, PostgreSQL, SQLite, SQL Server, Oracle
- NoSQL: MongoDB (document), Redis (key-value), Cassandra (column), Neo4j (graph)
- SQL: Structured schema, tables, relationships — great for complex queries
- NoSQL: Flexible schema, horizontal scaling — great for unstructured data
- SQL strength: ACID transactions, JOINs, complex queries
- NoSQL strength: Speed, scalability, JSON-like data, flexible structure
- Most production systems use BOTH (polyglot persistence)
- SQL is best for: banking, e-commerce, HR, inventory systems
When to Use Which
-- RELATIONAL (SQL) — best for:
-- Structured data with clear relationships
-- Financial transactions (require ACID)
-- Reports and analytics with complex JOINs
-- Example: E-commerce
-- users table → orders table → order_items table (related via keys)
-- SELECT u.name, COUNT(o.id) AS order_count
-- FROM users u
-- JOIN orders o ON u.id = o.user_id
-- GROUP BY u.name;
-- NOSQL (MongoDB, Redis) — best for:
-- Unstructured or semi-structured data
-- Real-time apps, caching, sessions
-- Rapidly-changing schemas (startups iterating fast)
-- Example: Product catalog (MongoDB document)
-- { "_id": "abc123", "name": "Laptop", "specs": { "ram": "16GB" }, "tags": ["electronics"] }
-- Modern apps often combine BOTH:
-- PostgreSQL for user accounts, orders (structured)
-- Redis for caching and sessions (fast key-value)
-- MongoDB for product catalogs (flexible JSON)Tip
Tip
Most modern applications use both SQL and NoSQL databases together — this is called polyglot persistence. Use SQL for structured data that needs transactions (users, orders, payments) and NoSQL for flexible or high-speed data (caching, sessions, product catalogs).
SQL for structured data + ACID. MongoDB for flexible schemas. Redis for caching + sessions.
Common Mistake
Warning
Don't choose NoSQL just because it sounds modern or scalable. Most applications under 10 million rows run perfectly on PostgreSQL or MySQL. Starting with SQL gives you transactions, JOINs, and data integrity — features that are hard to retrofit later.
Practice Task
Note
Think of an app you use daily (e.g., Amazon, Instagram). List 3 types of data it stores and decide whether each is best suited for SQL (structured, relational) or NoSQL (flexible, high-volume). Example: user accounts → SQL, activity feed → NoSQL.
Quick Quiz
Key Takeaways
- Not all databases are relational.
- Relational (SQL): MySQL, PostgreSQL, SQLite, SQL Server, Oracle
- NoSQL: MongoDB (document), Redis (key-value), Cassandra (column), Neo4j (graph)
- SQL: Structured schema, tables, relationships — great for complex queries