0% found this document useful (0 votes)
20 views40 pages

DBMSPractical - Join-Updated

The document discusses different types of joins in SQL including equijoins, non-equijoins, semi joins, and outer joins. It provides examples of how to write queries to perform each type of join between two or more tables including the use of table aliases and additional search conditions.

Uploaded by

Harsh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views40 pages

DBMSPractical - Join-Updated

The document discusses different types of joins in SQL including equijoins, non-equijoins, semi joins, and outer joins. It provides examples of how to write queries to perform each type of join between two or more tables including the use of table aliases and additional search conditions.

Uploaded by

Harsh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

DBMS Lab

JOIN
Dr. Rabindra Kumar Barik
Associate Professor,
School of Computer Application,
KIIT University, Odisha - India
2
Displaying Data
from Multiple Tables
Obtaining Data from Multiple Tables

EMPLOYEES DEPARTMENTS


What is a Join ?

A join is used to query data from one table or more than


one tables.

SELECT table1.column, table2.column


FROM table1,table2
WHERE table1.column1 = table2.column2;

Write the join condition in the WHERE clause.


Prefix the column name with the table name when
the same column appears in more than one table.

5
Cartesian Product

• A Cartesian product is formed when:


• A join condition is omitted
• A join condition is invalid
• All rows in the first table are joined to all rows in the
second table.

• To avoid a Cartesian product, always include a valid join


condition in a WHERE clause.

6
Generating a Cartesian Product
EMPLOYEES DEPARTMENTS

107 rows returned 27 rows returned

2889 rows returned


Cartesian Product :
107* 27 = 2889 rows
7
Generating a Cartesian Product
EMP (14 rows) DEPT (4 rows)
EMPNO ENAME . . . DEPTNO DEPTNO DNAME LOC
----------- ----------- ------------- ------------ --------------------- -----------
7839 KING 10 10 ACCOUNTING NEW YORK
7698 BLAKE 30 20 RESEARCH DALLAS
7934 MILLER 10 30 SALES CHICAGO
... 40 OPERATIONS BOSTON

ENAME DNAME
----------- --------------
KING ACCOUNTING
BLAKE ACCOUNTING
…….
KING RESEARCH
BLAKE RESEARCH
....
56 rows selected “ Cartesian Product :
14 * 4 = 56 rows ”
8
Types of Joins

◦ Cross Joins
◦ Equi joins
◦ Non Equi Joins
◦ Semi Join
◦ Outer joins
◦ Anti Join
◦ Self Joins
What is an Equijoin ?

DEPARTMENTS
EMPLOYEES

Primary Key
Foreign Key
10
Retrieving Records with Equijoin
Select
employees.employee_id,employees.last_name,employees.departme
nt_id,departments.department_name from employees ,departments
where employees.department_id=departments.department_id

Sample Output

……

11
Additional Search Conditions Using And
Operator
Select
employees.employee_id,employees.last_name,employees.department_id,depa
rtments.department_name ,salary from employees ,departments where
employees.department_id=departments.department_id and
department_name='Executive'or department_name='IT' and salary>12000
Sample Output

12
Using Table Alias
Select
employees.employee_id,employees.last_name,employ
ees.department_id,departments.department_name,sal
ary from employees ,departments where
employees.department_id=departments.department_id

select
e.employee_id,e.last_name,e.department_id,d.department_nam
e ,e.salary from employees e ,departments d where
e.department_id=d.department_id
13
Joining More Than Two Tables
EMPLOYEES DEPARTMENTS

LOCATIONS
select
e.employee_id,e.last_name,e.department_id,d.department_name
, l.location_id,l.city from employees e ,departments d ,locations l
where e.department_id=d.department_id and
d.location_id=l.location_id

Sample Output

……….

15
What is a Non- Equijoin ?
EMPLOYEES JOBS

Salary in the EMPLOYEES table is between min salary


and max salary in the JOBS table.
16
Retrieving Records with Non-Equijoin
select employee_id ,first_name,salary ,job_title from
employees,jobs where salary between min_salary and
max_salary
Sample Output

…………..

17
Semi Joins
• Semijoin is used infrequently for
join operations
• Semijoin is alike equi join with the
only exception that attributes in
the first relation are returned in
the result

18
Example of Semi Joins
select
e.employee_id,e.last_name,e.department_id,e.salar
y from employees e ,departments d where
e.department_id=d.department_id and
department_name = 'IT'
Sample Output

