What is a Relational Database?
Think of it like spreadsheets that talk to each other
A database is an organized way to store and retrieve data. You already use something similar — spreadsheets. A database is like a collection of spreadsheets, except it’s much faster at finding things, and the “spreadsheets” can reference each other automatically.
Almost everything you use online is powered by a database: your social media feed, your bank account, your school grades, online shopping — they all store information in databases.
Tables, Rows, and Columns
Data lives in tables. Each table is about one type of thing — students, books, orders, etc.
A table has columns (the categories of information) and rows (the individual records). Here’s a “students” table:
| id | name | grade | favorite_subject |
|---|---|---|---|
| 1 | Alice | 5 | Math |
| 2 | Bob | 4 | Science |
| 3 | Chloe | 5 | Art |
columns
4
id, name, grade, favorite_subject
rows
3
Alice, Bob, Chloe
table
1
students
Every Row Needs an ID: Primary Keys
How do you tell two students apart if they have the same name? Every table has a special column called a primary key — usually called “id”. It’s a unique number for each row, like a student ID card.
Rule: No two rows can have the same primary key. If Alice is id=1, no one else can be id=1.
This seems simple, but it’s the foundation of everything. Primary keys are how the database keeps track of each record and how other tables refer to it.
Connecting Tables: Foreign Keys
The “relational” in relational database means tables can point to each other. Suppose we want to store test scores. We could add columns like “math_score” and “science_score” to the students table — but what if a student takes 10 tests? 50? We’d need a column for every possible test. That doesn’t scale.
Instead, we create a separate table for test scores, and use a foreign key to link each score back to a student:
students
| id 🔑 | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
test_scores
| id 🔑 | student_id 🔗 | subject | score |
|---|---|---|---|
| 1 | 1 | Math | 95 |
| 2 | 1 | Science | 88 |
| 3 | 2 | Math | 72 |
See how student_id in the test_scores table matches the id in the students table? That’s a foreign key. It tells us:
- Rows 1 and 2 in test_scores (Math 95, Science 88) both belong to Alice (student_id = 1)
- Row 3 (Math 72) belongs to Bob (student_id = 2)
Why not just put everything in one big table? Because data repeats. If Alice takes 10 tests, you’d repeat her name, grade, and favorite_subject 10 times. Separate tables keep data clean and organized. This principle is called normalization.
Types of Relationships
Tables connect to each other in predictable patterns:
One-to-Many (1:N)
One student has many test scores. One teacher has many students. This is the most common type.
Many-to-Many (M:N)
A student can take many classes, and a class has many students. This needs a third “junction” table in between.
One-to-One (1:1)
One student has one locker. Less common, but useful for splitting large tables into smaller ones.
Asking Questions with SQL
SQL (Structured Query Language) is how you talk to a database. Instead of scrolling through rows yourself, you write a short command and the database finds the answer instantly — even across millions of rows.
Here’s a query that finds all students’ math scores, sorted from highest to lowest:
SELECT students.name, test_scores.score FROM students JOIN test_scores ON students.id = test_scores.student_id WHERE subject = 'Math' ORDER BY score DESC
Reading it almost like English: “Select the name and score from students joined with test_scores, where the subject is Math, ordered by score from high to low.”
result
| name | score |
|---|---|
| Alice | 95 |
| Bob | 72 |
Key Vocabulary
Table
A collection of related data, like a spreadsheet tab
Row
One record in a table (also called a "record" or "tuple")
Column
A category of data, like "name" or "score" (also called a "field")
Primary Key (PK)
A unique identifier for each row, usually "id"
Foreign Key (FK)
A column that references another table's primary key
SQL
The language used to query and manipulate databases
JOIN
Combining rows from two tables based on a shared key
Schema
The structure of a database — which tables exist and how they connect
Try SQL yourself
Practice writing queries in an in-browser database.
🗺️Explore real schemas
See how real products structure their tables and relationships.