Comparative Analysis of Join Types Join: ID Name
Comparative Analysis of Join Types Join: ID Name
Join
A JOIN clause is a BINARY operation used to combine rows from two or more DBMS tables, based on a
related column between them. A JOIN is a means for combining fields from two tables by using values
common to each. The tables in DBMS are associated using primary and foreign Keys.
Types of Join
Cross Join
Natural Join
Inner Join
Outer Join
Left Join
Right Join
Cross Join
The CROSS JOIN is used to generate a paired combination of each row of the first table with each row of
the second table. This join type is also known as CARTESION join. ... The main idea of the CROSS JOIN is
that it returns the Cartesian product of the joined tables.
Syntax: SELECT *
FROM table1
Student
ID Name
1 Usman
2 Ahmed
3 Ali
Student_info
ID City
1 Lahore
2 Multan
4 Karachi
Cross join query will be :
SELECT *
FROM Student
The result table returns join product of all records present in both tables
ID NAME ID ADDRESS
1 Usman 1 Lahore
2 Ahmed 1 Lahore
4 Ali 1 Lahore
1 Usman 2 Multan
2 Ahmed 2 Multan
4 Ali 2 Multan
1 Usman 3 Karachi
2 Ahmed 3 Karachi
4 Ali 3 Karachi
……………………………………………………………………………………………………….
Inner Join:
The INNER JOIN keyword selects records that have matching values in both tables.
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
WHERE table1.column_name = table2.column_name;
Example:
ID Name
1 Usman
2 Ahmed
3 Ali
4 Saleh
Student_info
ID City
1 Lahore
2 Multan
3 Karachi
Inner Join Query will be
ID NAME ID CITY
1 USMAN 1 LAHORE
2 AHMED 2 MULTAN
3 ALI 3 KARACHI
……………………………………………………………………………………………………….
NATURAL JOIN:
The SQL NATURAL JOIN is a type of EQUI JOIN and is structured in such a way that, columns with the
same name of associated tables will appear once only.
- The associated tables have one or more pairs of identically named columns.
- The columns must be the same data type.
- Don’t use ON clause in a natural join.
SYNTAX:
SELECT * FROM
Example
ID Name
1 Usman
2 Ahmed
3 Ali
4 Saleh
Student_info
ID City
1 Lahore
2 Multan
3 Karachi
……………………………………………………………………………………………………….
OUTER JOIN:
The SQL OUTER JOIN returns all rows from both the participating tables which satisfy the join condition
along with rows which do not satisfy the join condition. The SQL OUTER JOIN operator (+) is used only on
one side of the join condition only.
The SQL LEFT JOIN (specified with the keywords LEFT JOIN and ON) joins two tables and fetches all
matching rows of two tables for which the SQL-expression is true, plus rows from the frist table that do
not match any row in the second table.
Syntax:
SELECT *
FROM table1
ON table1.column_name=table2.column_name;
EXAMPLE:
ID Name
1 Usman
2 Ahmed
3 Ali
4 Saleh
5 Faheem
Student_info table
ID City
1 Lahore
2 Multan
3 Karachi
9 Tolamba
10 Qasba
ID NAME ID CITY
1 Usman 1 Lahore
2 Ahmed 2 Multan
3 Ali 3 Karachi
4 Saleh - -
5 Faheem - -
……………………………………………………………………………………………………….
The SQL RIGHT JOIN, joins two tables and fetches rows based on a condition, which is matching in both
the tables ( before and after the JOIN clause mentioned in the syntax below) , and the unmatched rows
will also be available from the table written after the JOIN clause .
Syntax:
ON table-name1.column-name = table-name2.column-name;
EXAMPLE:
ID Name
1 Usman
2 Ahmed
3 Ali
4 Saleh
5 Faheem
Student_info table
ID City
1 Lahore
2 Multan
3 Karachi
9 Tolamba
10 Qasba
ID NAME ID CITY
1 Usman 1 Lahore
2 Ahmed 2 Multan
3 Ali 3 Karachi
- - 9 Tolamba
- - 10 Qasba
……………………………………………………………………………………………………….
FULL OUTER JOIN:
In SQL the FULL OUTER JOIN combines the results of both left and right outer joins and returns all
(matched or unmatched) rows from the tables on both sides of the join clause.
Syntax:
ON table-name1.column-name = table-name2.column-name
EXAMPLE:
ID Name
1 Usman
2 Ahmed
3 Ali
4 Saleh
5 Faheem
Student_info table
ID City
1 Lahore
2 Multan
3 Karachi
9 Tolamba
10 Qasba
ID NAME ID ADDRESS
1 Usman 1 Lahore
2 Ahmed 2 Multan
3 Ali 3 Karachi
4 Saleh - -
5 Faheem - -
- - 9 Tolamba
- - 10 Qasba
……………………………………………………………………………………………………….