DBMS4
DBMS4
:- 04
Objective : Displaying data from multiple tables.
Table No. 01
Table No. 02
1. Inner Join : The INNER JOIN keyword in MYSQL selects only those tuples from both tables that satisfy
the join condition. It creates the resultant set by joining all tuples from both tables where the value of the
common column is the same.
Syntax : SELECT table1.column1, table1.column2, table2.column1
FROM table1 INNER JOIN table2
ON table1.common_column = table2.common_column;
Code : select people.name, people.city, people.age, course_table.Course_ID
from people INNER JOIN course_table
on people.pe_id = course_table.pe_id;
Output :
2. Left Join : The LEFT JOIN keyword returns all records from the left table(table1), and the matching
records(if any) from the right table(table2).
Syntax : SELECT table1.column1, table1.column2, table2.column1
FROM table1 LEFT JOIN table2
ON table1.common_column = table2.common_column;
Code : select people.name, people.age, people.course, course_table.Course_ID
from people LEFT JOIN course_table
on people.pe_id = course_table.pe_id;
Output :
3. Right Join : The RIGHT JOIN keyword returns all records from the right table(table2) and the matching
records from the left table(table1). The result is record from the left side, if there is no match.
Syntax : SELECT table1.column1, table1.column2, table2.column1
FROM table1 RIGHT JOIN table2
ON table1.common_column = table2.common_column;
Code : select people.name, people.age, people.course, course_table.Course_ID
from people RIGHT JOIN course_table
on people.pe_id = course_table.pe_id;
Output :
4. Full Join : A full join combines the results of a left outer join and a right outer join. It returns all rows
from both tables, including matching and non-matching rows. If there are no matching rows for a row in
one table, the other table's columns will have NULLs for those records.
Syntax : SELECT table1.column1, table1.column2, table2.column1
FROM table1 FULL JOIN table2
ON table1.common_column = table2.common_column;
Code : select people.name, people.age, people.course, course_table.Course_ID
from people FULL JOIN course_table
on people.pe_id = course_table.pe_id;
Note : In MySQL, you can not directly use the FULL OUTER JOIN or FULL JOIN, so we use a
combination of LEFT JOIN or RIGHT JOIN and UNION operator.