databases30 min

The Relational Model

Organizing data into tables with rows and columns

0/9Not Started

Why This Matters

Every app you use -- social media, banking, e-commerce, healthcare -- stores its data somewhere. The overwhelming majority of that data lives in relational databases. When you sign up for an account, your name, email, and password go into a table. When you place an order, the items, quantities, and prices go into another table. These tables are linked together by relationships, and SQL is the language you use to ask questions about that data.

Understanding the relational model is not optional for software engineers. It is the foundation that every backend system, data pipeline, and API is built on. Even "NoSQL" databases are often understood by contrast to the relational model. Learn this, and every database concept that follows will make sense.

Define Terms

Visual Model

Usersid | name | email
Ordersid | user_id | total
Productsid | name | price
JOINusers.id = orders.user_id
JOINorders.id = items.order_id
Result Setname | product | total

The full process at a glance. Click Start tour to walk through each step.

A relational database organizes data into tables. SQL queries with JOINs combine them to answer questions.

Code Example

Code
-- SQL: Creating a table
CREATE TABLE users (
  id      INTEGER PRIMARY KEY,
  name    TEXT NOT NULL,
  email   TEXT UNIQUE NOT NULL,
  age     INTEGER
);

-- Insert rows
INSERT INTO users (id, name, email, age)
VALUES (1, Alice, alice@example.com, 28);

INSERT INTO users (id, name, email, age)
VALUES (2, Bob, bob@example.com, 34);

INSERT INTO users (id, name, email, age)
VALUES (3, Charlie, charlie@example.com, 22);

-- SELECT: retrieve specific columns
SELECT name, email FROM users;
-- Returns:
-- Alice   | alice@example.com
-- Bob     | bob@example.com
-- Charlie | charlie@example.com

-- WHERE: filter rows
SELECT name, age FROM users WHERE age > 25;
-- Returns:
-- Alice | 28
-- Bob   | 34

-- ORDER BY: sort results
SELECT name, age FROM users ORDER BY age DESC;
-- Returns:
-- Bob     | 34
-- Alice   | 28
-- Charlie | 22

Interactive Experiment

Try these exercises in a SQL playground (like sqliteonline.com):

  • Create a products table with columns: id, name, price, category. Insert 5 products.
  • Write a SELECT query that finds all products under $20.
  • Write a query that counts how many products are in each category using GROUP BY.
  • Try inserting a row with a duplicate primary key. What error do you get?

Quick Quiz

Coding Challenge

Build a Query Result Filter

Write a function called `queryWhere` that simulates a SQL WHERE clause. It takes an array of row objects and a filter function, and returns only the rows that match. Then write `selectColumns` that takes rows and an array of column names, returning objects with only those columns.

Loading editor...

Real-World Usage

The relational model is the backbone of production systems:

  • Web applications: Every Rails, Django, and Express app typically uses PostgreSQL or MySQL to store users, sessions, and business data.
  • Banking systems: Financial transactions require strict data integrity that relational databases guarantee through constraints and transactions.
  • E-commerce: Product catalogs, shopping carts, orders, and inventory are all relational data with clear relationships between tables.
  • Analytics: Data warehouses like Snowflake and BigQuery use SQL to query terabytes of data organized in relational tables.
  • APIs: REST and GraphQL APIs translate between HTTP requests and SQL queries against relational databases.

Connections