Joins 
1. It is used to join multiple tables as one table. 
2. In SQL Server 8 to 10 tables are joined. 
3. Joints are used to join the columns of different tables. 
Types of joints: 
Inner join: 
If the table contains matched records or related records the inner joints can be used 
Ex: Equi Join, Self Join 
Outer Join: 
If the tables are not having match the records or related records then outer joins are used 
Ex: Left Outer Join, Right Outer Join, Full Outer Join 
Equi Join: 
The tables can be joined by using equals to symbol(=) 
Ex: 
Create Database PacticeDB 
GO 
Create Table DEPARTMENT 
Go 
Insert into DEPARTMENT values(DEPTNO int,DNAME varchar(15),LOC varchar(15)) 
GO 
insert DEPARTMENT values(10,”Sales”,”HYD”), 
(20,”Accounting”,”HYD”) 
GO 
Create Table EMPDET 
Go 
Insert into EMPDET values(ENO int,ENAME varchar(15),SAL varchar(MAX)) ,DEPTNO int)
GO 
insert DEPARTMENT values(1001,”Ram”,9000,10), 
(1002,”Vinod”,8000,10), 
(1003,”Ajay”,9200,20), 
(1004,”Kiran”,12000,10) 
GO 
Query: 
Select ENO,ENAME ,SAL,DEPARTMENT,DEPTNO,DNAME,LOC 
From 
DEPARTMENT,EMPDET 
Where DEPARTMENT,DEPTNO=EMPDET.DEPTNO 
NOTE: 
1. EQUI JOIN can be called as natural join. In select statement if where condition is not given then it 
is called cross join or Cartesian join 
2. It is join record in the 1st table will join with records in the 2nd table. For joining the table column 
names can be same or can be different but the values in the columns should be same. 
Self join: 
If different columns in the table contain similar value then self-join can be used. Self-join means a 
table can be joined by itself. 
QUERY: 
To get subordinate and managers list from Employee table 
SELECT D.ENO,D.ENAME,D.JOB,E.ENO,E.ENAME,E.JOB 
FROM 
EMPLOYEE D,EMPLOYEE E 
WHERE E.ENO=D.MGR 
OUTER JOINTS 
LEFTOUTER JOIN: 
It will takes all the records from the first table and will joins with the second table
QUERY(Same TABLE Of EQUI JOIN I USED HERE ALSO) 
SELECT ENO,ENAME,SAL,DEPARTMENT.DEPTNO,DNAME,LOC FROM 
DEPARTMENT.LEFTOUTERJOIN 
EMPDET ON DEPARTMENT.DEPTNO=EMPDET.DEPTNO 
Full Outer JOIN: 
1.It is the combination of left outer join and right outer join 
2.It will show all the records from the table 
Ex: 
Create Database PacticeDB 
GO 
Create Table EINFO1 
Go 
Insert into EINFO1 values(ENO int,ENAME varchar(15)) 
GO 
insert EINFO1 values(101,”AJAY”), (102,”RAM”), (103,”KIRAN”), (104,”VIKAS”), 
(105,”VIJAY”) 
Create Table EINFO2 
Go 
Insert into EINFO2 values(ENO int,SAL varchar(MAX)) 
GO 
insert EINFO2 values(101,9000), (103,12000),(105,8000) 
GO 
Create Table EINFO1 
Go 
Insert into EINFO3 values(ENO int,ENAME varchar(15)) 
GO 
insert EINFO3 values(101,”VIJAY”), (103,”RAM”), (105,”KIRAN”) 
GO
Create Table EINFO4 
Go 
Insert into EINFO4 values(ENO int,SAL varchar(MAX)) 
GO 
insert EINFO4 values(101,8000), (102,9000), (103,18000), (104,9000), 
(105,8000) 
QUERY USING FULL OUTER JOIN): 
SELECT A.ENO,A.ENAME,B.SAL 
FROM EINFO1 A FULLOUTERJOIN EINFO2 B 
ON A.ENO=B.ENO 
SELECT D.ENO,C.ENAME,D.SAL 
FROM EINFO3 C FULLOUTERJOIN EINFO4 D 
ON C.ENO=D.ENO ORDER BY D.ENO 
QUERY USING RIGHT OUTER JOIN 
SELECT EINFO4.ENO,ENAME,SAL 
FROM EINFO3 RIGHTOUTERJOIN EINFO4 
ON EINFO3.ENO=EINFO4.ENO 
QUERY USING EQUI JOIN(SAME TABLE): 
SELECT EINFO4.ENO,ENAME,SAL 
FROM EINFO3,EINFO4 
WHERE EINFO3.ENO=EINFO4.ENO

Joins

  • 1.
    Joins 1. Itis used to join multiple tables as one table. 2. In SQL Server 8 to 10 tables are joined. 3. Joints are used to join the columns of different tables. Types of joints: Inner join: If the table contains matched records or related records the inner joints can be used Ex: Equi Join, Self Join Outer Join: If the tables are not having match the records or related records then outer joins are used Ex: Left Outer Join, Right Outer Join, Full Outer Join Equi Join: The tables can be joined by using equals to symbol(=) Ex: Create Database PacticeDB GO Create Table DEPARTMENT Go Insert into DEPARTMENT values(DEPTNO int,DNAME varchar(15),LOC varchar(15)) GO insert DEPARTMENT values(10,”Sales”,”HYD”), (20,”Accounting”,”HYD”) GO Create Table EMPDET Go Insert into EMPDET values(ENO int,ENAME varchar(15),SAL varchar(MAX)) ,DEPTNO int)
  • 2.
    GO insert DEPARTMENTvalues(1001,”Ram”,9000,10), (1002,”Vinod”,8000,10), (1003,”Ajay”,9200,20), (1004,”Kiran”,12000,10) GO Query: Select ENO,ENAME ,SAL,DEPARTMENT,DEPTNO,DNAME,LOC From DEPARTMENT,EMPDET Where DEPARTMENT,DEPTNO=EMPDET.DEPTNO NOTE: 1. EQUI JOIN can be called as natural join. In select statement if where condition is not given then it is called cross join or Cartesian join 2. It is join record in the 1st table will join with records in the 2nd table. For joining the table column names can be same or can be different but the values in the columns should be same. Self join: If different columns in the table contain similar value then self-join can be used. Self-join means a table can be joined by itself. QUERY: To get subordinate and managers list from Employee table SELECT D.ENO,D.ENAME,D.JOB,E.ENO,E.ENAME,E.JOB FROM EMPLOYEE D,EMPLOYEE E WHERE E.ENO=D.MGR OUTER JOINTS LEFTOUTER JOIN: It will takes all the records from the first table and will joins with the second table
  • 3.
    QUERY(Same TABLE OfEQUI JOIN I USED HERE ALSO) SELECT ENO,ENAME,SAL,DEPARTMENT.DEPTNO,DNAME,LOC FROM DEPARTMENT.LEFTOUTERJOIN EMPDET ON DEPARTMENT.DEPTNO=EMPDET.DEPTNO Full Outer JOIN: 1.It is the combination of left outer join and right outer join 2.It will show all the records from the table Ex: Create Database PacticeDB GO Create Table EINFO1 Go Insert into EINFO1 values(ENO int,ENAME varchar(15)) GO insert EINFO1 values(101,”AJAY”), (102,”RAM”), (103,”KIRAN”), (104,”VIKAS”), (105,”VIJAY”) Create Table EINFO2 Go Insert into EINFO2 values(ENO int,SAL varchar(MAX)) GO insert EINFO2 values(101,9000), (103,12000),(105,8000) GO Create Table EINFO1 Go Insert into EINFO3 values(ENO int,ENAME varchar(15)) GO insert EINFO3 values(101,”VIJAY”), (103,”RAM”), (105,”KIRAN”) GO
  • 4.
    Create Table EINFO4 Go Insert into EINFO4 values(ENO int,SAL varchar(MAX)) GO insert EINFO4 values(101,8000), (102,9000), (103,18000), (104,9000), (105,8000) QUERY USING FULL OUTER JOIN): SELECT A.ENO,A.ENAME,B.SAL FROM EINFO1 A FULLOUTERJOIN EINFO2 B ON A.ENO=B.ENO SELECT D.ENO,C.ENAME,D.SAL FROM EINFO3 C FULLOUTERJOIN EINFO4 D ON C.ENO=D.ENO ORDER BY D.ENO QUERY USING RIGHT OUTER JOIN SELECT EINFO4.ENO,ENAME,SAL FROM EINFO3 RIGHTOUTERJOIN EINFO4 ON EINFO3.ENO=EINFO4.ENO QUERY USING EQUI JOIN(SAME TABLE): SELECT EINFO4.ENO,ENAME,SAL FROM EINFO3,EINFO4 WHERE EINFO3.ENO=EINFO4.ENO