0% found this document useful (0 votes)
47 views

Joins

The document describes different types of joins that can be used when querying data from multiple database tables. It provides examples of each join type - cross join, natural join, self join, and equi join. To find the name of the employee who works in the HR department, a natural join would be performed between the Employee and Department tables, equalizing on the common E_no column between the tables.

Uploaded by

Pradipto
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)
47 views

Joins

The document describes different types of joins that can be used when querying data from multiple database tables. It provides examples of each join type - cross join, natural join, self join, and equi join. To find the name of the employee who works in the HR department, a natural join would be performed between the Employee and Department tables, equalizing on the common E_no column between the tables.

Uploaded by

Pradipto
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/ 4

Employee

E_n E_name Address


o
1 Ram Delhi
2 Varun Chd
3 Ravi Chd
4 Amit Delhi
5 Nitin Noida
Department

Dept_no D_name E_no


D1 HR 1
D2 IT 2
D3 Marketing 4

Find E_name of employee who is working in HR department.

When we need information from more than 1 table, there we need to use JOIN.

Join is cross product + condition i.e. cross product with select operation.

You have to find first the table name from which the output can be taken.

Type of Joins

1. Cross product join


2. natural join
3. Self Join
4. Equi Join
5. conditional join

1. Cross product join or Cross Join

E_no E_name Dept_no Address D_name E_no


1 Ram D1 Delhi HR 1
1 Ram D2 Delhi IT 2
1 Ram D3 Delhi Marketing 4
2 Varun D1 Chd HR 1
2 Varun D2 Chd IT 2
2 Varun D3 Chd Marketing 4
3 Ravi D1 Chd HR 1
3 Ravi D2 Chd IT 2
3 Ravi D3 Chd Marketing 4
4 Amit D1 Delhi HR 1
4 Amit D2 Delhi IT 2
4 Amit D3 Delhi Marketing 4
5 Nitin D1 Noida HR 1
5 Nitin D2 Noida IT 2
5 Nitin D3 Noida Marketing 4

2. Natural Join: When we want the values of common attributes.

Find the employee names who are working in some departments.


Select E_name from Employee,Department where Employee.E_no=Department.E_no; //Not
actual query, just for understanding.

Here the bold written names means cross product. So first cross product will be done:

E_no E_name Dept_no E_no


1 Ram D1 1
1 Ram D2 2
1 Ram D3 4
2 Varun D1 1
2 Varun D2 2
2 Varun D3 4
3 Ravi D1 1
3 Ravi D2 2
3 Ravi D3 4
4 Amit D1 1
4 Amit D2 2
4 Amit D3 4
5 Nitin D1 1
5 Nitin D2 2
5 Nitin D3 4

So after natural join, the result is:

E_no E_name Dept_no E_no


1 Ram D1 1
2 Varun D2 2
4 Amit D3 4

Simply, equalize the E_no in both the tables.

Select E_name from Employee natural join Department; //Actual query.

3. Self Join: When we join a table to itself, we use self JOIN.

S_id C_id Since S_id C_id Since


S1 C1 2016 S1 C1 2016
S2 C2 2017 S2 C2 2017
S1 C2 2017 S1 C2 2017

Find the student id whose is enrolled in at least two courses. (Minimum two, maximum can be any
number)

Select from Study as T1, Study as T2 where T1.S_id=T2.S_id AND T1.C_id<>T2.C_id;


S_id C_id Since S_id C_id Since
S1 C1 2016 S1 C1 2016
S1 C1 2016 S2 C2 2017
S1 C1 2016 S1 C2 2017
S2 C2 2017 S1 C1 2016
S2 C2 2017 S2 C2 2017
S2 C2 2017 S1 C2 2017
S1 C2 2017 S1 C1 2016
S1 C2 2017 S2 C2 2017
S1 C2 2017 S1 C2 2017

we don’t need Since column as it is not asked in the query. so table is

S_id C_id S_id C_id


S1 C1 S1 C1
S1 C1 S2 C2
S1 C1 S1 C2
S2 C2 S1 C1
S2 C2 S2 C2
S2 C2 S1 C2
S1 C2 S1 C1
S1 C2 S2 C2
S1 C2 S1 C2

S_id=S_id but C_id not equal to C_id

So,

S_id C_id S_id C_id


S1 C1 S1 C2
S1 C2 S1 C1

You might also like