19
Outer Joins
1. To see rows that do not usually meet the join
condition
2. Outer join operator is (+) sign
3. Three types of Outer Joins:
•Left Outer Joins
•Right Outer Joins
•Full Outer Joins

20
What is Outer Join ?
EMPLOYEES DEPARTMENTS
EMPNO ENAME . . . DEPTNO DEPTNO DNAME LOC
----------- ----------- ------------- ------------ --------------------- -----------
7839 KING 10 10 ACCOUNTING NEW YORK
7698 BLAKE 30 20 RESEARCH DALLAS
7782 CLARK 10 30 SALES CHICAGO
7566 JONES 20 40 OPERATIONS BOSTON
7654 MARTIN 30
7499 ALLEN 30
7844 TURNER 30
7900 JAMES 30
7521 WARD 30
7902 FORD 20
7369 SMITH 20 No employee in the
7001 RAVI ---- Operation Department
...
14 rows selected.
No department assign
to the Employee
21
Left Outer Joins
Syntax

SELECT table1.column, table2.column


FROM table1,table2
WHERE table1.column = table2.column (+);

22
table1.column = Is the condition that joins (or
relates) the tables together.

table2.column (+) is the outer join symbol, which


can be placed either side of the
WHERE clause condition, but not
both sides. (Place the outer join
symbol following the name of the
column in the table without the
matching rows.)

23
Using Left Outer Joins
CREATE TABLE DEPT11 (DEPTNO NUMBER(4) PRIMARY KEY, DEPTNAME VARCHAR2(20));

INSERT INTO DEPT11 VALUES(10, 'SCA');


INSERT INTO DEPT11 VALUES(20, 'SCS');
INSERT INTO DEPT11 VALUES(30, 'SCE');
SELECT * FROM DEPT11;

CREATE TABLE EMP11(EMPNO NUMBER(4) PRIMARY KEY,EMPNAME


VARCHAR2(20),EMPSALARY NUMBER(7,2), DEPTNO NUMBER(4), CONSTRAINT RAVI_FK
FOREIGN KEY (DEPTNO) REFERENCES DEPT11 (DEPTNO))

INSERT INTO EMP11 VALUES (1, 'RAVI', 20000, 20);


INSERT INTO EMP11 VALUES (2, 'MUSHTAK', 30000, 10);
INSERT INTO EMP11 VALUES (3, 'VIVEK', 30000, 20);
INSERT INTO EMP11(empno,empname,empsalary) VALUES (4, 'JOHN', 20000);

SELECT * FROM EMP11;

select e.empno, e.empname, e.empsalary, d.deptno from emp11 e, dept11


d where e.deptno=d.deptno(+)

24
Using Left Outer Joins
SELECT * FROM EMP11; SELECT * FROM DEPT11;

select e.empno, e.empname, e.empsalary, d.deptno from


emp11 e, dept11 d where e.deptno=d.deptno(+) ;

25
Right Outer Joins

Syntax

SELECT table1.column, table2.column


FROM table1,table2
WHERE table1.column(+) = table2.column;

26
table1.column (+) is the outer join symbol, which
can be placed either side of the
WHERE clause condition, but not
both sides. (Place the outer join
symbol following the name of the
column in the table without the
matching rows.)

= table2.column Is the condition that joins (or


relates) the tables together.

27
Using Right Outer Joins
CREATE TABLE DEPT11 (DEPTNO NUMBER(4) PRIMARY KEY, DEPTNAME VARCHAR2(20));

INSERT INTO DEPT11 VALUES(10, 'SCA');


INSERT INTO DEPT11 VALUES(20, 'SCS');
INSERT INTO DEPT11 VALUES(30, 'SCE');
SELECT * FROM DEPT11;

CREATE TABLE EMP11(EMPNO NUMBER(4) PRIMARY KEY,EMPNAME


VARCHAR2(20),EMPSALARY NUMBER(7,2), DEPTNO NUMBER(4), CONSTRAINT RAVI_FK
FOREIGN KEY (DEPTNO) REFERENCES DEPT11 (DEPTNO))

INSERT INTO EMP11 VALUES (1, 'RAVI', 20000, 20);


INSERT INTO EMP11 VALUES (2, 'MUSHTAK', 30000, 10);
INSERT INTO EMP11 VALUES (3, 'VIVEK', 30000, 20);
INSERT INTO EMP11(empno,empname,empsalary) VALUES (4, 'JOHN', 20000);

