Joining Three or More Tables in SQLJoining 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:
Table 1: studentSTUDENT TABLE In the above table s_id is the primary key. Table 2: marksMARKS TABLE In the above table, school_id is primary key and s_id is the foreign key. Table 3: detailsIn 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: 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: Query: Output:
Next TopicWhat is Web SQL
|