Professional Documents
Culture Documents
JOINS
A join is a SELECT statement used to retrieve the data distributed among
different tables.
In order to join the tables, they must have a common column. Common
column means the names of the columns can differ, but they must have the same
data type, size and data.
Syntax of a Join:
SELECT <ColumnList>
FROM <LeftTable> <JoinType> <RigthtTable>
ON <criteria>
Types of Joins:
Based on the way the select statements are provided and the records they
return, joins are categorized into the following ways.
1. Inner Join
2. Outer Join
Left Outer Join
Right Outer Join
Full Outer Join
2. Cross Join
3. Self Join
Inner Join:
This join returns only the matching data between the tables. It is mainly
used to retrieve the related data between the tables.
Ex:Get the details of the students along with the class names from the following
student and class tables.
Class: Student:
2 B 100 200
3 C 101 150
4 D 102 150
5 E NULL 300
Sol:
SELECT sid,sname,s.cid stdCID,c.cid clsCID,cname
FROM Student s INNER JOIN Class c
ON s.cid=c.cid
Output:
Outer Joins:
This join returns the unmatched records between the tables. Which tables
unmatched data to be displayed, depends on the type of outer join we are using.
Sometimes the unmatched data will be displayed along with matched data and
some other times the unmatched data will be displayed alone.
This join returns the unmatched data from the left table. The
corresponding values from the right table will be NULL.
Ex:
a. Get the details of all students along with classes if any student is having a
class.
Sol:
SELECT sid,sname,s.cid stdCID,c.cid clsCID,cname
FROM Student s LEFT OUTER JOIN Class c ON s.cid=c.cid
Output:
b. Get the details of students who are not assigned to any class.
Sol:
SELECT sid,sname
FROM Student s LEFT OUTER JOIN Class c ON s.cid=c.cid WHERE c.cid is null
Output:
sid sname
5 E
This join returns the unmatched data from the right table. The
corresponding values from the left table will be null.
Ex:
a. Get the details of all classes along with students.
Sol:
SELECT sid,sname,s.cid stdCID,c.cid clsCID,cname
FROM Student s RIGHT OUTER JOIN Class c ON s.cid=c.cid
Output:
Sol:
SELECT c.cid,cname
FROM Student s RIGHT OUTER JOIN Class c ON s.cid=c.cid WHERE s.cid is null
Output:
cid cname
103 BCA
Venkat Mind Q Systems
Cross Join :
A join without common column criteria is called as a cross join. This join is used
to find the different combinations of data between the tables. The output will be
the product of number of records between the tables.
Ex:Get the different possibilities how a student can be assigned to a class only
when they are having more than 200 marks
NOTE:
Self Join:
Joining a table to it self is called as a self-join. In a self-join, since we are
using the same table twice, to distinguish the two copies of the table, we will
create to table aliases.
In the syntax of self join we never use the keyword ‘self’. Internally, we
perform an inner join only.
Ex: Get the employee details along with manager names from the EMP table. In
this table one the employee will be the manager for other employee.
Sol:
Select a.empid,a.ename,a.mgrid,b.ename mgrName
From emp a inner join emp b On a.mgrid=b.empid
Output: