Module 3: Working with Multiple Tables15 min

Understanding Table Relationships

Progress Tracking

Log in to save this lesson and continue from where you left off.

Log in

Why Tables Are Separate

In the first 2 Modules, every query is pulled from a single table. That works for simple questions such as “What are the employee names?” or “Which orders cost more than $50?”

But real business questions usually need data from multiple places. “Show me each order with the customer’s name and email.” That information lives in two different tables: orders and customers.

This separation isn’t a bug. It’s by design. Databases split data across tables to avoid redundancy, maintain accuracy, and stay organized. Before you can combine tables, you need to understand how they relate.

Primary Keys: Unique Identifiers

Every table needs a way to uniquely identify each row. That’s what a primary key does.

Look at the customers table:

Table: customers
idfirst_namelast_namecityaddressphone_number
8JohnJosephSan Francisco928-386-8164
7JillMichaelAustin813-297-0692
4WilliamDanielDenver813-368-1200
5HenryJacksonMiami808-601-7513
13EmmaIsaacMiami808-690-5201

The id column is the primary key. Each value is unique. No two customers share the same id. This lets you reference a specific customer unambiguously.

Primary key formats

Primary keys are almost always integers that auto-increment (1, 2, 3…). Some databases use UUIDs instead, but the concept is the same: one unique value per row.

Foreign Keys: The Connection

A foreign key is a column in one table that references the primary key of another table. It’s how tables link together.

Table: orders
idcust_idorder_dateorder_detailstotal_order_cost
132019-03-04Coat100
232019-03-01Shoes80
332019-03-07Skirt30
472019-02-01Coat25
572019-03-10Shoes80

The cust_id column is a foreign key. It references the id column in the customers table. Order ID 4 belongs to customer ID 7. Order ID 6 belongs to customer ID 15.

Foreign keys aren’t unique

Foreign keys don’t have to be unique. Customer ID 3 has multiple orders. That’s perfectly normal.

One-to-Many Relationships

The most common relationship in databases. One record in Table A can relate to many records in Table B.

  • One customer can have many orders
  • One department can have many employees
  • One author can write many books

The “one” side has the primary key. The “many” side has the foreign key.

We explored the customers and orders tables above. They had a one-to-many relationship.

One-to-One Relationships

Less common. Each record in Table A relates to exactly one record in Table B, and vice versa.

  • One employee has one employee profile
  • One user has one user settings record

You might wonder: why not put all that data in one table? Sometimes tables are split for performance (rarely accessed data in a separate table) or for organizational clarity.

Many-to-Many Relationships

When records on both sides can relate to multiple records on the other side.

  • Students can enroll in many courses; courses have many students
  • Products can be in many orders; orders contain many products

Databases can’t directly represent many-to-many. Instead, we use a junction table (also called a linking table or bridge table).

Junction Tables

Consider students and courses. We need a third table:

SQL
-- students: id, name
-- courses: id, course_name
-- enrollments: student_id, course_id ← Junction table

The enrollments table has two foreign keys. Each row represents one student enrolled in one course. This breaks the many-to-many into two one-to-many relationships.

Spotting junction tables

When you see a table with mostly foreign keys (like order_items or enrollments), it’s probably a junction table for a many-to-many relationship.

Identifying Relationships in Schema

When you encounter a new database, look for:

  • Columns ending in _id (foreign keys)
  • Tables with names combining two other tables (order_items, student_courses)
  • Column names that match other table names

Here are two tables from an Airbnb dataset. Preview both tables below and figure out: What columns could you use to match hosts with guests? Is this a one-to-many, one-to-one, or many-to-many relationship?

Table: airbnb_hosts
host_idnationalitygenderage
0USAM28
1USAF29
2ChinaF31
3ChinaM24
4MaliM30
Table: airbnb_guests
guest_idnationalitygenderage
0MaliM21
1ChinaF23
2MaliF27
3AustraliaF24
4LuxembourgM19

Exploring Related Tables

1
Count Orders per Customer

Count how many orders each customer has placed. This shows the one-to-many relationship.

Tables: customers, orders

Both tables have nationality and gender columns. A single host can match many guests (and vice versa) based on these shared attributes — that’s a many-to-many pattern. In the next few lessons, you’ll learn how to actually connect tables like these using JOINs.

Key Takeaways

  • Primary keys uniquely identify each row in a table
  • Foreign keys reference primary keys in other tables, creating relationships
  • One-to-many is the most common relationship (one customer, many orders)
  • Many-to-many requires a junction table with two foreign keys
  • Look for _id columns and combined table names to identify relationships

What’s Next

Now that you understand how tables relate, it’s time to connect them. In the next lesson, you’ll learn UNION — a way to stack results from multiple queries together.