A Guide to Using Inner Joins in SQL to Group Data from Multiple Tables

Use SQL Inner Joins to combine data from three or more tables

businessman using computer in office
Blend Images - Inti St Clair/Brand X Pictures/Getty Images

You can use SQL JOIN statements to combine data from three or more tables. SQL JOIN is extremely flexible, and its powerful functionality can be used to combine data from multiple tables. Let's take a look at the SQL statements that allow you to combine results from three different tables using an inner join.

Inner Join Example

For example, take tables that contain drivers in one table and vehicle match-ups in the second. The inner join occurs where both the vehicle and driver are located in the same city.  The inner join selects all the rows from both tables that contain a match between location columns.

The SQL statement below combines data from the Drivers and Vehicles tables in cases where the driver and vehicle are located in the same city:

 SELECT lastname, firstname, tag
FROM drivers, vehicles
WHERE drivers.location = vehicles.location

This query produces the following results:

 lastname firstname tag
-------- --------- ---
Baker Roland H122JM
Smythe Michael D824HA
Smythe Michael P091YF
Jacobs Abraham J291QR
Jacobs Abraham L990MT

Now, extend this example to include a third table. Imagine that you wanted to include only drivers and vehicles present at locations that are open on the weekend. You could bring a third table into your query by extending the JOIN statement as follows:

 SELECT lastname, firstname, tag, open_weekends
FROM drivers, vehicles, locations
WHERE drivers.location = vehicles.location
AND vehicles.location = locations.location
AND locations.open_weekends = 'Yes'
 lastname firstname tag open_weekends
-------- --------- --- -------------
Baker Roland H122JM yes
Jacobs Abraham J291QR yes
Jacobs Abraham L990MT yes

This powerful extension to the basic SQL JOIN statement allows you to combine data in a complex manner. In addition to combining tables with an inner join, you can also use this technique to combine multiple tables using an outer join. Outer joins include results that exist in one table but do not have a corresponding match in the joined table.