SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Displaying Data from Multiple Tables
Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and nonequality joins View data that generally does not meet a join condition by using outer joins Join a table to itself
Obtaining Data from Multiple Tables EMP  DEPT   EMPNOENAME	...	DEPTNO------	-----	...	------7839KING	...	    10 7698BLAKE	...	    30    ...	 7934MILLER	...	    10 DEPTNO DNAME     LOC      ------ ----------	-------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON EMPNO DEPTNO LOC ----- ------- --------  783910 NEW YORK 769830 CHICAGO 778210 NEW YORK 756620 DALLAS 765430 CHICAGO 749930 CHICAGO ... 14 rows selected.
What Is a Join? 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. SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1= table2.column2;
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.
Generating a Cartesian Product “Cartesianproduct: 14*4=56 rows” EMP (14 rows)  DEPT (4 rows)   EMPNOENAME	...	DEPTNO------	-----	...	------7839KING	...	    10 7698BLAKE	...	    30    ...	 7934MILLER	...	    10 DEPTNO DNAME     LOC      ------ ----------	-------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON ENAME    DNAME ------ 	---------- KINGACCOUNTING BLAKEACCOUNTING  ... KINGRESEARCH BLAKERESEARCH ... 56 rows selected.
Types of Joins Equijoin Non-equijoin Outer join Self join
What Is an Equijoin? Foreign key Primary key 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 SALESCHICAGO 10 ACCOUNTINGNEW YORK  20 RESEARCHDALLAS 30 SALESCHICAGO 30 SALESCHICAGO 30 SALESCHICAGO 30 SALESCHICAGO 30 SALESCHICAGO 20 RESEARCHDALLAS 20 RESEARCHDALLAS ... 14 rows selected.
Retrieving Records with Equijoins SQL> SELECT emp.empno, emp.ename, emp.deptno,2dept.deptno, dept.loc 3  FROM   emp, dept 4  WHERE  emp.deptno=dept.deptno; EMPNO ENAME DEPTNO DEPTNO LOC ----- ------ ------ ------ --------- 7839 KING1010 NEW YORK 7698 BLAKE  3030 CHICAGO 7782 CLARK1010 NEW YORK 7566 JONES      2020 DALLAS ... 14 rows selected.
Qualifying Ambiguous Column Names 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 ConditionsUsing the AND Operator  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 ACCOUNTINGNEW YORK 30SALES    CHICAGO 10 ACCOUNTINGNEW YORK  20 RESEARCHDALLAS 30 SALES    CHICAGO 30 SALES    CHICAGO 30 SALES    CHICAGO 30 SALES    CHICAGO 30 SALES    CHICAGO 20 RESEARCHDALLAS 20 RESEARCHDALLAS ... 14 rows selected.
Using Table Aliases 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 ORD   CUSTID   ORDID ------- ------- 101610 102611 104612 106601 102602 106604 106605 ...  21 rows selected. ITEM   ORDID  ITEMID ------ ------- 6103 6111 6121 6011 6021 ... 64 rows selected.      CUSTOMER  NAMECUSTID -----------	------ JOCKSPORTS100 TKB SPORT SHOP101 VOLLYRITE102 JUST TENNIS103 K+T SPORTS105 SHAPE UP106 WOMENS SPORTS     107 ...	... 9 rows selected.
Non-Equijoins “salary in the EMP  table is between  low salary and high  salary in the SALGRADE table” EMP SALGRADE  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 ----- ----- ------ 17001200 212011400 314012000 420013000 530019999
Retrieving Records with Non-Equijoins SQL> SELECT e.ename, e.sal, s.grade 2FROMemp e, salgrade s 3WHERE e.sal 4BETWEEN s.losal AND s.hisal; ENAME            SAL     GRADE ---------- --------- --------- JAMES            9501 SMITH            8001 ADAMS           11001 ... 14 rows selected.
Outer Joins No employee in theOPERATIONS department EMP  DEPT  ENAMEDEPTNO-----	------KING10 BLAKE30 CLARK10 JONES20 ...	 DEPTNO DNAME ------ ---------- 10ACCOUNTING 30SALES 10ACCOUNTING 20RESEARCH ...	 40OPERATIONS
Outer Joins You use an outer join to also see rows that do not usually meet the join condition. Outer join operator is the plus sign (+). SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column(+)= table2.column; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column(+);
Using Outer Joins SQL> SELECTe.ename, d.deptno, d.dname 2  FROMemp e, dept d 3  WHEREe.deptno(+) = d.deptno 4  ORDER BYe.deptno; ENAME         DEPTNO DNAME ---------- --------- ------------- KING              10 ACCOUNTING CLARK             10 ACCOUNTING ... 40 OPERATIONS 15 rows selected.
Self Joins “MGR in the WORKER table is equal to EMPNO in the MANAGER table” EMP (WORKER) EMP (MANAGER) EMPNOENAME MGR-----	------	----7839KING 7698BLAKE7839 7782CLARK7839 7566JONES7839 7654MARTIN7698 7499ALLEN7698 EMPNOENAME-----	-------- 7839KING 7839KING 7839KING 7698BLAKE 7698BLAKE
Joining a Table to Itself SQL> SELECT worker.ename||' works for '||manager.ename 2  FROM emp worker, emp manager 3  WHERE worker.mgr = manager.empno; WORKER.ENAME||'WORKSFOR'||MANAG ------------------------------- BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE ... 13 rows selected.
Summary SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1= table2.column2; Equijoin Non-equijoin Outer join Self join
Practice Overview Joining tables using an equijoin Performing outer and self joins Adding conditions

