SlideShare a Scribd company logo
Advance Database Management System
Lecture 03:
Joining-Displaying Data from
Multiple Tables
1
Learning Objectives
2
To know about:
 Join
 Types of Join
EMPNO DEPTNO LOC
----- ------- --------
7839 10 NEW YORK
7698 30 CHICAGO
7782 10 NEW YORK
7566 20 DALLAS
7654 30 CHICAGO
7499 30 CHICAGO
...
14 rows selected.
Obtaining Data from Multiple Tables
3
EMP DEPT
EMPNO ENAME ... DEPTNO
------ ----- ... ------
7839 KING ... 10
7698 BLAKE ... 30
...
7934 MILLER ... 10
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
What Is a Join?
4
 Use a join to query data from more than one
table.
 Write the join condition in the WHERE clause.
 Prefix the column name with the table name when the
same column name appears in more than one table.
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column1 = table2.column2;
Cartesian Product
5
 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.
Generating a Cartesian Product
6
ENAME DNAME
------ ----------
KING ACCOUNTING
BLAKE ACCOUNTING
...
KING RESEARCH
BLAKE RESEARCH
...
56 rows selected.
EMP (14 rows) DEPT (4 rows)
EMPNO ENAME ... DEPTNO
------ ----- ... ------
7839 KING ... 10
7698 BLAKE ... 30
...
7934 MILLER ... 10
DEPTNO DNAME LOC
------ ---------- --------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
“Cartesian
product:
14*4=56 rows”
Types of Joins
7
Equijoin
Non-equijoin
Outer join
Self join
Cross Joins
Natural Joins
Using Clause
ON Clause
Full or two sided outer joins
What Is an Equijoin?
8
EMP DEPT
EMPNO ENAME DEPTNO
------ ------- -------
7839 KING 10
7698 BLAKE 30
7782 CLARK 10
7566 JONES 20
7654 MARTIN 30
7499 ALLEN 30
7844 TURNER 30
7900 JAMES 30
7521 WARD 30
7902 FORD 20
7369 SMITH 20
...
14 rows selected.
DEPTNO DNAME LOC
------- ---------- --------
10 ACCOUNTING NEW YORK
30 SALES CHICAGO
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
30 SALES CHICAGO
30 SALES CHICAGO
30 SALES CHICAGO
30 SALES CHICAGO
20 RESEARCH DALLAS
20 RESEARCH DALLAS
...
14 rows selected.
Foreign key Primary key
Retrieving Records with Equijoins
9
SQL> SELECT emp.empno, emp.ename, emp.deptno,
2 dept.deptno, dept.loc
3 FROM emp, dept
4 WHERE emp.deptno=dept.deptno;
EMPNO ENAME DEPTNO DEPTNO LOC
----- ------ ------ ------ ---------
7839 KING 10 10 NEW YORK
7698 BLAKE 30 30 CHICAGO
7782 CLARK 10 10 NEW YORK
7566 JONES 20 20 DALLAS
...
14 rows selected.
Qualifying Ambiguous Column Names
10
 Use table prefixes to qualify column names that are in
multiple tables.
 Improve performance by using table prefixes.
 Distinguish columns that have identical names but
reside in different tables by using column aliases.
Additional Search Conditions Using the
AND Operator
11
EMP DEPT
EMPNO ENAME DEPTNO
------ ------- -------
7839 KING 10
7698 BLAKE 30
7782 CLARK 10
7566 JONES 20
7654 MARTIN 30
7499 ALLEN 30
7844 TURNER 30
7900 JAMES 30
7521 WARD 30
7902 FORD 20
7369 SMITH 20
...
14 rows selected.
DEPTNO DNAME LOC
------ --------- --------
10 ACCOUNTING NEW YORK
30 SALES CHICAGO
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
30 SALES CHICAGO
30 SALES CHICAGO
30 SALES CHICAGO
30 SALES CHICAGO
20 RESEARCH DALLAS
20 RESEARCH DALLAS
...
14 rows selected.
Using Table Aliases
12
 Simplify queries by using table aliases.
