SlideShare une entreprise Scribd logo
1  sur  21
4
   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

4-2
Obtaining Data from Multiple Tables
EMP                           DEPT
 EMPNO   ENAME   ... DEPTNO   DEPTNO   DNAME        LOC
------   -----   ... ------   ------   ----------   --------
  7839   KING    ...     10       10   ACCOUNTING   NEW YORK
  7698   BLAKE   ...     30       20   RESEARCH     DALLAS
   ...                            30   SALES        CHICAGO
  7934   MILLER ...      10       40   OPERATIONS   BOSTON



             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.

4-3
What Is a Join?
 Use a join to query data from more than
 one table.
      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 name
    appears in more than one table.
4-4
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.

4-5
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
    ...                            30   SALES        CHICAGO
   7934   MILLER ...      10       40   OPERATIONS   BOSTON



                    ENAME    DNAME
                    ------   ----------
                    KING     ACCOUNTING
    “Cartesian      BLAKE    ACCOUNTING
      product:      ...
                    KING     RESEARCH
14*4=56 rows”       BLAKE    RESEARCH
                    ...
                    56 rows selected.

 4-6
Types of Joins

Equijoin Non-equijoin Outer join Self join




4-7
What Is an Equijoin?
EMP                        DEPT
 EMPNO ENAME    DEPTNO      DEPTNO   DNAME        LOC
------ ------- -------     -------   ----------   --------
  7839 KING         10          10   ACCOUNTING   NEW YORK
  7698 BLAKE        30          30   SALES        CHICAGO
  7782 CLARK        10          10   ACCOUNTING   NEW YORK
  7566 JONES        20          20   RESEARCH     DALLAS
  7654 MARTIN       30          30   SALES        CHICAGO
  7499 ALLEN        30          30   SALES        CHICAGO
  7844 TURNER       30          30   SALES        CHICAGO
  7900 JAMES        30          30   SALES        CHICAGO
  7521 WARD         30          30   SALES        CHICAGO
  7902 FORD         20          20   RESEARCH     DALLAS
  7369 SMITH        20          20   RESEARCH     DALLAS
...                        ...
14 rows selected.          14 rows   selected.

             Foreign key   Primary key
4-8
Retrieving Records
             with Equijoins
 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.


4-9
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.



4-10
Additional Search Conditions
         Using the AND Operator
EMP                       DEPT
  EMPNO ENAME    DEPTNO   DEPTNO DNAME        LOC
 ------ ------- -------   ------ ---------    --------
   7839 KING         10       10 ACCOUNTING   NEW YORK
   7698 BLAKE        30       30 SALES        CHICAGO
   7782 CLARK        10       10 ACCOUNTING   NEW YORK
   7566 JONES        20       20 RESEARCH     DALLAS
   7654 MARTIN       30       30 SALES        CHICAGO
   7499 ALLEN        30       30 SALES        CHICAGO
   7844 TURNER       30       30 SALES        CHICAGO
   7900 JAMES        30       30 SALES        CHICAGO
   7521 WARD         30       30 SALES        CHICAGO
   7902 FORD         20       20 RESEARCH     DALLAS
   7369 SMITH        20       20 RESEARCH     DALLAS
 ...                      ...
 14 rows selected.        14 rows selected.

4-11
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;




4-12
Joining More Than Two Tables
CUSTOMER                  ORD
 NAME           CUSTID     CUSTID   ORDID
 -----------    ------    ------- -------
 JOCKSPORTS        100        101     610
 TKB SPORT SHOP     101       102     611
 VOLLYRITE          102       104     612
 JUST TENNIS        103       106     601
 K+T SPORTS        105        102     602        ITEM
 SHAPE UP           106       106     604
                                    ORDID ITEMID
 WOMENS SPORTS     107        106     605
                                   ------ -------
 ...            ...       ...
                                      610       3
 9 rows selected.         21 rows selected.
                                      611       1
                                      612       1
                                      601       1
                                      602       1
                                   ...
                                   64 rows selected.

4-13
Non-Equijoins
       EMP                     SALGRADE
        EMPNO ENAME      SAL   GRADE LOSAL HISAL
       ------ ------- ------   ----- ----- ------
         7839 KING      5000   1       700   1200
         7698 BLAKE     2850   2      1201   1400
         7782 CLARK     2450   3      1401   2000
         7566 JONES     2975   4      2001   3000
         7654 MARTIN    1250   5      3001   9999
         7499 ALLEN     1600
         7844 TURNER    1500
         7900 JAMES      950
       ...                     “salary in the EMP
       14 rows selected.       table is between
                               low salary and high
                               salary in the SALGRADE
                               table”

4-14
Retrieving Records
           with Non-Equijoins
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;


 ENAME            SAL     GRADE
 ---------- --------- ---------
 JAMES            950         1
 SMITH            800         1
 ADAMS           1100         1
 ...
 14 rows selected.