Weitere ähnliche Inhalte

Was ist angesagt?

SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12
Umair Amjad
 
SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2
Umair Amjad
 

Was ist angesagt? (20)

Les03 Single Row Function
Les03 Single Row FunctionLes03 Single Row Function
Les03 Single Row Function
 
Les03
Les03Les03
Les03
 
Les01
Les01Les01
Les01
 
Les12
Les12Les12
Les12
 
Les01
Les01Les01
Les01
 
Les10
Les10Les10
Les10
 
Les11
Les11Les11
Les11
 
SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12
 
Les09
Les09Les09
Les09
 
Les02
Les02Les02
Les02
 
ORACLE NOTES
ORACLE NOTESORACLE NOTES
ORACLE NOTES
 
SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2
 
MERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known FacetsMERGE SQL Statement: Lesser Known Facets
MERGE SQL Statement: Lesser Known Facets
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Les05[1]Aggregating Data Using Group Functions
Les05[1]Aggregating Data  Using Group FunctionsLes05[1]Aggregating Data  Using Group Functions
Les05[1]Aggregating Data Using Group Functions
 
COIS 420 - Practice02
COIS 420 - Practice02COIS 420 - Practice02
COIS 420 - Practice02
 
Les02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting DataLes02[1]Restricting and Sorting Data
Les02[1]Restricting and Sorting Data
 
Les03[1] Single-Row Functions
Les03[1] Single-Row FunctionsLes03[1] Single-Row Functions
Les03[1] Single-Row Functions
 
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكلحل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
حل اسئلة الكتاب السعودى فى شرح قواعد البيانات اوراكل
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 

Ähnlich wie Les04 Displaying Data From Multiple Table

SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4
Umair Amjad
 
Les01-Oracle
Les01-OracleLes01-Oracle
Les01-Oracle
suman1248
 
The select statement
The select statementThe select statement
The select statement
punu_82
 

Ähnlich wie Les04 Displaying Data From Multiple Table (20)

COIS 420 - Practice04
COIS 420 - Practice04COIS 420 - Practice04
COIS 420 - Practice04
 
SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4SQL WORKSHOP::Lecture 4
SQL WORKSHOP::Lecture 4
 
