Inner Join and Intersect: Bridging Data in SQL

Exploring INNER JOIN and INTERSECT in SQL

Taranjit Kaur
Plumbers Of Data Science
3 min readOct 27, 2023

--

SQL, which stands for Structured Query Language, is like the Swiss Army knife of data management and analysis.

Among its many tricks, two of the most frequently used are INNER JOIN and INTERSECT. These are the magicians of the SQL world. In this article, we’ll go on a journey to demystify INNER JOIN and INTERSECT and learn how to wield their power to tackle various data puzzles.

Inner Join

INNER JOIN: The Matchmaker of Tables

INNER JOIN is like the matchmaker of SQL, connecting data from two or more tables based on shared attributes. It’s the bridge that brings together rows from different tables, but only those that meet a specific condition. This condition is typically established using a common field or column, such as an ID or a foreign key.

Imagine you have two tables, “Customers” and “Orders.” “Customers” is your address book, storing customer information like names, contact details, and unique customer IDs. On the other hand, “Orders” is your ledger, recording every purchase, with details like customer IDs, order IDs, and order dates. To find out what each customer ordered, you’d use an INNER JOIN to unite these tables using the common “CustomerID” field.

SELECT Customers.CustomerName, Orders.OrderDate
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;

This SQL query performs the INNER JOIN by matching “Customers” and “Orders” through the “CustomerID” column. The outcome is a new table that blends customer names with their corresponding order dates, offering a unified snapshot of customer information and order history.

Intersect

INTERSECT: Where Common Grounds Are Found

Now, let’s switch gears to INTERSECT, which specializes in finding common ground among the results of two or more SELECT statements. It’s like a Venn diagram, showing only the overlapping portions.

Imagine you have two tables, “Employees” and “Managers.” “Employees” holds details about every employee in your company, including their IDs, names, and job titles. Meanwhile, “Managers” is your list of managers, complete with their IDs and names. To identify employees who are also managers, you can use INTERSECT to uncover individuals whose IDs appear in both tables.

SELECT EmployeeName
FROM Employees
INTERSECT
SELECT ManagerName
FROM Managers;

In this SQL query, INTERSECT acts as a detective, spotting the common names between the “Employees” and “Managers” tables. The result? A list of employees who are also managers within the organization.

Key Differences:

  1. INNER JOIN blends data from multiple tables, creating one comprehensive result. In contrast, INTERSECT focuses on common rows between SELECT statements.
  2. INNER JOIN relies on a shared column or field to connect tables, while INTERSECT works based on the structure of SELECT statements.
  3. INNER JOIN usually generates a larger dataset, weaving together information from different sources, while INTERSECT produces a smaller dataset featuring only shared elements.

Conclusion

INNER JOIN and INTERSECT are indispensable tools in your SQL toolbox for manipulating and extracting data. INNER JOIN is the glue that binds data from diverse tables, allowing you to see the big picture by linking related columns. Meanwhile, INTERSECT excels at finding common threads among results from different SELECT statements, illuminating shared data points.

Mastery of these operations is fundamental for anyone working with relational databases. Whether you’re unraveling customer order histories or identifying employees with dual roles, these SQL techniques will be your guiding light on your data-driven journey.

--

--