4-15
Outer Joins
       EMP              DEPT
       ENAME   DEPTNO   DEPTNO   DNAME
       -----   ------   ------   ----------
       KING    10       10       ACCOUNTING
       BLAKE   30       30       SALES
       CLARK   10       10       ACCOUNTING
       JONES   20       20       RESEARCH
       ...              ...
                        40       OPERATIONS



                No employee in the
                OPERATIONS department



4-16
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 (+).
 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(+);



4-17
Using Outer Joins
 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.




4-18
Self Joins
        EMP (WORKER)             EMP (MANAGER)
         EMPNO   ENAME     MGR    EMPNO ENAME
         -----   ------   ----    ----- --------
          7839   KING
          7698   BLAKE    7839     7839   KING
          7782   CLARK    7839     7839   KING
          7566   JONES    7839     7839   KING
          7654   MARTIN   7698     7698   BLAKE
          7499   ALLEN    7698     7698   BLAKE




       “MGR in the WORKER table is equal to EMPNO in the
                      MANAGER table”


4-19
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.




4-20
Summary

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



Equijoin Non-equijoin Outer join Self join




4-21

Contenu connexe

En vedette

OracleXE & SQLDeveloper Install
OracleXE & SQLDeveloper InstallOracleXE & SQLDeveloper Install
OracleXE & SQLDeveloper InstallAngel G Diaz
 
Lesson02 - Network Design & LAN
Lesson02 - Network Design & LANLesson02 - Network Design & LAN
Lesson02 - Network Design & LANAngel G Diaz
 
Making Connections
Making ConnectionsMaking Connections
Making ConnectionsAngel G Diaz
 
Errors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlErrors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlAngel G Diaz
 
Lesson 01 - Network Assessment
Lesson 01 - Network AssessmentLesson 01 - Network Assessment
Lesson 01 - Network AssessmentAngel G Diaz
 
Conducted and Wireless Media
Conducted and Wireless MediaConducted and Wireless Media
Conducted and Wireless MediaAngel G Diaz
 
Lesson - 02 Network Design and Management
Lesson - 02 Network Design and ManagementLesson - 02 Network Design and Management
Lesson - 02 Network Design and ManagementAngel G Diaz
 
Making Connections Efficient: Multiplexing and Compression
Making Connections Efficient: Multiplexing and CompressionMaking Connections Efficient: Multiplexing and Compression
Making Connections Efficient: Multiplexing and CompressionAngel G Diaz
 
Lesson 01 - Introduction to SQL
Lesson 01 - Introduction to SQLLesson 01 - Introduction to SQL
Lesson 01 - Introduction to SQLAngel G Diaz
 
SQL Developer installation-guide
SQL Developer installation-guideSQL Developer installation-guide
SQL Developer installation-guideAngel G Diaz
 
Fundamentals of Data and Signals
Fundamentals of Data and SignalsFundamentals of Data and Signals
Fundamentals of Data and SignalsAngel G Diaz
 
Errors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlErrors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlAngel G Diaz
 
Wired and wireless technologies
Wired and  wireless  technologiesWired and  wireless  technologies
Wired and wireless technologiesAkhil Sabu
 

En vedette (14)

OracleXE & SQLDeveloper Install
OracleXE & SQLDeveloper InstallOracleXE & SQLDeveloper Install
OracleXE & SQLDeveloper Install
 
Lesson02 - Network Design & LAN
Lesson02 - Network Design & LANLesson02 - Network Design & LAN
Lesson02 - Network Design & LAN
 
Making Connections
Making ConnectionsMaking Connections
Making Connections
 
Errors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlErrors, Error Detection, and Error Control
Errors, Error Detection, and Error Control
 
Lesson 01 - Network Assessment
Lesson 01 - Network AssessmentLesson 01 - Network Assessment
Lesson 01 - Network Assessment
 
Conducted and Wireless Media
Conducted and Wireless MediaConducted and Wireless Media
Conducted and Wireless Media
 
Lesson - 02 Network Design and Management
Lesson - 02 Network Design and ManagementLesson - 02 Network Design and Management
Lesson - 02 Network Design and Management
 
Making Connections Efficient: Multiplexing and Compression
Making Connections Efficient: Multiplexing and CompressionMaking Connections Efficient: Multiplexing and Compression
Making Connections Efficient: Multiplexing and Compression
 
Lesson 01 - Introduction to SQL
Lesson 01 - Introduction to SQLLesson 01 - Introduction to SQL
Lesson 01 - Introduction to SQL
 
SQL Developer installation-guide
SQL Developer installation-guideSQL Developer installation-guide
SQL Developer installation-guide
 