Les01-Oracle
Les01-OracleLes01-Oracle
Les01-Oracle
 
Sql2
Sql2Sql2
Sql2
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
 
Analytic SQL Sep 2013
Analytic SQL Sep 2013Analytic SQL Sep 2013
Analytic SQL Sep 2013
 
Connor McDonald 11g for developers
Connor McDonald 11g for developersConnor McDonald 11g for developers
Connor McDonald 11g for developers
 
Sangam 19 - Analytic SQL
Sangam 19 - Analytic SQLSangam 19 - Analytic SQL
Sangam 19 - Analytic SQL
 
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
 
11 things about 11gr2
11 things about 11gr211 things about 11gr2
11 things about 11gr2
 
Les02.pptx
Les02.pptxLes02.pptx
Les02.pptx
 
The select statement
The select statementThe select statement
The select statement
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchies
 
Les09[1]Manipulating Data
Les09[1]Manipulating DataLes09[1]Manipulating Data
Les09[1]Manipulating Data
 
OpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developersOpenWorld Sep14 12c for_developers
OpenWorld Sep14 12c for_developers
 
DBMS Lab
DBMS LabDBMS Lab
DBMS Lab
 
ANSI vs Oracle language
ANSI vs Oracle languageANSI vs Oracle language
ANSI vs Oracle language
 
Basic sql statements
Basic sql statementsBasic sql statements
Basic sql statements
 
CHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptxCHAPTER 1 BASIC sql STATEMENTS.pptx
CHAPTER 1 BASIC sql STATEMENTS.pptx
 

Kürzlich hochgeladen

Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
russian goa call girl and escorts service
 
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Goa Call Girls 9316020077 Call Girls In Goa By Russian Call Girl in goa
Goa Call Girls 9316020077 Call Girls  In Goa By Russian Call Girl in goaGoa Call Girls 9316020077 Call Girls  In Goa By Russian Call Girl in goa
Goa Call Girls 9316020077 Call Girls In Goa By Russian Call Girl in goa
russian goa call girl and escorts service
 
Goa Call "Girls Service 9316020077 Call "Girls in Goa
Goa Call "Girls  Service   9316020077 Call "Girls in GoaGoa Call "Girls  Service   9316020077 Call "Girls in Goa
Goa Call "Girls Service 9316020077 Call "Girls in Goa
sexy call girls service in goa
 

Kürzlich hochgeladen (20)

↑Top Model (Kolkata) Call Girls Salt Lake ⟟ 8250192130 ⟟ High Class Call Girl...
↑Top Model (Kolkata) Call Girls Salt Lake ⟟ 8250192130 ⟟ High Class Call Girl...↑Top Model (Kolkata) Call Girls Salt Lake ⟟ 8250192130 ⟟ High Class Call Girl...
↑Top Model (Kolkata) Call Girls Salt Lake ⟟ 8250192130 ⟟ High Class Call Girl...
 
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
↑Top Model (Kolkata) Call Girls Rajpur ⟟ 8250192130 ⟟ High Class Call Girl In...
 
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...Call Girls  Agency In Goa  💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
Call Girls Agency In Goa 💚 9316020077 💚 Call Girl Goa By Russian Call Girl ...
 
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
Independent Joka Escorts ✔ 8250192130 ✔ Full Night With Room Online Booking 2...
 
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment BookingCall Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
Call Girls in Barasat | 7001035870 At Low Cost Cash Payment Booking
 
