SQL Join
SQL Join
SQL Join is used to fetch data from two or more tables, which is joined to appear as
single set of data. It is used for combining column from two or more tables by using
values common to both tables.
JOIN Keyword is used in SQL queries for joining two or more tables. Minimum
required condition for joining table, is (n-1) where n, is number of tables. A table
can also join to itself, which is known as, Self Join.
Types of JOIN
Following are the types of JOIN that we can use in SQL:
Inner
Outer
Left
Right
FROM
1 abhi
2 Adam
4 Alex
ID NAME ID Address
1 Abhi 1 DELHI
2 Adam 1 DELHI
4 Alex 1 DELHI
1 Abhi 2 MUMBAI
2 Adam 2 MUMBAI
4 Alex 2 MUMBAI
1 Abhi 3 CHENNAI
2 Adam 3 CHENNAI
4 Alex 3 CHENNAI
and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
SELECT * FROM
ID NAME
1 abhi
2 adam
3 alex
4 anu
and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
ID NAME ID Address
1 Abhi 1 DELHI
2 Adam 2 MUMBAI
3 Alex 3 CHENNAI
Natural JOIN
Natural Join is a type of Inner join which is based on column having same name and
same datatype present in both the tables to be joined.
The syntax for Natural Join is,
SELECT * FROM
ID NAME
1 abhi
2 adam
3 alex
4 anu
and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
ID NAME Address
1 Abhi DELHI
2 Adam MUMBAI
3 Alex CHENNAI
In the above example, both the tables being joined have ID column(same name and
same datatype), hence the records for which value of ID matches in both the tables will
be the result of Natural Join of these two tables.
OUTER JOIN
Outer Join is based on both matched and unmatched data. Outer Joins subdivide
further into,
ON table-name1.column-name = table-name2.column-name;
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
ID NAME ID Address
1 Abhi 1 DELHI
2 Adam 2 MUMBAI
3 Alex 3 CHENNAI
ON table-name1.column-name = table-name2.column-name;
table-name1, table-name2
ON table-name1.column-name(+) = table-name2.column-name;
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 ashish
and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
ID NAME ID Address
1 Abhi 1 DELHI
2 Adam 2 MUMBAI
3 Alex 3 CHENNAI
ON table-name1.column-name = table-name2.column-name;
ID NAME
1 abhi
2 adam
3 alex
4 anu
5 Ashish
and the class_info table,
ID Address
1 DELHI
2 MUMBAI
3 CHENNAI
7 NOIDA
8 PANIPAT
ID NAME ID Address
1 Abhi 1 DELHI
2 Adam 2 MUMBAI
3 Alex 3 CHENNAI
DBMS - Joins
Advertisements
Previous Page
Next Page
We understand the benefits of taking a Cartesian product of two relations, which gives
us all the possible tuples that are paired together. But it might not be feasible for us in
certain cases to take a Cartesian product where we encounter huge relations with
thousands of tuples having a considerable large number of attributes.
Join is a combination of a Cartesian product followed by a selection process. A Join
operation pairs two tuples from different relations, if and only if a given join condition is
satisfied.
We will briefly describe various join types in the following sections.
Notation
R1 ⋈θ R2
R1 and R2 are relations having attributes (A1, A2, .., An) and (B1, B2,.. ,Bn) such that
the attributes don’t have anything in common, that is R1 ∩ R2 = Φ.
Theta join can use all kinds of comparison operators.
Student
101 Alex 10
102 Maria 11
Subjects
Class Subject
10 Math
10 English
11 Music
11 Sports
Student_Detail −
STUDENT ⋈Student.Std = Subject.Class SUBJECT
Student_detail
Equijoin
When Theta join uses only equality comparison operator, it is said to be equijoin. The
above example corresponds to equijoin.
Courses
CS01 Database CS
ME01 Mechanics ME
EE01 Electronics EE
HoD
Dept Head
CS Alex
ME Maya
EE Mira
Courses ⋈ HoD
Outer Joins
Theta Join, Equijoin, and Natural Join are called inner joins. An inner join includes only
those tuples with matching attributes and the rest are discarded in the resulting
relation. Therefore, we need to use outer joins to include all the tuples from the
participating relations in the resulting relation. There are three kinds of outer joins − left
outer join, right outer join, and full outer join.
Left
A B
100 Database
101 Mechanics
102 Electronics
Right
A B
100 Alex
102 Maya
104 Mira
Courses HoD
A B C D
A B C D
Courses HoD
A B C D
Table 2: marks
create table marks(school_id int primary key, s_id int,
score int, status varchar(20));
Table 3: details
create table details(address_city varchar(20), email_ID varchar(20),
school_id int, accomplishments varchar(50));
2. Using parent-child relationship:
This is rather an interesting approach. Create column X as primary key in one table and
as foreign key in another table (i.e creating a parent-child relationship).
Let’s look in the tables created:
s_id is the primary key in student table and is foreign key in marks table. (student
(parent) – marks(child)).
school_id is the primary key in marks table and foreign key in details
table. (marks(parent) – details(child)).
Query:
select s_name, score, status, address_city,
email_id, accomplishments from student s,
marks m, details d where s.s_id = m.s_id and
m.school_id = d.school_id;
Output: