You are on page 1of 22

JOINS

IN
SQL SERVER

Prepared By
Bhagirathi Muduli
JOINS
 JOIN is a query clause that can be used with the SELECT, UPDATE, and DELETE
data query statements to simultaneously affect rows from multiple tables.
 Joined tables must each include at least one filed in both tables that contain
comparable data.
Self Join

Inner Join

Outer Join
Left Outer Join
Right Outer Join
Full Outer Join

Cross Join
SELF JOIN

In this circumstance, the same table is specified with


different aliases in order to match the data within the same
table.
EMP

Write the query to find the corresponding manager name for each employee ?
SELECT A.EMPNO, A.ENAME, A.MGR, B.ENAME, A.JOB, A.SAL, A.DEPTNO
FROM EMP A LEFT JOIN EMP B ON A.MGR=B.EMPNO
INNER JOIN

Match rows between the defined tables specified in the


INNER JOIN statement based on one or more columns
having matching data.  
SELECT NM.EMPLID, NM.FIRST_NAME, NM.LAST_NAME,
DEPT.DEPTID, DEPT.DESCR, JC.JOB_CODE, JC.JOB_DESCR FROM
PS_NAMES AS NM INNER JOIN PS_JOB AS JOB ON
NM.EMPLID=JOB.EMPLID INNER JOIN PS_JOBCODE AS JC ON
JC.JOB_CODE=JOB.JOB_CODE INNER JOIN PS_DEPT_TBL AS DEPT
ON DEPT.DEPTID=JOB.DEPTID
OUTER JOIN
An OUTER JOIN is a join operation that includes rows that have a match,
plus rows that do not have a match in the other table.
LEFT OUTER JOIN
Returns all the rows from the left table in conjunction with the matching rows from
the right table. If there are no columns matching in the right table, it returns NULL
values.
RIGHT OUTER JOIN
Returns all the rows from the right table in conjunction with the matching rows
from the left table. If there are no columns matching in the left table, it returns
NULL values.
FULL OUTER JOIN
Combines left outer join and right after join. It returns row from either table when
the conditions are met and returns null value when there is no match.
LEFT OUTER JOIN

Write the query for counting the number of employees


according to their respective department and job profile?
SELECT DEPT.DEPTID, DEPT.DESCR, JC.JOB_CODE,
JC.JOB_DESCR, COUNT(JOB.EMPLID) AS NO_OF_EMPLOYEE
FROM PS_DEPT_TBL AS DEPT LEFT JOIN PS_JOB AS JOB ON
DEPT.DEPTID=JOB.DEPTID LEFT JOIN PS_JOBCODE JC ON
JC.JOB_CODE=JOB.JOB_CODE GROUP BY DEPT.DEPTID,
DEPT.DESCR,JC.JOB_CODE, JC.JOB_DESCR ORDER BY
DEPT.DEPTID
RIGHT OUTER JOIN
Write the query for finding the detail information about an
employee?
SELECT NM.EMPLID,NM.FIRST_NAME+' '+NM.LAST_NAME AS
EMPLOYEE_NAME, DEPT.DEPTID, DEPT.DESCR
, JC.JOB_CODE, JC.JOB_DESCR FROM PS_DEPT_TBL AS DEPT
INNER JOIN PS_JOB AS JOB ON DEPT.DEPTID=JOB.DEPTID
INNER JOIN PS_JOBCODE AS JC ON
JC.JOB_CODE=JOB.JOB_CODE RIGHT JOIN PS_NAMES AS NM
ON NM.EMPLID=JOB.EMPLID
CROSS JOIN
Does not necessitate any condition to join. It contains records that are multiplication
of record number from both the tables.
SET OPERATION IN SQL SERVER DATABASE
The set operators allow to serially combine more than one
select statements. Although more than one select
statement will then be present, only one result set is then
returned.
If the select statements vary in their numbers of returned columns, SQL
Server report an error message

Msg 205, Level 16, State 1, Line 1


All queries combined using a UNION, INTERSECT or EXCEPT operator
must have an equal number of expressions in their target lists.

 UNION  INTERSECT  EXCEPT


 UNION
QUERY1  
UNION [ ALL ]   
QUERY2   
[ ...n ] ]

SELECT DEPTID,
DESCR FROM
PS_JOB
UNION
SELECT DEPTID,
DESCR FROM
PS_DEPT_TBL
ALL TABLES RECORDS
SELECT DEPTID, DESCR
FROM PS_JOB UNION
ALL SELECT DEPTID,
DESCR FROM
PS_DEPT_TBL
 INTERSECT
QUERY1 INTERSECT QUERY2    [ ...n ] ]
SELECT DEPTID FROM PS_JOB INTERSECT SELECT
DEPTID FROM PS_DEPT_TBL
 EXCEPT
QUERY1 EXCEPTQUERY2    [ ...n ] ]
SELECT DEPTID FROM PS_DEPT_TBL EXCEPT
SELECT DEPTID FROM PS_JOB
SUBQUERY
 A subquery is a SELECT query that returns a single value and is nested inside a
SELECT, INSERT, UPDATE, or DELETE statement, or inside another subquery.

 A subquery can be used anywhere an expression is allowed.


SELECT NM.EMPLID, NM.FIRST_NAME+' ' +NM.LAST_NAME AS
EMP_NAME, NM.AGE, DEPT.DESCR FROM PS_NAMES NM LEFT
JOIN PS_JOB JOB ON NM.EMPLID=JOB.EMPLID LEFT JOIN
PS_DEPT_TBL DEPT ON DEPT.DEPTID=JOB.DEPTID WHERE
DEPT.DEPTID = (SELECT DEPTID FROM PS_DEPT_TBL WHERE
DESCR='SOFTWARE DEVELOPMENT')

You might also like