SELECT * FROM EMP11;

select e.empno, e.empname, e.empsalary, d.deptno from emp11 e, dept11


d where e.deptno(+)=d.deptno ;

28
Using Right Outer Joins
SELECT * FROM EMP11; SELECT * FROM DEPT11;

select e.empno, e.empname, e.empsalary, d.deptno from


emp11 e, dept11 d where e.deptno(+)=d.deptno ;

29
Full Outer Joins
Syntax
SELECT table1.column, table2.column
FROM table1,table2
WHERE table1.column(+) = table2.column;

union
SELECT table1.column, table2.column
FROM table1,table2
WHERE table1.column = table2.column (+);

30
Using Full Outer Joins
CREATE TABLE DEPT11 (DEPTNO NUMBER(4) PRIMARY KEY, DEPTNAME VARCHAR2(20));

INSERT INTO DEPT11 VALUES(10, 'SCA');


INSERT INTO DEPT11 VALUES(20, 'SCS');
INSERT INTO DEPT11 VALUES(30, 'SCE');
SELECT * FROM DEPT11;

CREATE TABLE EMP11(EMPNO NUMBER(4) PRIMARY KEY,EMPNAME


VARCHAR2(20),EMPSALARY NUMBER(7,2), DEPTNO NUMBER(4), CONSTRAINT RAVI_FK
FOREIGN KEY (DEPTNO) REFERENCES DEPT11 (DEPTNO))

INSERT INTO EMP11 VALUES (1, 'RAVI', 20000, 20);


INSERT INTO EMP11 VALUES (2, 'MUSHTAK', 30000, 10);
INSERT INTO EMP11 VALUES (3, 'VIVEK', 30000, 20);
INSERT INTO EMP11(empno,empname,empsalary) VALUES (4, 'JOHN', 20000);

SELECT * FROM EMP11;

select e.empno, e.empname, e.empsalary, d.deptno from emp11 e, dept11


d where e.deptno=d.deptno(+) union select e.empno, e.empname,
e.empsalary, d.deptno from emp11 e, dept11 d where
e.deptno(+)=d.deptno ;
31
Using Full Outer Joins
SELECT * FROM EMP11; SELECT * FROM DEPT11;

select e.empno, e.empname, e.empsalary, d.deptno from


emp11 e, dept11 d where e.deptno=d.deptno(+) union select
e.empno, e.empname, e.empsalary, d.deptno from emp11 e,
dept11 d where e.deptno(+)=d.deptno ;

32
Anti Joins
• An antijoin between two tables returns rows from the
first table where no matches are found in the second
table.
• Anti-joins are written using the NOT EXISTS or NOT IN
constructs
• To find un-matched data between two tables Anti-Join
is better than using not in or not exists.
• It is the opposite work of semi joins.
Anti Joins
DEPARTMENTS EMPLOYEES

returns a list of empty departments.


Anti Joins..
returning a list of empty departments.
SELECT D1.department_id, D1.department_name FROM
departments D1 MINUS SELECT D2.department_id,
D2.department_name FROM departments D2, employees E2
WHERE D2.department_id = E2.department_id ;
Output
Self Joins

A self join is a join made on the same table.

To perform a self join, it must use a different table


alias.

36
Self Joins
WORKER MANAGER
Employee_ID First_Name Manager_ID Employee_ID First_Name
----------- ----------- ------------- ----------- -----------
7839 KING 7839 KING
7698 BLAKE 7839 7698 BLAKE
7782 CLARK 7839 7782 CLARK
7566 JONES 7839 7566 JONES
7654 MARTIN 7839 7654 MARTIN
7499 ALLEN 7839 7499 ALLEN

“Manager_ID in the WORKER table is equal to Employee_ID in


the MANAGER table”

Utpal/SCA/SQL-5 37
Self Joins

Employee_ID First_Name Manager_ID


----------- ----------- -------------
7839 KING
7698 BLAKE 7839
7782 CLARK 7839
7566 JONES 7839
7654 MARTIN 7839
7499 ALLEN 7839

“Manager_ID in the WORKER table is equal to Employee_ID in


the MANAGER table”

38
Joining a Table to itself
SELECT worker.first_name || 'works for' || manager.
first_name FROM employees worker, employees
manager WHERE
worker.manager_id=manager.employee_id;
Sample Output

………………

39
40

You might also like