JOINS
JOINS
DEFINITION :-
In database, join is used to retrieve data from two or more logically related
tables based on common set of values.
These values are usually the same column name and datatype that appear in both
the participating tables in Join.
General Syntax :-
SELECT columnname(s)
FROM table1
JOIN_TYPE table2
ON table1.column = table2.column;
Types of Joins :-
There are different types of joins available in SQL. Some are :-
INNER JOIN
SELF JOIN
NATURAL JOIN
FULL JOIN
LEFT JOIN / LEFT OUTER JOIN
RIGHT JOIN / RIGHT OUTER JOIN etc.,
Venn diagram representation for joins
1.Inner join :-
It selects or retrieve only the matching records from both tables, only if
they meet the conditions specified in the “ON” clause.
Syntax :-
SELECT columnname(s) FROM table1 INNER JOIN table2
ON table1.column = table2.column;
Example, consider the two given tables, table1 “KFC” and table 2 “Subway”.
SELECT Output :
kfc.food,kfc.price,subway.quantity Food Price Quantit
FROM kfc y
INNER JOIN Subway Pizza 199 100
ON kfc.food = subway.food;
2. Left Join / Left Outer join :-
It retrieves the matching records from both tables and the rowsBurger
from table1(left 200 200
table) even if they are unmatched.
(Simply it performs Inner Join + Left table rows)
Syntax :- Sandwic 180 150
SELECT columnname(s) FROM table1 LEFT JOIN table2
ON table1.column = table2.column;
h
Command and output :- Cake 120 120
SELECT kfc.food,kfc.price,subway.quantity
FROM kfc
LEFT JOIN Subway
ON kfc.food = subway.food;
3. Right Join / Right Outer join :-
It retrieves the matching records from both tables and the rows from
table2(right table) even if they are unmatched. (Simply it performs Inner Join +
Right table rows)
Syntax :-
SELECT columnname(s) FROM table1 RIGHT JOIN table2
ON table1.column = table2.column;
4. Full Join :-
It retrieves all the records from both the tables.
Syntax :-
SELECT columnname(s) FROM table1 FULL JOIN table2
ON table1.column = table2.column;
Command and output :-