Peforming INNER JOINS

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.

What is an INNER JOIN?

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.

Syntax of INNER JOIN:

				
					console.log("helloword")SELECT column1, column2, ...
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

				
			

In this syntax:

  • table1 and table2 are the tables you are joining.
  • column1, column2, … are the columns you want to retrieve.
  • The ON clause specifies the column on which the join is based.

Basic Example of INNER JOIN

Let’s assume you have two tables: customers and orders.

customers Table:

customer_idcustomer_namecity
1AliceNew York
2BobChicago
3CharlieLos Angeles

orders Table:

order_idcustomer_idorder_total
1011250
1022150
1031300
1044450

Query:

				
					SELECT customers.customer_name, orders.order_id, orders.order_total
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;

				
			

Output:

customer_nameorder_idorder_total
Alice101250
Alice103300
Bob102150

Explanation:

  • The query joins the customers and orders tables on the customer_id column.
  • Only the rows where the 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.
  • The result contains all orders placed by Alice and Bob, but excludes Charlie because he has not placed an order.

Multiple INNER JOINS

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.

Example with Three Tables:

Let’s introduce a third table called products:

products Table:

product_idproduct_nameprice
201Laptop1000
202Phone500
203Tablet600

order_items Table:

order_item_idorder_idproduct_idquantity
3011012012
3021022021
3031032033
3041032011

Query:

				
					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;

				
			

Output:

customer_nameorder_idproduct_namequantity
Alice101Laptop2
Bob102Phone1
Alice103Tablet3
Alice103Laptop1

Explanation:

  • This query performs multiple INNER JOINS to combine data from four tables: customers, orders, order_items, and products.
  • The result shows the products ordered by Alice and Bob, including the quantities.

Advanced Concepts: INNER JOIN with Aliases and Conditions

Using Table Aliases

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.

Query with Aliases:

				
					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;

				
			

Output:

customer_nameorder_idproduct_namequantity
Alice101Laptop2
Bob102Phone1
Alice103Tablet3
Alice103Laptop1

Explanation:

  • The tables customers, orders, order_items, and products have been aliased as c, o, oi, and p, respectively.
  • This makes the query more concise without changing the logic.

Adding Conditions in INNER JOIN Queries

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.

Query with Condition:

				
					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;

				
			

Output:

customer_nameorder_idorder_total
Alice101250
Alice103300

Explanation:

  • This query returns only the orders where the total amount exceeds 200. The condition is applied after the INNER JOIN, filtering the result set.

Common Mistakes with INNER JOIN

While INNER JOINs are straightforward to use, there are a few common mistakes to watch out for:

Missing or Incorrect JOIN Condition

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.

Example of a Cartesian Product:

				
					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.

Ambiguous Column Names

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 !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India