We have previously discussed how to perform operations with a single table. But what if you need data from two or more tables? In this section, we will assemble data in multiple tables using joins and unions.
Connecting Tables Using JOIN
In the previous chapter, we discussed how to query data from a table. However, most of the time, the data you are interested in is spread across multiple tables. Fortunately, SQL has methods for bringing related tables together using the JOIN
keyword.
To illustrate, let's take a look at two tables in our database—dealerships
and salespeople
.
Figure 2.1: Dealerships table structure
Figure 2.2: Salespeople table structure
In the salespeople
table, we observe that we have a column called dealership_id
. This dealership_id
column is a direct reference to the dealership_id
column in the dealerships
table. When table A has a column that references the primary key of...