Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Learn the Basics  ›  Lesson

SQL vs NoSQL Databases

SQL vs NoSQL Databases 12 minSQL databases fix the data's shape and answer any question, while NoSQL families such as key-value, document, wide-column and graph are shaped around specific access patterns and easy scaling.

SQL vs NoSQL Databases

A team building a chat app hears that "NoSQL scales better" and moves everything, including billing, into a document store. Six months later they are writing code by hand to keep invoice totals consistent. Neither family is better in general; each is better at particular jobs.

The rule

SQL: declare the shape, ask anything. NoSQL: design for the access, scale out.

SQL databases are relational: fixed tables, keys linking them, and one standard language for any question. You design the shape of the data, and queries adapt to it.

NoSQL is a loose name for databases that are not built around relational tables. There are four big families:

Family Stores Good at Example
Key-value a value per key very fast lookups by key Redis
Document JSON-like documents records with varying shape MongoDB
Wide-column rows with flexible column sets huge write volumes Cassandra
Graph nodes and edges following relationships Neo4j

With NoSQL you usually start from the question — "load a user's profile by id" — and shape the data so that one question is cheap. Data is often duplicated on purpose. Many NoSQL systems were built to spread across many machines easily, and some relax consistency to do so, though modern ones increasingly offer transactions too.

"NoSQL" does not mean "no query language" — most have one; it is just not SQL. And the line is blurring: PostgreSQL can store JSON documents, and some NoSQL stores accept SQL-like queries.

The shortcut

Name the question before you pick the store. Write down the main way the data will be read, then match it:

  • Must be correct and questioned in many ways → relational
  • Fetched by one key, very fast → key-value
  • Each record has its own shape, read as a whole → document
  • The question is about connections → graph
Placing the shop's dataStep 1 of 4
  1. Orders and payments

    Must be correct, and analysts will slice them many ways. Relational.

The trap: choosing a store for its reputation instead of for the question it must answer.

Drills

Answer fast — trust the rule.

quick drill

Session tokens are looked up by token millions of times a day and expire after 30 minutes. Which store fits best?

quick drill

Which statement about NoSQL is true?

quick drill

A general ledger where every debit must match a credit, audited yearly in new ways. Best default?

quick drill

A social app's main query is "friends of my friends who live in my city". Which family is designed for this?

Remember

  • SQL: declare the shape, ask anything. NoSQL: design for the access, scale out.
  • Name the question before you pick the store.
  • NoSQL is a family of designs, not "no queries" and not automatically faster.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPick a store for two features

A shop needs to store (1) customer orders and (2) the items currently in each visitor's shopping cart, which are read on every page view and thrown away after an hour. Recommend a kind of database for each and justify each choice by the question the data must answer.