Call Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service NashikCall Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
Call Girls Nashik Gayatri 7001305949 Independent Escort Service Nashik
 
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in  Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Malviya Nagar, (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girl Service Belur - 7001035870 with real photos and phone numbers
Call Girl Service Belur - 7001035870 with real photos and phone numbersCall Girl Service Belur - 7001035870 with real photos and phone numbers
Call Girl Service Belur - 7001035870 with real photos and phone numbers
 
👙 Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service
👙  Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service👙  Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service
👙 Kolkata Call Girls Park Circus 💫💫7001035870 Model escorts Service
 
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
Behala ( Call Girls ) Kolkata ✔ 6297143586 ✔ Hot Model With Sexy Bhabi Ready ...
 
2k Shot Call girls Laxmi Nagar Delhi 9205541914
2k Shot Call girls Laxmi Nagar Delhi 92055419142k Shot Call girls Laxmi Nagar Delhi 9205541914
2k Shot Call girls Laxmi Nagar Delhi 9205541914
 
𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Sonagachi Call Girls In All Kolkata 24/7 Provide Call W...
 
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment BookingAlmora call girls 📞 8617697112 At Low Cost Cash Payment Booking
Almora call girls 📞 8617697112 At Low Cost Cash Payment Booking
 
Goa Call Girls 9316020077 Call Girls In Goa By Russian Call Girl in goa
Goa Call Girls 9316020077 Call Girls  In Goa By Russian Call Girl in goaGoa Call Girls 9316020077 Call Girls  In Goa By Russian Call Girl in goa
Goa Call Girls 9316020077 Call Girls In Goa By Russian Call Girl in goa
 
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
5* Hotels Call Girls In Goa {{07028418221}} Call Girls In North Goa Escort Se...
 
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
Top Rated Kolkata Call Girls Khardah ⟟ 6297143586 ⟟ Call Me For Genuine Sex S...
 
Call Girl Nashik Amaira 7001305949 Independent Escort Service Nashik
Call Girl Nashik Amaira 7001305949 Independent Escort Service NashikCall Girl Nashik Amaira 7001305949 Independent Escort Service Nashik
Call Girl Nashik Amaira 7001305949 Independent Escort Service Nashik
 
Goa Call "Girls Service 9316020077 Call "Girls in Goa
Goa Call "Girls  Service   9316020077 Call "Girls in GoaGoa Call "Girls  Service   9316020077 Call "Girls in Goa
Goa Call "Girls Service 9316020077 Call "Girls in Goa
 
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
Dakshineswar Call Girls ✔ 8005736733 ✔ Hot Model With Sexy Bhabi Ready For Se...
 
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
𓀤Call On 6297143586 𓀤 Ultadanga Call Girls In All Kolkata 24/7 Provide Call W...
 

Les04 Displaying Data From Multiple Table

  • 1. Displaying Data from Multiple Tables
  • 2. Objectives After completing this lesson, you should be able to do the following: Write SELECT statements to access data from more than one table using equality and nonequality joins View data that generally does not meet a join condition by using outer joins Join a table to itself
  • 3. Obtaining Data from Multiple Tables EMP DEPT EMPNOENAME ... DEPTNO------ ----- ... ------7839KING ... 10 7698BLAKE ... 30 ... 7934MILLER ... 10 DEPTNO DNAME LOC ------ ---------- -------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON EMPNO DEPTNO LOC ----- ------- -------- 783910 NEW YORK 769830 CHICAGO 778210 NEW YORK 756620 DALLAS 765430 CHICAGO 749930 CHICAGO ... 14 rows selected.
  • 4. What Is a Join? 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. SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1= table2.column2;
  • 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 “Cartesianproduct: 14*4=56 rows” EMP (14 rows) DEPT (4 rows) EMPNOENAME ... DEPTNO------ ----- ... ------7839KING ... 10 7698BLAKE ... 30 ... 7934MILLER ... 10 DEPTNO DNAME LOC ------ ---------- -------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON ENAME DNAME ------ ---------- KINGACCOUNTING BLAKEACCOUNTING ... KINGRESEARCH BLAKERESEARCH ... 56 rows selected.
  • 7. Types of Joins Equijoin Non-equijoin Outer join Self join
  • 8. What Is an Equijoin? Foreign key Primary key 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 SALESCHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALESCHICAGO 30 SALESCHICAGO 30 SALESCHICAGO 30 SALESCHICAGO 30 SALESCHICAGO 20 RESEARCHDALLAS 20 RESEARCHDALLAS ... 14 rows selected.
  • 9. Retrieving Records with Equijoins SQL> SELECT emp.empno, emp.ename, emp.deptno,2dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; EMPNO ENAME DEPTNO DEPTNO LOC ----- ------ ------ ------ --------- 7839 KING1010 NEW YORK 7698 BLAKE 3030 CHICAGO 7782 CLARK1010 NEW YORK 7566 JONES 2020 DALLAS ... 14 rows selected.
  • 10. Qualifying Ambiguous Column Names 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 ConditionsUsing the AND Operator 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 ACCOUNTINGNEW YORK 30SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 30 SALES CHICAGO 20 RESEARCHDALLAS 20 RESEARCHDALLAS ... 14 rows selected.
  • 12. Using Table Aliases 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 ORD CUSTID ORDID ------- ------- 101610 102611 104612 106601 102602 106604 106605 ... 21 rows selected. ITEM ORDID ITEMID ------ ------- 6103 6111 6121 6011 6021 ... 64 rows selected. CUSTOMER NAMECUSTID ----------- ------ JOCKSPORTS100 TKB SPORT SHOP101 VOLLYRITE102 JUST TENNIS103 K+T SPORTS105 SHAPE UP106 WOMENS SPORTS 107 ... ... 9 rows selected.
  • 14. Non-Equijoins “salary in the EMP table is between low salary and high salary in the SALGRADE table” EMP SALGRADE 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 ----- ----- ------ 17001200 212011400 314012000 420013000 530019999
  • 15. Retrieving Records with Non-Equijoins SQL> SELECT e.ename, e.sal, s.grade 2FROMemp e, salgrade s 3WHERE e.sal 4BETWEEN s.losal AND s.hisal; ENAME SAL GRADE ---------- --------- --------- JAMES 9501 SMITH 8001 ADAMS 11001 ... 14 rows selected.
  • 16. Outer Joins No employee in theOPERATIONS department EMP DEPT ENAMEDEPTNO----- ------KING10 BLAKE30 CLARK10 JONES20 ... DEPTNO DNAME ------ ---------- 10ACCOUNTING 30SALES 10ACCOUNTING 20RESEARCH ... 40OPERATIONS
  • 17. Outer Joins You use an outer join to also see rows that do not usually meet the join condition. Outer join operator is the plus sign (+). SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column(+)= table2.column; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column = table2.column(+);
  • 18. Using Outer Joins SQL> SELECTe.ename, d.deptno, d.dname 2 FROMemp e, dept d 3 WHEREe.deptno(+) = d.deptno 4 ORDER BYe.deptno; ENAME DEPTNO DNAME ---------- --------- ------------- KING 10 ACCOUNTING CLARK 10 ACCOUNTING ... 40 OPERATIONS 15 rows selected.
  • 19. Self Joins “MGR in the WORKER table is equal to EMPNO in the MANAGER table” EMP (WORKER) EMP (MANAGER) EMPNOENAME MGR----- ------ ----7839KING 7698BLAKE7839 7782CLARK7839 7566JONES7839 7654MARTIN7698 7499ALLEN7698 EMPNOENAME----- -------- 7839KING 7839KING 7839KING 7698BLAKE 7698BLAKE
  • 20. Joining a Table to Itself SQL> SELECT worker.ename||' works for '||manager.ename 2 FROM emp worker, emp manager 3 WHERE worker.mgr = manager.empno; WORKER.ENAME||'WORKSFOR'||MANAG ------------------------------- BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE ... 13 rows selected.
  • 21. Summary SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1= table2.column2; Equijoin Non-equijoin Outer join Self join
  • 22. Practice Overview Joining tables using an equijoin Performing outer and self joins Adding conditions