Fundamentals of Data and Signals
Fundamentals of Data and SignalsFundamentals of Data and Signals
Fundamentals of Data and Signals
 
Errors, Error Detection, and Error Control
Errors, Error Detection, and Error ControlErrors, Error Detection, and Error Control
Errors, Error Detection, and Error Control
 
Wired and wireless technologies
Wired and  wireless  technologiesWired and  wireless  technologies
Wired and wireless technologies
 
OSI Model
OSI ModelOSI Model
OSI Model
 

Similaire à COIS 420 - Practice04 (20)

Sql2
Sql2Sql2
Sql2
 
Analytic SQL Sep 2013
Analytic SQL Sep 2013Analytic SQL Sep 2013
Analytic SQL Sep 2013
 
SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2SQL WORKSHOP::Lecture 2
SQL WORKSHOP::Lecture 2
 
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
 
11 things about 11gr2
11 things about 11gr211 things about 11gr2
11 things about 11gr2
 
Using SQL to process hierarchies
Using SQL to process hierarchiesUsing SQL to process hierarchies
Using SQL to process hierarchies
 
80 different SQL Queries with output
80 different SQL Queries with output80 different SQL Queries with output
80 different SQL Queries with output
 
Les02 Restricting And Sorting Data
Les02 Restricting And Sorting DataLes02 Restricting And Sorting Data
Les02 Restricting And Sorting Data
 
Les02
Les02Les02
Les02
 
Les02.pptx
Les02.pptxLes02.pptx
Les02.pptx
 
Analytics ioug 2011
Analytics ioug 2011Analytics ioug 2011
Analytics ioug 2011
 
SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12SQL WORKSHOP::Lecture 12
SQL WORKSHOP::Lecture 12
 
Restricting and sorting data
Restricting and sorting data Restricting and sorting data
Restricting and sorting data
 
Les12 creating views
Les12 creating viewsLes12 creating views
Les12 creating views
 
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
 
DBMS Lab
DBMS LabDBMS Lab
DBMS Lab
 
Les02
Les02Les02
Les02
 
SQL and PLSQL features for APEX Developers
SQL and PLSQL features for APEX DevelopersSQL and PLSQL features for APEX Developers
SQL and PLSQL features for APEX Developers
 

Plus de Angel G Diaz

GNS3 Simulator Installation
GNS3 Simulator InstallationGNS3 Simulator Installation
GNS3 Simulator InstallationAngel G Diaz
 
WAMP & Joomla! Installation
WAMP & Joomla! InstallationWAMP & Joomla! Installation
WAMP & Joomla! InstallationAngel G Diaz
 
COIS 420 - Practice 03
COIS 420 - Practice 03COIS 420 - Practice 03
COIS 420 - Practice 03Angel G Diaz
 
COIS 420 - Practice01
COIS 420 - Practice01COIS 420 - Practice01
COIS 420 - Practice01Angel G Diaz
 
Introduction to Computer Networks and Data Communications
Introduction to Computer Networks and Data CommunicationsIntroduction to Computer Networks and Data Communications
Introduction to Computer Networks and Data CommunicationsAngel G Diaz
 

Plus de Angel G Diaz (6)

GNS3 Simulator Installation
GNS3 Simulator InstallationGNS3 Simulator Installation
GNS3 Simulator Installation
 
WAMP & Joomla! Installation
WAMP & Joomla! InstallationWAMP & Joomla! Installation
WAMP & Joomla! Installation
 
COIS 420 - Practice 03
COIS 420 - Practice 03COIS 420 - Practice 03
COIS 420 - Practice 03
 
COIS 420 - Practice01
COIS 420 - Practice01COIS 420 - Practice01
COIS 420 - Practice01
 
Introduction to Computer Networks and Data Communications
Introduction to Computer Networks and Data CommunicationsIntroduction to Computer Networks and Data Communications
Introduction to Computer Networks and Data Communications
 
Cois240 lesson01
Cois240 lesson01Cois240 lesson01
Cois240 lesson01
 

Dernier

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 

Dernier (20)

Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 

