Merging tables with joins
SQL joins are used to combine rows from two or more tables based on a related column between them, providing a complete view of the data. We previously hinted at these related columns as primary keys and foreign keys.
As a refresher, a primary key is a column (or a combination of columns) in a database table that uniquely identifies each row in that table. A foreign key, on the other hand, is a column or a combination of columns in a table that establishes a link or a relationship to the primary key of another table.
As we dive into SQL joins, we will put our knowledge of primary and foreign keys to work!
Note
When discussing SQL joins, we will mostly focus on joining two tables to simplify the concepts. Traditionally, two joined tables are referred to as the left table and the right table.
Inner joins
INNER JOIN selects records that have matching values in both tables. Figure 5.8 best demonstrates the logic of this join type: