Home/Curriculum/Lesson 12
Intermediate3 min read

Querying Multiple Tables with INNER JOIN

Understanding Relational Data

Relational databases deliberately distribute data across multiple specialized tables to eliminate redundancy and maintain data integrity. While this structure is highly efficient for storage, answering practical business questions often requires you to combine this fragmented data into a single, comprehensive result set. The SQL operation used to link these independent tables together during a query is called a join.

The INNER JOIN

The most fundamental and frequently used join operation is the inner join. An inner join evaluates two tables and returns only the rows that have matching values in the specified columns of both tables. If a row in the first table does not have a corresponding match in the second table, that row is entirely excluded from the final output.

Syntax and the ON Clause

Modern SQL utilizes the INNER JOIN keyword within the FROM clause to bring tables together. To instruct the database engine on exactly how the tables relate to one another, you must provide a specific join condition using the ON clause. This condition typically equates a primary key column in one table to a foreign key column in the other.

POSTGRESQL CODE SNIPPET
SELECT product_name, category_name
FROM products
INNER JOIN categories
ON products.category_id = categories.category_id;

In standard SQL syntax, if you omit the word INNER and simply write JOIN, the database system will automatically default to performing an inner join.

Qualifying Column Names

When querying multiple tables, it is common for both tables to share a column with the exact same name, such as an id or category_id column. If you request this shared column in your SELECT list without specifying which table it belongs to, the database engine will not know which version to retrieve and will return an ambiguity error. To resolve this, you must explicitly qualify the column name by prefixing it with the table name and a period.

POSTGRESQL CODE SNIPPET
SELECT products.product_name, categories.category_id
FROM products
INNER JOIN categories
ON products.category_id = categories.category_id;

Using Table Aliases

Typing full table names repeatedly for column qualification can make your queries verbose and difficult to read. To simplify your code, you can assign temporary table aliases within the FROM clause using the AS keyword.

POSTGRESQL CODE SNIPPET
SELECT p.product_name, c.category_id, c.category_name
FROM products AS p
INNER JOIN categories AS c
ON p.category_id = c.category_id;

Once you define a table alias in the FROM clause, you must use that specific alias throughout the rest of the query. The original table name is no longer recognized by the database engine for that specific execution.

Joining More Than Two Tables

Relational models frequently require joining three or more tables to assemble the required data for a report. SQL allows you to string multiple INNER JOIN clauses together in a single query. The database engine logically joins the first two tables, and then joins that intermediate result set to the third table based on the next condition.

POSTGRESQL CODE SNIPPET
SELECT c.customer_name, o.order_date, p.product_name
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
INNER JOIN products AS p
ON o.product_id = p.product_id;

Combining Joins with Filters

A join operation simply merges the tables based on your structural condition. You can still apply all standard filtering and sorting clauses to the resulting dataset. The WHERE clause evaluates the joined rows, and the ORDER BY clause sorts the final output.

POSTGRESQL CODE SNIPPET
SELECT c.customer_name, o.order_date, o.total_amount
FROM customers AS c
INNER JOIN orders AS o
ON c.customer_id = o.customer_id
WHERE o.total_amount >= 100.00
ORDER BY o.order_date DESC;

Apply these inner join techniques to successfully reconnect distributed data across multiple tables within your database.

Advertisement