SQL INNER JOIN

Learn via video course
FREE
View all courses
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
DBMS Course - Master the Fundamentals and Advanced Concepts
DBMS Course - Master the Fundamentals and Advanced Concepts
by Srikanth Varma
1000
5
Start Learning
Topics Covered

In the realm of database management, the ability to link various data points is essential for discovering meaningful insights. The Inner Join in SQL acts as a crucial connector, uniting tables through a shared column. This capability enables the seamless integration and examination of data from diverse origins. Whether it involves tracking sales figures, overseeing inventory levels, or scrutinizing user interactions, the Inner Join facilitates a holistic view by amalgamating pertinent data into a unified perspective.

SQL INNER JOIN

The INNER JOIN in SQL command retrieves rows from multiple tables where there is a common column linking them. It produces results only when there's at least one corresponding match found across the tables involved. If no match is found, the rows are not returned.

Syntax

The basic syntax for an INNER JOIN statement is:

Demo Database

Let's consider two tables: Customers and Orders. Here are the SQL commands to create and populate these tables.

Table 1: Customers

Here's the table:

CustomerIDCustomerNameContactNameCountry
101Alaric ElectronicsMaria AndersGermany
102Fortuna DigitalAna TrujilloMexico
103Island TradingAntonio MorenoUK
104Laughing BacchusThomas HardyCanada

Table 2: Orders

Here's the table:

OrderIDOrderNumberCustomerIDOrderDate
501ORD0011032024-01-08
502ORD0021012024-03-10
503ORD0031022024-02-20
504ORD0041042024-03-05

Example

Let's say we want to find all orders along with customer information. We would use an INNER JOIN to combine Customers and Orders based on the CustomerID that is common to both tables.

Output

Executing the above INNER JOIN query would produce a result set that looks like this:

CustomerNameOrderNumber
Alaric ElectronicsORD002
Fortuna DigitalORD003
Island TradingORD001
Laughing BacchusORD004

This output shows the matching rows from both Customers and Orders tables where the CustomerID is common in both, illustrating the concept of an INNER JOIN.

Naming the Columns

Including the table name when referencing columns in an SQL statement is a recommended practice for clarity and to avoid ambiguity, especially in queries involving multiple tables. This practice becomes crucial when dealing with columns that exist in more than one table, ensuring that SQL can accurately interpret the intended column reference.

Example

If we were to include a column existing in both tables, such as CustomerID, specifying the table name becomes essential. Without clarifying whether we're referring to Customers.CustomerID or Orders.CustomerID, SQL would return an error due to the ambiguity.

In this query, explicitly mentioning Customers.CustomerID removes any confusion about which table's CustomerID we intend to use, demonstrating the importance of naming columns with their table names in complex SQL statements.

Join Two Tables With a Matching Field

Syntax

Example

Using our Customers and Orders tables, we want to list all orders with the names of the customers who made them.

Output

CustomerNameOrderNumber
Alaric ElectronicsORD002
Fortuna DigitalORD003
Island TradingORD001
Laughing BacchusORD004

SQL INNER JOIN With MULTIPLE Tables

Syntax

Example

Assuming we have a third table Products:

Here's the table:

ProductIDProductNameCustomerID
201Laptop101
202Tablet103
203Smartphone102
204Monitor104

Now, we'll join Customers, Orders, and Products to list all orders with customer names and product names.

Output

CustomerNameOrderNumberProductName
Alaric ElectronicsORD002Laptop
Fortuna DigitalORD003Smartphone
Island TradingORD001Tablet
Laughing BacchusORD004Monitor

SQL INNER JOIN With WHERE Clause

Syntax

Example

List all orders from Canada.

Output

CustomerNameOrderNumber
Laughing BacchusORD004

SQL INNER JOIN With AS Alias

Syntax

Example

Using aliases for tables and columns.

Output

NameOrders
Alaric ElectronicsORD002
Fortuna DigitalORD003
Island TradingORD001
Laughing BacchusORD004

Conclusion

  • INNER JOIN in SQL is essential for combining data from two or more tables, based on related columns, enabling complex queries and deep data analysis.
  • INNER JOIN in SQL allows for the retrieval of rows that have matching values in both tables, making data extraction more precise and relevant for specific needs.
  • By joining multiple tables or employing conditions and aliases, INNER JOIN simplifies the process of handling complex queries, making database management more efficient.
  • Whether it's managing customer orders, product inventories, or any relational dataset, INNER JOIN in SQL offers practical solutions for real-world database management challenges.