In relational databases, data is stored across multiple tables. To retrieve data efficiently and make meaningful connections between these tables, SQL provides various join operations. One of the most common and widely used join types is the INNER JOIN. It allows you to combine records from two or more tables, based on a related column that exists in both tables.
In this chapter, we will explore everything you need to know about INNER JOINS in SQL, from the basics to advanced concepts, with clear examples. By the end of this chapter, you will be able to effectively use INNER JOIN to query multiple tables and retrieve the necessary data with ease.
An INNER JOIN is a type of SQL join that retrieves only the records where there is a match in both tables involved in the query. It is used when you want to select records that have corresponding values in both tables.
When performing an INNER JOIN, SQL compares the specified columns in the two tables and returns only those rows where the condition is met (i.e., where there is a match). If no match is found between the tables, that row is not included in the result.
console.log("helloword")SELECT column1, column2, ...
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
In this syntax:
ON
clause specifies the column on which the join is based.Let’s assume you have two tables: customers
and orders
.
customers Table:
customer_id | customer_name | city |
---|---|---|
1 | Alice | New York |
2 | Bob | Chicago |
3 | Charlie | Los Angeles |
orders Table:
order_id | customer_id | order_total |
---|---|---|
101 | 1 | 250 |
102 | 2 | 150 |
103 | 1 | 300 |
104 | 4 | 450 |
SELECT customers.customer_name, orders.order_id, orders.order_total
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;
customer_name | order_id | order_total |
---|---|---|
Alice | 101 | 250 |
Alice | 103 | 300 |
Bob | 102 | 150 |
customers
and orders
tables on the customer_id
column.customer_id
is present in both tables are returned. In this case, customer_id = 4
in the orders
table has no matching record in the customers
table, so that row is not included in the result.You can join more than two tables using multiple INNER JOINS. This is useful when you have related data spread across multiple tables and need to combine it into a single result set.
Let’s introduce a third table called products
:
products Table:
product_id | product_name | price |
---|---|---|
201 | Laptop | 1000 |
202 | Phone | 500 |
203 | Tablet | 600 |
order_items Table:
order_item_id | order_id | product_id | quantity |
---|---|---|---|
301 | 101 | 201 | 2 |
302 | 102 | 202 | 1 |
303 | 103 | 203 | 3 |
304 | 103 | 201 | 1 |
SELECT customers.customer_name, orders.order_id, products.product_name, order_items.quantity
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id
INNER JOIN order_items ON orders.order_id = order_items.order_id
INNER JOIN products ON order_items.product_id = products.product_id;
customer_name | order_id | product_name | quantity |
---|---|---|---|
Alice | 101 | Laptop | 2 |
Bob | 102 | Phone | 1 |
Alice | 103 | Tablet | 3 |
Alice | 103 | Laptop | 1 |
customers
, orders
, order_items
, and products
.When writing complex queries with multiple joins, using table aliases can simplify the SQL code and make it more readable. An alias is a temporary name given to a table or column for the duration of the query.
SELECT c.customer_name, o.order_id, p.product_name, oi.quantity
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id
INNER JOIN order_items oi ON o.order_id = oi.order_id
INNER JOIN products p ON oi.product_id = p.product_id;
customer_name | order_id | product_name | quantity |
---|---|---|---|
Alice | 101 | Laptop | 2 |
Bob | 102 | Phone | 1 |
Alice | 103 | Tablet | 3 |
Alice | 103 | Laptop | 1 |
customers
, orders
, order_items
, and products
have been aliased as c
, o
, oi
, and p
, respectively.You can also add conditions to filter the results further. For example, you may want to retrieve only the orders where the order_total
exceeds a certain value.
SELECT c.customer_name, o.order_id, o.order_total
FROM customers c
INNER JOIN orders o ON c.customer_id = o.customer_id
WHERE o.order_total > 200;
customer_name | order_id | order_total |
---|---|---|
Alice | 101 | 250 |
Alice | 103 | 300 |
While INNER JOINs are straightforward to use, there are a few common mistakes to watch out for:
Forgetting to specify the join condition (using the ON
clause) will result in a Cartesian product, meaning every row from the first table is combined with every row from the second table. This is usually not what you want.
SELECT customers.customer_name, orders.order_id
FROM customers
INNER JOIN orders;
This will return a very large result set, where each customer is paired with every order. Always remember to use the ON
clause when performing joins.
When the same column exists in multiple tables (e.g., customer_id
), you need to prefix the column name with the table name (or alias) to avoid ambiguity.
INNER JOINS are essential for retrieving data from multiple related tables in a relational database. They return only the rows where there is a match between the columns in the joined tables. Whether you're combining two tables or joining several tables with additional conditions, mastering the use of INNER JOIN is crucial for efficient SQL querying. Happy coding !❤️