When working with tables in SQL, there may be some situations when you need to query three or more tables. You can join three tables by first using a join statement to join two tables to create a temporary joined table. Then use a second join statement to join the third table.

Steps

  1. 1
    Type SELECT followed by the column names you want to query. Type the column name from each of the three you want to query. Separate each column name with a comma. In this example, we will be querying from three tables named "Students", "Schools", "Details"
    • For example SELECT student_id, student_name, school_id, school, grade
  2. 2
    Type FROM followed by the first table name. This can go on a separate line or immediately after the first statement. In our example we would type FROM Students.
    Advertisement
  3. 3
    Type a join statement followed by the name of the second table. There are four types of join statements you can use to join two tables. They are as follows:[1] [2]
    • Type JOIN to do an Inner Join. This returns records that have matching values in both tables. For example FROM Students JOIN Details.
    • Type LEFT JOIN to do an Outer Left Join. This returns all records from the left table and matching values from the right table. For example FROM Students LEFT JOIN Details.
    • Type RIGHT JOIN to do a Outer Right Join. This returns all records from the right table and matching values from the left table. For example FROM Students RIGHT JOIN Details.
    • Type FULL JOIN to do a Full Outer Join. This returns all records from both tables. For example FROM Students FULL JOIN Details.
  4. 4
    Type an "ON" statement for the tables and columns are being joined. The syntax for this statement is "ON table_1.primary_key = table_2.foreign_key". "Table_1" is the name of the first table you are joining, and "primary_key" is primary column name in the first table. "Table_2" is the name of the second table, and "foreign_key" is a column name from the second table that matches the primary column from the first table.
    • In our example, "Students" is the first table and "student_id" is the primary key from the Students table, which is also in the Details table. So we would type ON Students.student_id = Details.student_id. This joins the Students table with the Details table using "student_id" as the primary key.
    • Alternatively, if the student_name column is in the "Details" table, you can display the student_name column in place of the student_id field by typing ON Students.student_id = Details.student_name.
  5. 5
    Type a join statement followed by the name of the third table. This can be on a separate line or immediately after the "ON" statement that joined the first two tables. You can use any one of the four join statements.
    • In our example, we would type JOIN Schools.
  6. 6
    Type an "ON" statement that indicates which tables and columns will be joined. The syntax for the third join is "ON table_3.primary_key = table_1.foreign_key". "Table_1". "Table_3 is the name of the third table. This adds table three to the final join using the primary column name from the third table and the foreign key from the first table. In our example, this we would type ON Schools.student_id = Students.student_id.[3] The entire join statement should look something like this:
      SELECT student_id, student_name, school_id, school, grade
      FROM Students FULL JOIN Details
      ON Students.student_id = Details.student_id JOIN Schools
      ON Schools.student_id = Students.student_id
      
  7. Advertisement

About This Article

Travis Boylls
Written by:
wikiHow Technology Writer
This article was co-authored by wikiHow staff writer, Travis Boylls. Travis Boylls is a Technology Writer and Editor for wikiHow. Travis has experience writing technology-related articles, providing software customer service, and in graphic design. He specializes in Windows, macOS, Android, iOS, and Linux platforms. He studied graphic design at Pikes Peak Community College. This article has been viewed 14,021 times.
How helpful is this?
Co-authors: 6
Updated: September 21, 2022
Views: 14,021
Article SummaryX

1. Type SELECT followed by the column names you want to query.
2. Type FROM followed by the first table name.
3. Type JOIN followed by the second table name.
4. Type the following syntax: "ON table_1_name.primary_key = table_2_name.foreign_key".
5. Type JOIN followed by the third table name.
6 Type the following syntax:"ON table_3_name.primary_key = table_1_name.foreign_key".

Did this summary help you?
Advertisement