Joining Three or More Tables in SQL
Joining multiple tables in SQL is some tricky task. It can be more difficult if you need to join more than two tables in single SQL query, we will analyze how to retrieve data from multiple tables using INNER JOINs. In this section, we have used two approaches to join three or more tables in SQL.
We are creating three tables, as follows:
Table 1: student
In the above table s_id is the primary key.
Table 2: marks
In the above table, school_id is primary key and s_id is the foreign key.
Table 3: details
In the above table, school_id is the foreign key.
There are two approaches to join three or more tables in SQL:
1. Using JOINS in SQL:
The same logic is applied here which is used to join two tables i.e., the minimum number of join statements to join n tables are (n-1).
2. Using the Parent-child Relationship:
In the parent-child relationship, we use where clause to join two or more tables. Create column X as a primary key in one table and a foreign key in another table
Look at the tables which are created: