Javatpoint Logo
Javatpoint Logo

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.

Example:

We are creating three tables, as follows:

  1. student
  2. marks
  3. details

Table 1: student

STUDENT TABLE

Joining Three or More Tables in SQL

In the above table s_id is the primary key.

Table 2: marks

MARKS TABLE

Joining Three or More Tables in SQL

In the above table, school_id is primary key and s_id is the foreign key.

Table 3: details


Joining Three or More Tables in SQL

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).

Output:

Joining Three or More Tables in SQL

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:
s_id is the primary key in the student table and foreign key in the marks table. (student (parent) - marks(child)).
school_id is the primary key in the marks table and foreign key in the student table. (marks(parent) - details(child)).

Query:

Output:

Joining Three or More Tables in SQL
Next TopicWhat is Web SQL




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA