Friday, 31 July 2015

JOINS IN DATABASE MANAGEMENT

Q - WHAT IS A JOIN IN THE DATABASE ?
ans-A join is an SQL operation performed to establish a connection between two or more database tables based on matching columns, thereby creating a relationship between the tables. Most complex queries in an SQL database management system involve join commands. 

There are different types of joins. The type of join a programmer uses determines which records the query selects. Three algorithms work behind join operations: hash join, sort-merge join and nested loop join.

Q- what are the different types of join in the database ?
ans -
tAn SQL JOIN clause is used to combine rows from two or more tables, based on a common field between them.
There are many types of join.
  • Inner Join
    1. Equi-join
    2. Natural Join
    3. thita join 
  • Outer Join
    1. Left outer Join
    2. Right outer join
    3. Full outer join
  • Cross Join
  • Self Join


 INNER JOIN 

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.

Theta (θ) Join

Theta join combines tuples from different relations provided they satisfy the theta condition. The join condition is denoted by the symbol θ.

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
SIDNameStd
101Alex10
102Maria11
Subjects
ClassSubject
10Math
10English
11Music
11Sports
Student_Detail −
STUDENT Student.Std = Subject.Class SUBJECT
Student_detail
SIDNameStdClassSubject
101Alex1010Math
101Alex1010English
102Maria1111Music
102Maria1111Sports

Equijoin

When Theta join uses only equality comparison operator, it is said to be equijoin. The above example corresponds to equijoin.

Natural Join ()

Natural join does not use any comparison operator. It does not concatenate the way a Cartesian product does. We can perform a Natural Join only if there is at least one common attribute that exists between two relations. In addition, the attributes must have the same name and domain.
Natural join acts on those matching attributes where the values of attributes in both the relations are same.
Courses
CIDCourseDept
CS01DatabaseCS
ME01MechanicsME
EE01ElectronicsEE
HoD
DeptHead
CSAlex
MEMaya
EEMira
Courses ⋈ HoD
DeptCIDCourseHead
CSCS01DatabaseAlex
MEME01MechanicsMaya
EEEE01ElectronicsMira


Q-DIFFERENCE BETWEEN  NATURAL JOIN AND OTHER INNER JOINS 

 ANS -
  1. Inner join can have equality (=) and other operators (like <,>,<>) in the join condition.
  2. Equi join only have equality (=) operator in the join condition.
  3. Equi join can be an Inner join, Left Outer join, Right Outer join
  4. The USING clause is not supported by SQL Server and Sybase. This clause is supported by Oracle and MySQL.
  5. IN CASE OF  NATURAL JOIN NEW TABLE CONTAINS  REMOVE REDUNDENT COLUMNS 

Outer JOIN

Outer Join is based on both matched and unmatched data. Outer Joins subdivide further into,
  • Left Outer Join
  • Right Outer Join
  • Full Outer Join

Left Outer Join

The left outer join returns a result table with the matched data of two tables then remaining rows of the left table and null for the right table's column.
Left Outer Join syntax is,
SELECT column-name-list
from table-name1 
LEFT OUTER JOIN 
table-name2
on table-name1.column-name = table-name2.column-name;
Left outer Join Syntax for Oracle is,
select column-name-list
from table-name1, 
table-name2
on table-name1.column-name = table-name2.column-name(+);

Example of Left Outer Join

The class table,
IDNAME
1abhi
2adam
3alex
4anu
5ashish
The class_info table,
IDAddress
1DELHI
2MUMBAI
3CHENNAI
7NOIDA
8PANIPAT
Left Outer Join query will be,
SELECT * FROM class LEFT OUTER JOIN class_info ON (class.id=class_info.id);
The result table will look like,
IDNAMEIDAddress
1abhi1DELHI
2adam2MUMBAI
3alex3CHENNAI
4anunullnull
5ashishnullnull

Right Outer Join

The right outer join returns a result table with the matched data of two tables then remaining rows of the right table and null for the left table's columns.
Right Outer Join Syntax is,
select column-name-list
from table-name1 
RIGHT OUTER JOIN 
table-name2
on table-name1.column-name = table-name2.column-name;
Right outer Join Syntax for Oracle is,
select column-name-list
from table-name1, 
table-name2
on table-name1.column-name(+) = table-name2.column-name;

Example of Right Outer Join

The class table,
IDNAME
1abhi
2adam
3alex
4anu
5ashish
The class_info table,
IDAddress
1DELHI
2MUMBAI
3CHENNAI
7NOIDA
8PANIPAT
Right Outer Join query will be,
SELECT * FROM class RIGHT OUTER JOIN class_info on (class.id=class_info.id);
The result table will look like,
IDNAMEIDAddress
1abhi1DELHI
2adam2MUMBAI
3alex3CHENNAI
nullnull7NOIDA
nullnull8PANIPAT

Full Outer Join

The full outer join returns a result table with the matched data of two table then remaining rows of both left table and then the right table.
Full Outer Join Syntax is,
select column-name-list
from table-name1 
FULL OUTER JOIN 
table-name2
on table-name1.column-name = table-name2.column-name;

Example of Full outer join is,

The class table,
IDNAME
1abhi
2adam
3alex
4anu
5ashish
The class_info table,
IDAddress
1DELHI
2MUMBAI
3CHENNAI
7NOIDA
8PANIPAT
Full Outer Join query will be like,
SELECT * FROM class FULL OUTER JOIN class_info on (class.id=class_info.id);
The result table will look like,
IDNAMEIDAddress
1abhi1DELHI
2adam2MUMBAI
3alex3CHENNAI
4anunullnull
5ashishnullnull
nullnull7NOIDA
nullnull8PANIPAT

No comments:

Post a Comment