SQL> SELECT emp.empno, emp.ename, emp.deptno,
2 dept.deptno, dept.loc
3 FROM emp, dept
4 WHERE emp.deptno=dept.deptno;
SQL> SELECT e.empno, e.ename, e.deptno,
2 d.deptno, d.loc
3 FROM emp e, dept d
4 WHERE e.deptno=d.deptno;
Joining More Than Two Tables
13
NAME CUSTID
----------- ------
JOCKSPORTS 100
TKB SPORT SHOP 101
VOLLYRITE 102
JUST TENNIS 103
K+T SPORTS 105
SHAPE UP 106
WOMENS SPORTS 107
... ...
9 rows selected.
CUSTOMER
CUSTID ORDID
------- -------
101 610
102 611
104 612
106 601
102 602
106 604
106 605
...
21 rows selected.
ORD
ORDID ITEMID
------ -------
610 3
611 1
612 1
601 1
602 1
...
64 rows selected.
ITEM
Non-Equijoins
14
EMP SALGRADE
“salary in the EMP
table is between
low salary and high
salary in the SALGRADE
table”
EMPNO ENAME SAL
------ ------- ------
7839 KING 5000
7698 BLAKE 2850
7782 CLARK 2450
7566 JONES 2975
7654 MARTIN 1250
7499 ALLEN 1600
7844 TURNER 1500
7900 JAMES 950
...
14 rows selected.
GRADE LOSAL HISAL
----- ----- ------
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
Retrieving Records with Non-Equijoins
15
ENAME SAL GRADE
---------- --------- ---------
JAMES 950 1
SMITH 800 1
ADAMS 1100 1
...
14 rows selected.
SQL> SELECT e.ename, e.sal, s.grade
2 FROM emp e, salgrade s
3 WHERE e.sal
4 BETWEEN s.losal AND s.hisal;
Outer Joins
16
EMP DEPT
No employee in the
OPERATIONS department
ENAME DEPTNO
----- ------
KING 10
BLAKE 30
CLARK 10
JONES 20
...
DEPTNO DNAME
------ ----------
10 ACCOUNTING
30 SALES
10 ACCOUNTING
20 RESEARCH
...
40 OPERATIONS
Outer Joins
17
 You use an outer join to also see rows that do not
usually meet the join condition.
 Outer join operator is the plus sign (+).
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column(+) = table2.column;
SELECT table1.column, table2.column
FROM table1, table2
WHERE table1.column = table2.column(+);
Using Outer Joins
18
SQL> SELECT e.ename, d.deptno, d.dname
2 FROM emp e, dept d
3 WHERE e.deptno(+) = d.deptno
4 ORDER BY e.deptno;
ENAME DEPTNO DNAME
---------- --------- -------------
KING 10 ACCOUNTING
CLARK 10 ACCOUNTING
...
40 OPERATIONS
15 rows selected.
Self Joins
19
EMP (WORKER) EMP (MANAGER)
“MGR in the WORKER table is equal to EMPNO in the
MANAGER table”
EMPNO ENAME MGR
----- ------ ----
7839 KING
7698 BLAKE 7839
7782 CLARK 7839
7566 JONES 7839
7654 MARTIN 7698
7499 ALLEN 7698
EMPNO ENAME
----- --------
7839 KING
7839 KING
7839 KING
7698 BLAKE
7698 BLAKE
Joining a Table to Itself
20
WORKER.ENAME||'WORKSFOR'||MANAG
-------------------------------
BLAKE works for KING
CLARK works for KING
JONES works for KING
MARTIN works for BLAKE
...
13 rows selected.
SQL> SELECT worker.ename||' works for '||manager.ename
2 FROM emp worker, emp manager
3 WHERE worker.mgr = manager.empno;
Cross Join: Same as Cartesian product
21
Select empno,dname
From emp
Cross join dept;
Select empno,dname from emp,dept;
22
Select empno,dname from emp
Natural join dept; [where deptno in (10,30);]
Select empno,dname from emp,dept
Where emp.deptno=dept.deptno;
Natural Join or Join (Equi Join)
Join with USING clause
23
Select empno,dname
From emp join dept
Using (deptno)
[where deptno<>10]
Join with ON clause
24
Select empno,dname from emp e
join dept d
On (e.deptno=d.deptno);
[where deptno in (10,30);]
 Can also be used for Self join:
