SQL JOINS:
We can combine rows from two or more tables using JOIN. There are basically four types of JOINS.
- INNER JOIN
- LEFT JOIN
- RIGHT JOIN
- FULL JOIN
INNER JOIN:
Suppose we have two tables named student, student_info, and student_personal.
INNER JOIN |
The above three tables have a common field that is ID. We want the id, name, balance, and home_district of students from the tables above displayed. We just want the records which are common to the tables on the basis of ID. In the above tables, some ids are similar and the rest are dissimilar. INNER JOIN is used to combine the similar rows of two or more tables.
SYNTAX: SELECT table1.column_name, table1.column_name, table2.column_name, table2.column_name, table3.column_name, table3.column_name
FROM table1
INNER JOIN table2 ON table1.column_name = table2.column_name
INNER JOIN table3 ON table1.column_name = table3.column_name;
SQL CODE: SELECT student.id, student.name, student_personal.home_dist, student_info.balance
FROM student INNER JOIN student_info ON student.id = student_info.id
FROM student INNER JOIN student_info ON student.id = student_info.id
INNER JOIN student_personal ON student.id= student_personal.id;
As id 1001, 1003, 1004 are common to both tables so the required information of these ids is displayed for INNER JOIN.
The common field of the three tables |
Post a Comment