Sunteți pe pagina 1din 7

Joins in SQL Server

Joins in SQL server is used to integrate rows from multiple data sets, based on a common field between
them.

There are various types of joins available in SQL server.

 Inner Join
 Outer Join
 Full Outer Join – also called Full Join
 Left Outer Join – also called Left Join
 Right Outer Join – also called Right Join
 Self Join
 Cross Join

(Image Reference: https://www.dofactory.com/sql/join)

To understand them more, we will go through each join with examples.

Before that, let us first have 2 tables with data.

Table 1: Employee

EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO


7839 KING PRESIDENT null 11/17/1981 5000 100 10
7698 BLAKE MANAGER 7839 5/1/1981 2850 null 30
7782 CLARK MANAGER 7839 6/9/1981 2450 100 10
7566 JONES MANAGER 7839 4/2/1981 2975 120 20
7788 SCOTT ANALYST 7566 4/19/1987 3000 120 20
7902 FORD ANALYST 7566 12/3/1981 3000 120 20
7369 SMITH CLERK 7902 12/17/1980 800 120 20
7499 ALLEN SALESMAN 7698 2/20/1981 1600 300 30
7521 WARD SALESMAN 7698 2/22/1981 1250 500 30
7654 MARTIN SALESMAN 7698 9/28/1981 1250 1400 30

Table 2: Departments

DEPTNO DNAME LOC


10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
70 MARKETING ATLANTA

Inner Join
Inner join is used to extract the records which are common between both the tables. In SQL terms, inner
join returns all records where join condition is met.

General form of inner join SQL statement is:

SELECT column-names

FROM table1 INNER JOIN table2

ON table1.columnname = table2.columnname

For above 2 tables, we can write the inner join statement like below:

SELECT *

FROM emp e INNER JOIN dept d

ON e.deptno = d.deptno

Query Output:

EMPN ENA JOB MG HIREDA SAL COM DEPT DEPT DNAME LOC
O ME R TE M NO NO
7839 KING PRESIDE null ###### 5000 100 10 10 ACCOUNTI NEW
NT ## NG YORK
7782 CLAR MANAG 7839 6/9/198 2450 100 10 10 ACCOUNTI NEW
K ER 1 NG YORK
7788 SCOT ANALYS 7566 ###### 3000 120 20 20 RESEARCH DALLA
T T ## S
7369 SMIT CLERK 7902 ###### 800 120 20 20 RESEARCH DALLA
H ## S
7902 FORD ANALYS 7566 ###### 3000 120 20 20 RESEARCH DALLA
T ## S
7566 JONE MANAG 7839 4/2/198 2975 120 20 20 RESEARCH DALLA
S ER 1 S
7499 ALLE SALESM 7698 ###### 1600 300 30 30 SALES CHICA
N AN ## GO
7698 BLAK MANAG 7839 5/1/198 2850 null 30 30 SALES CHICA
E ER 1 GO

Here, in query output, we can see employee having empno as 7521 and 7654 were not displayed
because they have deptno as null. So, they are failing while joining with dept table.

Outer Join
Left Outer Join
As the name implies, left outer join extracts all the records from the left table whereas from right table,
only common records whichever are meeting join condition will be fetched.

General form of left outer join SQL statement is:

SELECT column-names

FROM table1 LEFT OUTER JOIN table2

ON table1.columnname = table2.columnname

For above 2 tables, we can write the left outer join statement like below:

SELECT *

FROM emp e LEFT OUTER JOIN dept d

ON e.deptno = d.deptno

Query Output:

EMPN ENAM JOB MG HIREDA SAL COM DEPTN DEPTN DNAME LOC
O E R TE M O O
7782 CLARK MANAG 783 6/9/198 245 100 10 10 ACCOUNTI NEW
ER 9 1 0 NG YORK
7839 KING PRESIDE null ###### 500 100 10 10 ACCOUNTI NEW
NT ## 0 NG YORK
7369 SMITH CLERK 790 ###### 800 120 20 20 RESEARCH DALLAS
2 ##
7902 FORD ANALYST 756 ###### 300 120 20 20 RESEARCH DALLAS
6 ## 0
7788 SCOTT ANALYST 756 ###### 300 120 20 20 RESEARCH DALLAS
6 ## 0
7566 JONES MANAG 783 4/2/198 297 120 20 20 RESEARCH DALLAS
ER 9 1 5
7499 ALLEN SALESM 769 ###### 160 300 30 30 SALES CHICA
AN 8 ## 0 GO
7698 BLAKE MANAG 783 5/1/198 285 null 30 30 SALES CHICA
ER 9 1 0 GO
7654 MARTI SALESM 769 ###### 125 1400 null null null null
N AN 8 ## 0
7521 WARD SALESM 769 ###### 125 500 null null null null
AN 8 ## 0

Here, in query output we can see, all the records from left (emp) table were selected irrespective of
their deptno values.

Right Outer Join


As the name implies, right outer join extracts all the records from the right table whereas from left table,
only common records whichever are meeting join condition will be fetched.

General form of right outer join SQL statement is:

SELECT column-names

FROM table1 RIGHT OUTER JOIN table2

ON table1.columnname = table2.columnname

For above 2 tables, we can write the right outer join statement like below:

SELECT *

FROM emp e RIGHT OUTER JOIN dept d

ON e.deptno = d.deptno

Query Output:

EMPN ENAM JOB MGR HIRED SAL COM DEPT DEPT DNAME LOC
O E ATE M NO NO
7839 KING PRESIDE null ###### 5000 100 10 10 ACCOUNT NEW
NT ## ING YORK
7782 CLAR MANAG 7839 6/9/19 2450 100 10 10 ACCOUNT NEW
K ER 81 ING YORK
7369 SMIT CLERK 7902 ###### 800 120 20 20 RESEARCH DALLA
H ## S
7788 SCOT ANALYS 7566 ###### 3000 120 20 20 RESEARCH DALLA
T T ## S
7566 JONE MANAG 7839 4/2/19 2975 120 20 20 RESEARCH DALLA
S ER 81 S
7902 FORD ANALYS 7566 ###### 3000 120 20 20 RESEARCH DALLA
T ## S
7499 ALLEN SALESM 7698 ###### 1600 300 30 30 SALES CHICA
AN ## GO
7698 BLAKE MANAG 7839 5/1/19 2850 null 30 30 SALES CHICA
ER 81 GO
null null null null null null null null 40 OPERATIO BOSTO
NS N
null null null null null null null null 70 MARKETI ATLAN
NG TA

Here, in query output, we can see all the records from right(dept) table are fetched and only matched
records from left(emp) table are fetched.

Full Outer Join


Full outer join extracts all the records from left and right both the tables irrespective of any condition.

General form of full outer join SQL statement is:

SELECT column-names

FROM table1 FULL OUTER JOIN table2

ON table1.columnname = table2.columnname

For above 2 tables, we can write the right outer join statement like below:

SELECT *

FROM emp e FULL OUTER JOIN dept d

ON e.deptno = d.deptno

Query Output:

EMPN ENAM JOB MGR HIRED SAL COM DEPT DEPT DNAME LOC
O E ATE M NO NO
7839 KING PRESIDE null ###### 5000 100 10 10 ACCOUNT NEW
NT ## ING YORK
7698 BLAKE MANAG 7839 5/1/19 2850 null 30 30 SALES CHICA
ER 81 GO
7782 CLAR MANAG 7839 6/9/19 2450 100 10 10 ACCOUNT NEW
K ER 81 ING YORK
7566 JONE MANAG 7839 4/2/19 2975 120 20 20 RESEARCH DALLA
S ER 81 S
7788 SCOT ANALYS 7566 ###### 3000 120 20 20 RESEARCH DALLA
T T ## S
7902 FORD ANALYS 7566 ###### 3000 120 20 20 RESEARCH DALLA
T ## S
7369 SMIT CLERK 7902 ###### 800 120 20 20 RESEARCH DALLA
H ## S
7499 ALLEN SALESM 7698 ###### 1600 300 30 30 SALES CHICA
AN ## GO
7521 WAR SALESM 7698 ###### 1250 500 null null null null
D AN ##
7654 MART SALESM 7698 ###### 1250 1400 null null null null
IN AN ##
null null null null null null null null 70 MARKETI ATLAN
NG TA
null null null null null null null null 40 OPERATIO BOSTO
NS N

Here, in query output, we can see all the records from left(emp) and right(dept) table are fetched. Full
outer join is also termed as join of left and right outer join.

Self Join
Self join is a simple join with the same table itself. It is mainly used when the hierarchy is involved, or
some relationships is there between records in the same table. For example, each employee will have
one manager. And each manager is an employee as well. So for each manager, there will be a record in
employee table.

General form of self join SQL statement is:

SELECT column-names

FROM table1 t1 JOIN table1 t2

ON t1.columnname = t2.columnname

For above 2 tables, we can write the self join statement like below:

SELECT e1.*, e2.empno "MGR EMPNO", e2.ename "MGR ENAME"

FROM emp e1 JOIN emp e2

ON e1.mgr = e2.empno

Query Output:

EMPN ENAM JOB MGR HIREDATE SAL COMM DEPTN MGR MGR
O E O EMPN ENAM
O E
7902 FORD ANALYST 7566 12/3/1981 3000 120 20 7566 JONES
7788 SCOTT ANALYST 7566 4/19/1987 3000 120 20 7566 JONES
7521 WARD SALESMA 7698 2/22/1981 1250 500 null 7698 BLAKE
N
7654 MARTI SALESMA 7698 9/28/1981 1250 1400 null 7698 BLAKE
N N
7499 ALLEN SALESMA 7698 2/20/1981 1600 300 30 7698 BLAKE
N
7566 JONES MANAGE 7839 4/2/1981 2975 120 20 7839 KING
R
7782 CLARK MANAGE 7839 6/9/1981 2450 100 10 7839 KING
R
7698 BLAKE MANAGE 7839 5/1/1981 2850 null 30 7839 KING
R
7369 SMITH CLERK 7902 12/17/198 800 120 20 7902 FORD
0
Here, in query output, we can see employee details with his manager details by joining the emp table
with itself.

Cross Join
Cross join is a cartesian product of two tables. It will connect all rows of left table to each row of right
table. So, query result of a cross join is no. of rows in left table multiplied by no. of rows in right table.

General form of cross join SQL statement is:

SELECT column-names

FROM table1 t1 CROSS JOIN table1 t2

For above 2 tables, we can write the cross join statement like below:

SELECT count(*)

FROM emp CROSS JOIN dept;

Query Output:

50

50 = (no. of rows in emp table) * (no. of rows in dept table)

S-ar putea să vă placă și