Select e.ename,m.ename
From emp e join emp m
On (e.mgr=m.empno);
3 way joins with ON clause
25
Select empno,ename,dname,sal,grade from
emp e
Join dept d
On e.deptno=d.deptno
Join salgrade l
On (sal between losal and hisal)
Left outer Join
26
Select ename,d.deptno,dname from emp
Left outer join dept d
On (emp.deptno=d.deptno);
Right outer Join
27
Select ename,d.deptno,dname from dept
d right outer join emp e
On (e.deptno=d.deptno);
Full outer Join
28
Select ename,d.deptno,dname from dept
d full outer join emp e
On (e.deptno=d.deptno);
29
THANK YOU

More Related Content

Similar to Lecture03.._Joining_Spring2023-2024.pptx

SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2Umair Amjad
 
ANSI vs Oracle language
ANSI vs Oracle languageANSI vs Oracle language
ANSI vs Oracle language
Connor McDonald
 
Connor McDonald 11g for developers
Connor McDonald 11g for developersConnor McDonald 11g for developers
Connor McDonald 11g for developers
InSync Conference
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
Sachin Shukla
 
Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
siavosh kaviani
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
siavosh kaviani
 
The Five Best Things To Happen To SQL
The Five Best Things To Happen To SQLThe Five Best Things To Happen To SQL
The Five Best Things To Happen To SQL
Connor McDonald
 
Les02.pptx
Les02.pptxLes02.pptx
Les02.pptx
NishaTariq1
 
Get your moneys worth out of your database
Get your moneys worth out of your databaseGet your moneys worth out of your database
Get your moneys worth out of your database
Patrick Barel
 
Flashback ITOUG
Flashback ITOUGFlashback ITOUG
Flashback ITOUG
Connor McDonald
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data
HuzaifaMushtaq3
 
chap2 (3).ppt
chap2 (3).pptchap2 (3).ppt
chap2 (3).ppt
eemantariq2
 
12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax
Connor McDonald
 
ILOUG 2019 - SQL features for Developers
ILOUG 2019 - SQL features for DevelopersILOUG 2019 - SQL features for Developers
ILOUG 2019 - SQL features for Developers
Connor McDonald
 
DBMS Lab
DBMS LabDBMS Lab
DBMS Lab
Neil Mathew
 
COIS 420 - Practice02
COIS 420 - Practice02COIS 420 - Practice02
COIS 420 - Practice02
Angel G Diaz
 

Similar to Lecture03.._Joining_Spring2023-2024.pptx (20)

11 things about 11gr2
11 things about 11gr211 things about 11gr2
11 things about 11gr2
 
SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2
 
ANSI vs Oracle language
ANSI vs Oracle languageANSI vs Oracle language
ANSI vs Oracle language
 
Connor McDonald 11g for developers
Connor McDonald 11g for developersConnor McDonald 11g for developers
Connor McDonald 11g for developers
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
 
Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
 
Les02
Les02Les02
Les02
 
Les02 Restricting And Sorting Data
Les02 Restricting And Sorting DataLes02 Restricting And Sorting Data
Les02 Restricting And Sorting Data
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 
The Five Best Things To Happen To SQL
The Five Best Things To Happen To SQLThe Five Best Things To Happen To SQL
The Five Best Things To Happen To SQL
 
Les02.pptx
Les02.pptxLes02.pptx
Les02.pptx
 
Get your moneys worth out of your database
Get your moneys worth out of your databaseGet your moneys worth out of your database
Get your moneys worth out of your database
 
Flashback ITOUG
Flashback ITOUGFlashback ITOUG
Flashback ITOUG
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data
 
chap2 (3).ppt
chap2 (3).pptchap2 (3).ppt
chap2 (3).ppt
 
12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax12c Mini Lesson - ANSI standard TOP-N query syntax
12c Mini Lesson - ANSI standard TOP-N query syntax
 
ILOUG 2019 - SQL features for Developers
ILOUG 2019 - SQL features for DevelopersILOUG 2019 - SQL features for Developers
ILOUG 2019 - SQL features for Developers
 
DBMS Lab
DBMS LabDBMS Lab
DBMS Lab
 
COIS 420 - Practice02
COIS 420 - Practice02COIS 420 - Practice02
COIS 420 - Practice02
 
Les12 creating views
Les12 creating viewsLes12 creating views
Les12 creating views
 

Recently uploaded

Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Denish Jangid
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Sourabh Kumar
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
PedroFerreira53928
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
Nguyen Thanh Tu Collection
 

