Skip to content

SQL vs. NoSQL

One-Liner

The choice between structured, relational databases (SQL) and flexible, non-relational databases (NoSQL).

What It Is

  • SQL (Structured Query Language): Databases that use a structured schema (tables, rows, columns) and are known for reliability and complex query capabilities (e.g., MySQL, PostgreSQL). They adhere to ACID properties.
  • NoSQL (Not Only SQL): Databases that do not use a fixed schema and are known for their flexibility, scalability, and performance. They come in various models (key-value, document, column-family, graph) and often favor BASE properties.

Why It Exists

To provide different database models for different needs. SQL is great for transactional, structured data, while NoSQL is built for the scale and flexibility required by modern web applications.

How It Works

  • SQL: Enforces a predefined schema. Data is normalized to reduce redundancy.
  • NoSQL: Schema is dynamic. Data is often denormalized for faster reads.

Tradeoffs

SQL

  • Pros: Strong consistency (ACID), powerful query language, mature ecosystem.
  • Cons: Difficult to scale horizontally, rigid schema.

NoSQL

  • Pros: Easy to scale horizontally, flexible schema, high performance for specific access patterns.
  • Cons: Weaker consistency guarantees (eventual consistency), joins are often not supported.

Failure Modes

  • SQL: The database becomes a single point of failure and a scaling bottleneck.
  • NoSQL: Data inconsistency due to eventual consistency, application-level complexity to handle lack of joins.

Interview Traps

  • Saying one is always better than the other.
  • Not being able to justify the choice of one over the other for a specific use case.

Real-World Usage

  • SQL: Financial systems, e-commerce order management.
  • NoSQL: Social media feeds, IoT data, real-time analytics.

Anti-Patterns

  • Using NoSQL for a system that requires complex transactions and joins.
  • Using SQL for a system that has a rapidly evolving data model and needs to scale massively.
  • ACID vs. BASE
  • Database Sharding
  • Read Replicas