COIS 420 - Practice04

  • 1. 4 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 4-2
  • 3. Obtaining Data from Multiple Tables EMP DEPT EMPNO ENAME ... DEPTNO DEPTNO DNAME LOC ------ ----- ... ------ ------ ---------- -------- 7839 KING ... 10 10 ACCOUNTING NEW YORK 7698 BLAKE ... 30 20 RESEARCH DALLAS ... 30 SALES CHICAGO 7934 MILLER ... 10 40 OPERATIONS BOSTON 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. 4-3
  • 4. What Is a Join? Use a join to query data from more than one table. 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 name appears in more than one table. 4-4
  • 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. 4-5
  • 6. 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 ... 30 SALES CHICAGO 7934 MILLER ... 10 40 OPERATIONS BOSTON ENAME DNAME ------ ---------- KING ACCOUNTING “Cartesian BLAKE ACCOUNTING product: ... KING RESEARCH 14*4=56 rows” BLAKE RESEARCH ... 56 rows selected. 4-6
  • 7. Types of Joins Equijoin Non-equijoin Outer join Self join 4-7
  • 8. What Is an Equijoin? EMP DEPT EMPNO ENAME DEPTNO DEPTNO DNAME LOC ------ ------- ------- ------- ---------- -------- 7839 KING 10 10 ACCOUNTING NEW YORK 7698 BLAKE 30 30 SALES CHICAGO 7782 CLARK 10 10 ACCOUNTING NEW YORK 7566 JONES 20 20 RESEARCH DALLAS 7654 MARTIN 30 30 SALES CHICAGO 7499 ALLEN 30 30 SALES CHICAGO 7844 TURNER 30 30 SALES CHICAGO 7900 JAMES 30 30 SALES CHICAGO 7521 WARD 30 30 SALES CHICAGO 7902 FORD 20 20 RESEARCH DALLAS 7369 SMITH 20 20 RESEARCH DALLAS ... ... 14 rows selected. 14 rows selected. Foreign key Primary key 4-8
  • 9. Retrieving Records with Equijoins 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. 4-9
  • 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. 4-10
  • 11. Additional Search Conditions Using the AND Operator EMP DEPT EMPNO ENAME DEPTNO DEPTNO DNAME LOC ------ ------- ------- ------ --------- -------- 7839 KING 10 10 ACCOUNTING NEW YORK 7698 BLAKE 30 30 SALES CHICAGO 7782 CLARK 10 10 ACCOUNTING NEW YORK 7566 JONES 20 20 RESEARCH DALLAS 7654 MARTIN 30 30 SALES CHICAGO 7499 ALLEN 30 30 SALES CHICAGO 7844 TURNER 30 30 SALES CHICAGO 7900 JAMES 30 30 SALES CHICAGO 7521 WARD 30 30 SALES CHICAGO 7902 FORD 20 20 RESEARCH DALLAS 7369 SMITH 20 20 RESEARCH DALLAS ... ... 14 rows selected. 14 rows selected. 4-11
  • 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; 4-12
  • 13. Joining More Than Two Tables CUSTOMER ORD NAME CUSTID CUSTID ORDID ----------- ------ ------- ------- JOCKSPORTS 100 101 610 TKB SPORT SHOP 101 102 611 VOLLYRITE 102 104 612 JUST TENNIS 103 106 601 K+T SPORTS 105 102 602 ITEM SHAPE UP 106 106 604 ORDID ITEMID WOMENS SPORTS 107 106 605 ------ ------- ... ... ... 610 3 9 rows selected. 21 rows selected. 611 1 612 1 601 1 602 1 ... 64 rows selected. 4-13
  • 14. Non-Equijoins EMP SALGRADE EMPNO ENAME SAL GRADE LOSAL HISAL ------ ------- ------ ----- ----- ------ 7839 KING 5000 1 700 1200 7698 BLAKE 2850 2 1201 1400 7782 CLARK 2450 3 1401 2000 7566 JONES 2975 4 2001 3000 7654 MARTIN 1250 5 3001 9999 7499 ALLEN 1600 7844 TURNER 1500 7900 JAMES 950 ... “salary in the EMP 14 rows selected. table is between low salary and high salary in the SALGRADE table” 4-14
  • 15. Retrieving Records with Non-Equijoins 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; ENAME SAL GRADE ---------- --------- --------- JAMES 950 1 SMITH 800 1 ADAMS 1100 1 ... 14 rows selected. 4-15
  • 16. Outer Joins EMP DEPT ENAME DEPTNO DEPTNO DNAME ----- ------ ------ ---------- KING 10 10 ACCOUNTING BLAKE 30 30 SALES CLARK 10 10 ACCOUNTING JONES 20 20 RESEARCH ... ... 40 OPERATIONS No employee in the OPERATIONS department 4-16
  • 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 (+). 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(+); 4-17
  • 18. Using Outer Joins 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. 4-18
  • 19. Self Joins EMP (WORKER) EMP (MANAGER) EMPNO ENAME MGR EMPNO ENAME ----- ------ ---- ----- -------- 7839 KING 7698 BLAKE 7839 7839 KING 7782 CLARK 7839 7839 KING 7566 JONES 7839 7839 KING 7654 MARTIN 7698 7698 BLAKE 7499 ALLEN 7698 7698 BLAKE “MGR in the WORKER table is equal to EMPNO in the MANAGER table” 4-19
  • 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. 4-20
  • 21. Summary SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2; Equijoin Non-equijoin Outer join Self join 4-21