Recently uploaded (20)

Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptxSolid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
Solid waste management & Types of Basic civil Engineering notes by DJ Sir.pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.pptBasic_QTL_Marker-assisted_Selection_Sourabh.ppt
Basic_QTL_Marker-assisted_Selection_Sourabh.ppt
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
PART A. Introduction to Costumer Service
PART A. Introduction to Costumer ServicePART A. Introduction to Costumer Service
PART A. Introduction to Costumer Service
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
50 ĐỀ LUYỆN THI IOE LỚP 9 - NĂM HỌC 2022-2023 (CÓ LINK HÌNH, FILE AUDIO VÀ ĐÁ...
 

Lecture03.._Joining_Spring2023-2024.pptx

  • 1. Advance Database Management System Lecture 03: Joining-Displaying Data from Multiple Tables 1
  • 2. Learning Objectives 2 To know about:  Join  Types of Join
  • 3. EMPNO DEPTNO LOC ----- ------- -------- 7839 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLAS 7654 30 CHICAGO 7499 30 CHICAGO ... 14 rows selected. Obtaining Data from Multiple Tables 3 EMP DEPT EMPNO ENAME ... DEPTNO ------ ----- ... ------ 7839 KING ... 10 7698 BLAKE ... 30 ... 7934 MILLER ... 10 DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON
  • 4. What Is a Join? 4  Use a join to query data from more than one table.  Write the join condition in the WHERE clause.  Prefix the column name with the table name when the same column name appears in more than one table. SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2;
  • 5. Cartesian Product 5  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 6 ENAME DNAME ------ ---------- KING ACCOUNTING BLAKE ACCOUNTING ... KING RESEARCH BLAKE RESEARCH ... 56 rows selected. EMP (14 rows) DEPT (4 rows) EMPNO ENAME ... DEPTNO ------ ----- ... ------ 7839 KING ... 10 7698 BLAKE ... 30 ... 7934 MILLER ... 10 DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON “Cartesian product: 14*4=56 rows”
  • 7. Types of Joins 7 Equijoin Non-equijoin Outer join Self join Cross Joins Natural Joins Using Clause ON Clause Full or two sided outer joins
  • 8. What Is an Equijoin? 8 EMP DEPT EMPNO ENAME DEPTNO ------ ------- ------- 7839 KING 10 7698 BLAKE 30 7782 CLARK 10 7566 JONES 20 7654 MARTIN 30 7499 ALLEN 30 7844 TURNER 30 7900 JAMES 30 7521 WARD 30 7902 FORD 20 7369 SMITH 20 ... 14 rows selected. DEPTNO DNAME LOC ------- ---------- -------- 10 ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 20 RESEARCH DALLAS 20 RESEARCH DALLAS ... 14 rows selected. Foreign key Primary key
  • 9. Retrieving Records with Equijoins 9 SQL> SELECT emp.empno, emp.ename, emp.deptno, 2 dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; EMPNO ENAME DEPTNO DEPTNO LOC ----- ------ ------ ------ --------- 7839 KING 10 10 NEW YORK 7698 BLAKE 30 30 CHICAGO 7782 CLARK 10 10 NEW YORK 7566 JONES 20 20 DALLAS ... 14 rows selected.
  • 10. Qualifying Ambiguous Column Names 10  Use table prefixes to qualify column names that are in multiple tables.  Improve performance by using table prefixes.  Distinguish columns that have identical names but reside in different tables by using column aliases.
  • 11. Additional Search Conditions Using the AND Operator 11 EMP DEPT EMPNO ENAME DEPTNO ------ ------- ------- 7839 KING 10 7698 BLAKE 30 7782 CLARK 10 7566 JONES 20 7654 MARTIN 30 7499 ALLEN 30 7844 TURNER 30 7900 JAMES 30 7521 WARD 30 7902 FORD 20 7369 SMITH 20 ... 14 rows selected. DEPTNO DNAME LOC ------ --------- -------- 10 ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 20 RESEARCH DALLAS 20 RESEARCH DALLAS ... 14 rows selected.
  • 12. Using Table Aliases 12  Simplify queries by using table aliases. SQL> SELECT emp.empno, emp.ename, emp.deptno, 2 dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; SQL> SELECT e.empno, e.ename, e.deptno, 2 d.deptno, d.loc 3 FROM emp e, dept d 4 WHERE e.deptno=d.deptno;
  • 13. Joining More Than Two Tables 13 NAME CUSTID ----------- ------ JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS 107 ... ... 9 rows selected. CUSTOMER CUSTID ORDID ------- ------- 101 610 102 611 104 612 106 601 102 602 106 604 106 605 ... 21 rows selected. ORD ORDID ITEMID ------ ------- 610 3 611 1 612 1 601 1 602 1 ... 64 rows selected. ITEM
  • 14. Non-Equijoins 14 EMP SALGRADE “salary in the EMP table is between low salary and high salary in the SALGRADE table” EMPNO ENAME SAL ------ ------- ------ 7839 KING 5000 7698 BLAKE 2850 7782 CLARK 2450 7566 JONES 2975 7654 MARTIN 1250 7499 ALLEN 1600 7844 TURNER 1500 7900 JAMES 950 ... 14 rows selected. GRADE LOSAL HISAL ----- ----- ------ 1 700 1200 2 1201 1400 3 1401 2000 4 2001 3000 5 3001 9999
  • 15. Retrieving Records with Non-Equijoins 15 ENAME SAL GRADE ---------- --------- --------- JAMES 950 1 SMITH 800 1 ADAMS 1100 1 ... 14 rows selected. SQL> SELECT e.ename, e.sal, s.grade 2 FROM emp e, salgrade s 3 WHERE e.sal 4 BETWEEN s.losal AND s.hisal;
  • 16. Outer Joins 16 EMP DEPT No employee in the OPERATIONS department ENAME DEPTNO ----- ------ KING 10 BLAKE 30 CLARK 10 JONES 20 ... DEPTNO DNAME ------ ---------- 10 ACCOUNTING 30 SALES 10 ACCOUNTING 20 RESEARCH ... 40 OPERATIONS
  • 17. Outer Joins 17  You use an outer join to also see rows that do not usually meet the join condition.  Outer join operator is the plus sign (+). SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column(+) = table2.column; SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column = table2.column(+);
  • 18. Using Outer Joins 18 SQL> SELECT e.ename, d.deptno, d.dname 2 FROM emp e, dept d 3 WHERE e.deptno(+) = d.deptno 4 ORDER BY e.deptno; ENAME DEPTNO DNAME ---------- --------- ------------- KING 10 ACCOUNTING CLARK 10 ACCOUNTING ... 40 OPERATIONS 15 rows selected.
  • 19. Self Joins 19 EMP (WORKER) EMP (MANAGER) “MGR in the WORKER table is equal to EMPNO in the MANAGER table” EMPNO ENAME MGR ----- ------ ---- 7839 KING 7698 BLAKE 7839 7782 CLARK 7839 7566 JONES 7839 7654 MARTIN 7698 7499 ALLEN 7698 EMPNO ENAME ----- -------- 7839 KING 7839 KING 7839 KING 7698 BLAKE 7698 BLAKE
  • 20. Joining a Table to Itself 20 WORKER.ENAME||'WORKSFOR'||MANAG ------------------------------- BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE ... 13 rows selected. SQL> SELECT worker.ename||' works for '||manager.ename 2 FROM emp worker, emp manager 3 WHERE worker.mgr = manager.empno;
  • 21. Cross Join: Same as Cartesian product 21 Select empno,dname From emp Cross join dept; Select empno,dname from emp,dept;
  • 22. 22 Select empno,dname from emp Natural join dept; [where deptno in (10,30);] Select empno,dname from emp,dept Where emp.deptno=dept.deptno; Natural Join or Join (Equi Join)
  • 23. Join with USING clause 23 Select empno,dname From emp join dept Using (deptno) [where deptno<>10]
  • 24. Join with ON clause 24 Select empno,dname from emp e join dept d On (e.deptno=d.deptno); [where deptno in (10,30);]  Can also be used for Self join: Select e.ename,m.ename From emp e join emp m On (e.mgr=m.empno);
  • 25. 3 way joins with ON clause 25 Select empno,ename,dname,sal,grade from emp e Join dept d On e.deptno=d.deptno Join salgrade l On (sal between losal and hisal)
  • 26. Left outer Join 26 Select ename,d.deptno,dname from emp Left outer join dept d On (emp.deptno=d.deptno);
  • 27. Right outer Join 27 Select ename,d.deptno,dname from dept d right outer join emp e On (e.deptno=d.deptno);
  • 28. Full outer Join 28 Select ename,d.deptno,dname from dept d full outer join emp e On (e.deptno=d.deptno);