0% found this document useful (0 votes)
53 views8 pages

Comparative Analysis of Join Types Join: ID Name

The document discusses different types of joins in SQL including: 1) Cross join, inner join, natural join which return rows that match the join condition between two tables. 2) Outer joins (left, right, full) which return all rows that match the condition along with unmatched rows from one or both tables. 3) Examples are provided to illustrate the syntax and results of each join type.

Uploaded by

M Saleh Butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views8 pages

Comparative Analysis of Join Types Join: ID Name

The document discusses different types of joins in SQL including: 1) Cross join, inner join, natural join which return rows that match the join condition between two tables. 2) Outer joins (left, right, full) which return all rows that match the condition along with unmatched rows from one or both tables. 3) Examples are provided to illustrate the syntax and results of each join type.

Uploaded by

M Saleh Butt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

COMPARATIVE ANALYSIS OF JOIN TYPES

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

Some of the majorly use joins are given below

 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

CROSS JOIN table2;

Example: Here is a table of student entity and student_info table

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

CROSS JOIN Student_info;

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:

The Student table

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

SELECT * from student INNER JOIN student_info where class.id = class_info.id;

Which results in the following table

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.

Natural Join: Guidelines

- 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

table-name1 NATURAL JOIN table-name2;

Example

The Student table

ID Name
1 Usman
2 Ahmed
3 Ali
4 Saleh
Student_info

ID City
1 Lahore
2 Multan
3 Karachi

Natural join query will be,

SELECT * from student NATURAL JOIN student_info;

The result set table will look like


ID NAME CITY
1 USMAN LAHORE
2 AHMED MULTAN
3 ALI 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 subtypes of SQL OUTER JOIN

 LEFT OUTER JOIN or LEFT JOIN

 RIGHT OUTER JOIN or RIGHT JOIN

 FULL OUTER JOIN

LEFT OUTER JOIN:

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

LEFT [OUTER] JOIN table2

ON table1.column_name=table2.column_name;

EXAMPLE:

The Student table

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

Left Outer Join query will be,

SELECT * FROM student LEFT OUTER JOIN student_info ON (class.id = class_info.id);

The result set table will look like

ID NAME ID CITY
1 Usman 1 Lahore
2 Ahmed 2 Multan
3 Ali 3 Karachi
4 Saleh - -
5 Faheem - -

……………………………………………………………………………………………………….

RIGHT OUTER JOIN:

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:

SELECT column-name-list FROM

table-name1 RIGHT OUTER JOIN table-name2

ON table-name1.column-name = table-name2.column-name;
EXAMPLE:

The Student table

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

Right Outer Join query will be,

SELECT * FROM student RIGHT OUTER JOIN student_info ON (class.id = class_info.id);

The result set table will look like

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:

SELECT column-name-list FROM

table-name1 FULL OUTER JOIN table-name2

ON table-name1.column-name = table-name2.column-name

EXAMPLE:

The Student table

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

Full Outer Join query will be,

SELECT * FROM student FULL OUTER JOIN student_info ON (class.id = class_info.id);

The result set table will look like

ID NAME ID ADDRESS
1 Usman 1 Lahore
2 Ahmed 2 Multan
3 Ali 3 Karachi
4 Saleh - -
5 Faheem - -
- - 9 Tolamba
- - 10 Qasba

……………………………………………………………………………………………………….

You might also like