Connecting Tables: JOINs and More Essential SQL

The real power of a relational database emerges when data is split across multiple related tables instead of being duplicated everywhere. This article explains why splitting data across tables avoids redundancy, covers the foreign key relationship that connects tables together, walks through the different types of JOIN used to query across related tables, and introduces a few more SQL techniques for managing table structure and data safely.

SQL JOINForeign KeyRelated Tables

~5 min read · Updated Sep 7, 2026

Why Data Gets Split Across Multiple Tables

Storing every piece of related information in a single table quickly leads to repeated data. Consider storing each customer's order history directly inside the customer table: every new order would require repeating that customer's name and email again, wasting space and creating a risk that the same customer's name is spelled differently across different rows.

Problematic single-table design:
| customer_name | email          | order_id | order_date |
|----------------|----------------|----------|------------|
| Alice Smith    | [email protected]   | 101      | 2026-01-05 |
| Alice Smith    | [email protected]   | 102      | 2026-01-12 |

Alice's name and email are duplicated for every order

Splitting this into two related tables — one for customers, one for orders — stores each customer's details exactly once, and each order simply references which customer placed it.

Foreign Keys: The Glue Connecting Tables

A Foreign Key is a column in one table that references the Primary Key, discussed earlier in this series, of another table, establishing a link between a row in one table and a row in another.

CREATE TABLE orders (
    order_id INTEGER PRIMARY KEY,
    customer_id INTEGER,
    order_date DATE,
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);

This foreign key constraint does more than just document the relationship; the database actively enforces it, refusing to insert an order that references a customer_id that does not actually exist in the customers table — a safeguard called Referential Integrity that prevents orphaned or inconsistent data.

Retrieving Data Across Tables: The JOIN Clause

Once data is split across tables, retrieving a complete picture — such as a customer's name alongside their order dates — requires combining rows from both tables using a JOIN.

INNER JOIN: Only Matching Rows

SELECT customers.name, orders.order_date
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;

An INNER JOIN returns only rows where a match exists in both tables — a customer with no orders would not appear at all in this result, since there is no matching row in the orders table to pair it with.

LEFT JOIN: Keep Every Row from One Side

SELECT customers.name, orders.order_date
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;

A LEFT JOIN returns every row from the left table (customers) regardless of whether a match exists in the right table (orders), filling in NULL for the order columns when no matching order exists. This is the right choice whenever the goal is to see every customer, including those who have never placed an order.

Choosing Between JOIN Types

INNER JOIN: only rows with a match on both sides
LEFT JOIN:  all rows from the left table, matched
            data from the right where available
RIGHT JOIN: the mirror image of LEFT JOIN
FULL JOIN:  all rows from both tables, matched
            where possible, NULL where not

Combining JOIN with Aggregation

JOINs are frequently combined with the GROUP BY and aggregate functions discussed earlier in this series to answer questions spanning multiple tables.

SELECT customers.name, COUNT(orders.order_id) AS order_count
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id
GROUP BY customers.name;

This query counts how many orders each customer has placed, correctly showing a count of zero for customers who have never ordered, thanks to the LEFT JOIN preserving their row even without a match.

Revisiting Table and Data Management

Beyond the basic CREATE TABLE covered earlier in this series, real database work often requires modifying a table's structure after it already contains data, using ALTER TABLE.

-- Add a new column to an existing table
ALTER TABLE customers ADD COLUMN phone VARCHAR(20);

-- Remove a column no longer needed
ALTER TABLE customers DROP COLUMN phone;

When deleting data that other tables depend on through foreign keys, the database's referential integrity enforcement, discussed earlier in this article, will block the deletion by default unless the dependent rows are removed first, or unless the foreign key is explicitly configured with a cascading behavior that defines what should happen to related rows automatically.

Why Mastering JOINs Is a Turning Point

Understanding how to properly split data across related tables and reliably reconnect it using JOINs is the single most important skill separating basic SQL usage from genuine relational database competency. Nearly every real-world database contains dozens of interconnected tables, and the ability to correctly combine data from several of them at once, without accidentally duplicating or losing rows, is essential for writing correct queries and, more importantly, for designing well-structured databases in the first place — the deeper design principles covered throughout the rest of this series.

Written & researched by Dr. Shahin Siami

Related Articles

An Overview of Database Design: Goals, Process, and Key Phases

Writing SQL queries is only half the picture; designing a database well before writing any queries at all determines whether that database will remain reliable, efficient, and maintainable as an application grows. This article explains the core goals every database design should pursue, walks through the overall design process from requirements to implementation, and introduces the key phases every well-designed database passes through.

Continue

Getting Started with Relational Databases and SQL

Relational databases organize data into structured tables that can be queried, updated, and managed using SQL, a language designed specifically for working with structured data. This article introduces what a relational database actually is, walks through writing a first SQL query, covers the basic query clauses every database user relies on, and explains the fundamentals of creating and managing tables and their data.

Continue

Set Theory in Relational Databases

Set Theory is the mathematical foundation of the relational database model. Concepts such as Union, Intersection, Difference, and Cartesian Product are directly implemented in SQL. Understanding these concepts helps database engineers write more logical, efficient, and powerful queries. This article explains the relationship between Set Theory and relational databases, the main operations, and practical SQL examples.

Continue

What Are Relational Databases? A Complete Guide to Relational Database Systems

Relational databases are one of the most widely used types of databases that store data in structured tables with defined relationships. By using primary keys, foreign keys, and the SQL language, relational database systems provide reliable, consistent, and efficient data management for modern applications.

Continue