SlideShare a Scribd company logo
1 of 30
Download to read offline
Copyright © 2004, Oracle. All rights reserved.
Displaying Data
from Multiple Tables
Copyright © 2004, Oracle. All rights reserved.
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 equijoins and non-
equijoins
• Join a table to itself by using a self-join
• View data that generally does not meet a join
condition by using outer joins
• Generate a Cartesian product of all rows from two
or more tables
Copyright © 2004, Oracle. All rights reserved.
Obtaining Data from Multiple Tables
EMPLOYEES DEPARTMENTS
…
…
Copyright © 2004, Oracle. All rights reserved.
Types of Joins
Joins that are compliant with the SQL:1999 standard
include the following:
• Cross joins
• Natural joins
• USING clause
• Full (or two-sided) outer joins
• Arbitrary join conditions for outer joins
Copyright © 2004, Oracle. All rights reserved.
Joining Tables Using SQL:1999 Syntax
Use a join to query data from more than one table:
SELECT table1.column, table2.column
FROM table1
[NATURAL JOIN table2] |
[JOIN table2 USING (column_name)] |
[JOIN table2
ON (table1.column_name = table2.column_name)]|
[LEFT|RIGHT|FULL OUTER JOIN table2
ON (table1.column_name = table2.column_name)]|
[CROSS JOIN table2];
Copyright © 2004, Oracle. All rights reserved.
Creating Natural Joins
• The NATURAL JOIN clause is based on all columns
in the two tables that have the same name.
• It selects rows from the two tables that have equal
values in all matched columns.
• If the columns having the same names have
different data types, an error is returned.
Copyright © 2004, Oracle. All rights reserved.
SELECT department_id, department_name,
location_id, city
FROM departments
NATURAL JOIN locations ;
Retrieving Records with Natural Joins
Copyright © 2004, Oracle. All rights reserved.
Creating Joins with the USING Clause
• If several columns have the same names but the
data types do not match, the NATURAL JOIN clause
can be modified with the USING clause to specify
the columns that should be used for an equijoin.
• Use the USING clause to match only one column
when more than one column matches.
• Do not use a table name or alias in the referenced
columns.
• The NATURAL JOIN and USING clauses are
mutually exclusive.
Copyright © 2004, Oracle. All rights reserved.
Joining Column Names
EMPLOYEES DEPARTMENTS
Foreign key Primary key
… …
Copyright © 2004, Oracle. All rights reserved.
SELECT employees.employee_id, employees.last_name,
departments.location_id, department_id
FROM employees JOIN departments
USING (department_id) ;
Retrieving Records with the USING Clause
…
Copyright © 2004, Oracle. All rights reserved.
Qualifying Ambiguous
Column Names
• Use table prefixes to qualify column names that
are in multiple tables.
• Use table prefixes to improve performance.
• Use column aliases to distinguish columns that
have identical names but reside in different tables.
• Do not use aliases on columns that are identified
in the USING clause and listed elsewhere in the
SQL statement.
Copyright © 2004, Oracle. All rights reserved.
SELECT e.employee_id, e.last_name,
d.location_id, department_id
FROM employees e JOIN departments d
USING (department_id) ;
Using Table Aliases
• Use table aliases to simplify queries.
• Use table aliases to improve performance.
Copyright © 2004, Oracle. All rights reserved.
Creating Joins with the ON Clause
• The join condition for the natural join is basically
an equijoin of all columns with the same name.
• Use the ON clause to specify arbitrary conditions
or specify columns to join.
• The join condition is separated from other search
conditions.
• The ON clause makes code easy to understand.
Copyright © 2004, Oracle. All rights reserved.
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id);
Retrieving Records with the ON Clause
…
Copyright © 2004, Oracle. All rights reserved.
Self-Joins Using the ON Clause
MANAGER_ID in the WORKER table is equal to
EMPLOYEE_ID in the MANAGER table.
EMPLOYEES (WORKER) EMPLOYEES (MANAGER)
… …
Copyright © 2004, Oracle. All rights reserved.
Self-Joins Using the ON Clause
SELECT e.last_name emp, m.last_name mgr
FROM employees e JOIN employees m
ON (e.manager_id = m.employee_id);
…
Copyright © 2004, Oracle. All rights reserved.
SELECT e.employee_id, e.last_name, e.department_id,
d.department_id, d.location_id
FROM employees e JOIN departments d
ON (e.department_id = d.department_id)
AND e.manager_id = 149 ;
Applying Additional Conditions
to a Join
Copyright © 2004, Oracle. All rights reserved.
SELECT employee_id, city, department_name
FROM employees e
JOIN departments d
ON d.department_id = e.department_id
JOIN locations l
ON d.location_id = l.location_id;
Creating Three-Way Joins with the
ON Clause
…
Copyright © 2004, Oracle. All rights reserved.
Non-Equijoins
EMPLOYEES JOB_GRADES
Salary in the EMPLOYEES
table must be between
lowest salary and highest
salary in the JOB_GRADES
table.
…
Copyright © 2004, Oracle. All rights reserved.
SELECT e.last_name, e.salary, j.grade_level
FROM employees e JOIN job_grades j
ON e.salary
BETWEEN j.lowest_sal AND j.highest_sal;
Retrieving Records
with Non-Equijoins
…
Copyright © 2004, Oracle. All rights reserved.
Outer Joins
EMPLOYEESDEPARTMENTS
There are no employees in
department 190.
…
Copyright © 2004, Oracle. All rights reserved.
INNER Versus OUTER Joins
• In SQL:1999, the join of two tables returning only
matched rows is called an inner join.
• A join between two tables that returns the results
of the inner join as well as the unmatched rows
from the left (or right) tables is called a left (or
right) outer join.
• A join between two tables that returns the results
of an inner join as well as the results of a left and
right join is a full outer join.
Copyright © 2004, Oracle. All rights reserved.
SELECT e.last_name, e.department_id, d.department_name
FROM employees e LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
LEFT OUTER JOIN
…
Copyright © 2004, Oracle. All rights reserved.
SELECT e.last_name, e.department_id, d.department_name
FROM employees e RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
RIGHT OUTER JOIN
…
Copyright © 2004, Oracle. All rights reserved.
SELECT e.last_name, d.department_id, d.department_name
FROM employees e FULL OUTER JOIN departments d
ON (e.department_id = d.department_id) ;
FULL OUTER JOIN
…
Copyright © 2004, Oracle. All rights reserved.
Cartesian Products
• 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.
Copyright © 2004, Oracle. All rights reserved.
Generating a Cartesian Product
Cartesian product:
20 x 8 = 160 rows
EMPLOYEES (20 rows) DEPARTMENTS (8 rows)
…
…
Copyright © 2004, Oracle. All rights reserved.
SELECT last_name, department_name
FROM employees
CROSS JOIN departments ;
Creating Cross Joins
• The CROSS JOIN clause produces the cross-
product of two tables.
• This is also called a Cartesian product between
the two tables.
…
Copyright © 2004, Oracle. All rights reserved.
Summary
In this lesson, you should have learned how to use
joins to display data from multiple tables by using:
• Equijoins
• Non-equijoins
• Outer joins
• Self-joins
• Cross joins
• Natural joins
• Full (or two-sided) outer joins
Copyright © 2004, Oracle. All rights reserved.
Practice 5: Overview
This practice covers the following topics:
• Joining tables using an equijoin
• Performing outer and self-joins
• Adding conditions

More Related Content

What's hot

Oracle sql joins
Oracle sql joinsOracle sql joins
Oracle sql joinsredro
 
e computer notes - From multiple tables
e computer notes - From multiple tablese computer notes - From multiple tables
e computer notes - From multiple tablesecomputernotes
 
Oracle SQL - Grants, filters, groups and more
Oracle SQL - Grants, filters, groups and moreOracle SQL - Grants, filters, groups and more
Oracle SQL - Grants, filters, groups and moreA Data Guru
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSgaurav koriya
 
Displaying data from multiple tables
Displaying data from multiple tablesDisplaying data from multiple tables
Displaying data from multiple tablesSyed Zaid Irshad
 
Oracle SQL - Select Part -1 let's write some queries!
Oracle SQL - Select Part -1  let's write some queries!Oracle SQL - Select Part -1  let's write some queries!
Oracle SQL - Select Part -1 let's write some queries!A Data Guru
 
Consultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datosConsultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datosCaleb Gutiérrez
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | EdurekaEdureka!
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Achmad Solichin
 
Xml part1
Xml part1  Xml part1
Xml part1 NOHA AW
 

What's hot (20)

Oracle sql joins
Oracle sql joinsOracle sql joins
Oracle sql joins
 
App C
App CApp C
App C
 
Sql joins
Sql joinsSql joins
Sql joins
 
e computer notes - From multiple tables
e computer notes - From multiple tablese computer notes - From multiple tables
e computer notes - From multiple tables
 
Les05
Les05Les05
Les05
 
Oracle SQL - Grants, filters, groups and more
Oracle SQL - Grants, filters, groups and moreOracle SQL - Grants, filters, groups and more
Oracle SQL - Grants, filters, groups and more
 
ALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMSALL ABOUT SQL AND RDBMS
ALL ABOUT SQL AND RDBMS
 
Displaying data from multiple tables
Displaying data from multiple tablesDisplaying data from multiple tables
Displaying data from multiple tables
 
Oracle SQL - Select Part -1 let's write some queries!
Oracle SQL - Select Part -1  let's write some queries!Oracle SQL - Select Part -1  let's write some queries!
Oracle SQL - Select Part -1 let's write some queries!
 
Consultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datosConsultas con agrupaci¾n de datos
Consultas con agrupaci¾n de datos
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
Sql Basics | Edureka
Sql Basics | EdurekaSql Basics | Edureka
Sql Basics | Edureka
 
Oracle: Joins
Oracle: JoinsOracle: Joins
Oracle: Joins
 
Hira
HiraHira
Hira
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
Retrieving Data From A Database
Retrieving Data From A DatabaseRetrieving Data From A Database
Retrieving Data From A Database
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
Xml part1
Xml part1  Xml part1
Xml part1
 

Viewers also liked (19)

Tupen 6 1235010002
Tupen 6   1235010002Tupen 6   1235010002
Tupen 6 1235010002
 
Lapres 4 1235010002
Lapres 4 1235010002Lapres 4 1235010002
Lapres 4 1235010002
 
Tupen 7 1235010002
Tupen 7 1235010002Tupen 7 1235010002
Tupen 7 1235010002
 
Lapres 6 1235010002
Lapres 6   1235010002Lapres 6   1235010002
Lapres 6 1235010002
 
Lapres 7 1235010002
Lapres 7 1235010002Lapres 7 1235010002
Lapres 7 1235010002
 
Tupen 7 1235010002
Tupen 7   1235010002Tupen 7   1235010002
Tupen 7 1235010002
 
Tupen 8 1235010002
Tupen 8 1235010002Tupen 8 1235010002
Tupen 8 1235010002
 
Lapres 2 1235010002
Lapres 2 1235010002Lapres 2 1235010002
Lapres 2 1235010002
 
Tupen 8 1235010002
Tupen 8   1235010002Tupen 8   1235010002
Tupen 8 1235010002
 
Les04
Les04Les04
Les04
 
Tugas jarkom router 2 1235010002
Tugas jarkom router 2   1235010002Tugas jarkom router 2   1235010002
Tugas jarkom router 2 1235010002
 
Lapres 5 1235010002
Lapres 5 1235010002Lapres 5 1235010002
Lapres 5 1235010002
 
Lapres 4 1235010002
Lapres 4 1235010002Lapres 4 1235010002
Lapres 4 1235010002
 
Lapers 6 1235010002
Lapers 6 1235010002Lapers 6 1235010002
Lapers 6 1235010002
 
Lapres 2 1235010002
Lapres 2 1235010002Lapres 2 1235010002
Lapres 2 1235010002
 
Les02
Les02Les02
Les02
 
Intro
IntroIntro
Intro
 
Les07
Les07Les07
Les07
 
Les08
Les08Les08
Les08
 

Similar to Les05

Les04 Displaying Data from Multiple Tables.ppt
Les04 Displaying Data from Multiple Tables.pptLes04 Displaying Data from Multiple Tables.ppt
Les04 Displaying Data from Multiple Tables.pptDrZeeshanBhatti
 
Displaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data BaseDisplaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data BaseSalman Memon
 
Les01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.pptLes01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.pptDrZeeshanBhatti
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsSalman Memon
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptDrZeeshanBhatti
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseSalman Memon
 
SQL, consultas rapidas y sencillas, oracle
SQL, consultas rapidas y sencillas, oracleSQL, consultas rapidas y sencillas, oracle
SQL, consultas rapidas y sencillas, oraclemarycielocartagena73
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group FunctionsSalman Memon
 
Lesson05 从多表中查询数据
Lesson05 从多表中查询数据Lesson05 从多表中查询数据
Lesson05 从多表中查询数据renguzi
 
ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"Ankit Surti
 
Oracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.pptOracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.pptDrZeeshanBhatti
 
Less07 schema
Less07 schemaLess07 schema
Less07 schemaImran Ali
 

Similar to Les05 (20)

Les04 Displaying Data from Multiple Tables.ppt
Les04 Displaying Data from Multiple Tables.pptLes04 Displaying Data from Multiple Tables.ppt
Les04 Displaying Data from Multiple Tables.ppt
 
Displaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data BaseDisplaying Data from Multiple Tables - Oracle Data Base
Displaying Data from Multiple Tables - Oracle Data Base
 
Les01
Les01Les01
Les01
 
Les01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.pptLes01 Writing BAsic SQL SELECT Statement.ppt
Les01 Writing BAsic SQL SELECT Statement.ppt
 
Writing Basic SQL SELECT Statements
Writing Basic SQL SELECT StatementsWriting Basic SQL SELECT Statements
Writing Basic SQL SELECT Statements
 
Les02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.pptLes02 Restricting and Sorting Data using SQL.ppt
Les02 Restricting and Sorting Data using SQL.ppt
 
Restricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data BaseRestricting and Sorting Data - Oracle Data Base
Restricting and Sorting Data - Oracle Data Base
 
SQL, consultas rapidas y sencillas, oracle
SQL, consultas rapidas y sencillas, oracleSQL, consultas rapidas y sencillas, oracle
SQL, consultas rapidas y sencillas, oracle
 
Aggregating Data Using Group Functions
Aggregating Data Using Group FunctionsAggregating Data Using Group Functions
Aggregating Data Using Group Functions
 
Lesson05 从多表中查询数据
Lesson05 从多表中查询数据Lesson05 从多表中查询数据
Lesson05 从多表中查询数据
 
Sql oracle
Sql oracleSql oracle
Sql oracle
 
Les10
Les10Les10
Les10
 
ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"ADVANCE SQL-"Sub queries"
ADVANCE SQL-"Sub queries"
 
Oracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.pptOracle SQL - Aggregating Data Les 05.ppt
Oracle SQL - Aggregating Data Les 05.ppt
 
Les04
Les04Les04
Les04
 
Less07 schema
Less07 schemaLess07 schema
Less07 schema
 
Les01
Les01Les01
Les01
 
plsql Les08
plsql Les08 plsql Les08
plsql Les08
 
plsql les01
 plsql les01 plsql les01
plsql les01
 
plsql Les07
plsql Les07 plsql Les07
plsql Les07
 

More from Abrianto Nugraha (20)

Ds sn is-02
Ds sn is-02Ds sn is-02
Ds sn is-02
 
Ds sn is-01
Ds sn is-01Ds sn is-01
Ds sn is-01
 
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkapPertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
Pertemuan 5 optimasi_dengan_alternatif_terbatas_-_lengkap
 
04 pemodelan spk
04 pemodelan spk04 pemodelan spk
04 pemodelan spk
 
02 sistem pengambilan-keputusan_revised
02 sistem pengambilan-keputusan_revised02 sistem pengambilan-keputusan_revised
02 sistem pengambilan-keputusan_revised
 
01 pengantar sistem-pendukung_keputusan
01 pengantar sistem-pendukung_keputusan01 pengantar sistem-pendukung_keputusan
01 pengantar sistem-pendukung_keputusan
 
Pertemuan 7
Pertemuan 7Pertemuan 7
Pertemuan 7
 
Pertemuan 7 dan_8
Pertemuan 7 dan_8Pertemuan 7 dan_8
Pertemuan 7 dan_8
 
Pertemuan 5
Pertemuan 5Pertemuan 5
Pertemuan 5
 
Pertemuan 6
Pertemuan 6Pertemuan 6
Pertemuan 6
 
Pertemuan 4
Pertemuan 4Pertemuan 4
Pertemuan 4
 
Pertemuan 3
Pertemuan 3Pertemuan 3
Pertemuan 3
 
Pertemuan 2
Pertemuan 2Pertemuan 2
Pertemuan 2
 
Pertemuan 1
Pertemuan 1Pertemuan 1
Pertemuan 1
 
Modul 1 mengambil nilai parameter
Modul 1   mengambil nilai parameterModul 1   mengambil nilai parameter
Modul 1 mengambil nilai parameter
 
Modul 3 object oriented programming dalam php
Modul 3   object oriented programming dalam phpModul 3   object oriented programming dalam php
Modul 3 object oriented programming dalam php
 
Modul 2 menyimpan ke database
Modul 2  menyimpan ke databaseModul 2  menyimpan ke database
Modul 2 menyimpan ke database
 
Pbo 7
Pbo 7Pbo 7
Pbo 7
 
Pbo 6
Pbo 6Pbo 6
Pbo 6
 
Pbo 4
Pbo 4Pbo 4
Pbo 4
 

Recently uploaded

Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 

Recently uploaded (20)

9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 

Les05

  • 1. Copyright © 2004, Oracle. All rights reserved. Displaying Data from Multiple Tables
  • 2. Copyright © 2004, Oracle. All rights reserved. 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 equijoins and non- equijoins • Join a table to itself by using a self-join • View data that generally does not meet a join condition by using outer joins • Generate a Cartesian product of all rows from two or more tables
  • 3. Copyright © 2004, Oracle. All rights reserved. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS … …
  • 4. Copyright © 2004, Oracle. All rights reserved. Types of Joins Joins that are compliant with the SQL:1999 standard include the following: • Cross joins • Natural joins • USING clause • Full (or two-sided) outer joins • Arbitrary join conditions for outer joins
  • 5. Copyright © 2004, Oracle. All rights reserved. Joining Tables Using SQL:1999 Syntax Use a join to query data from more than one table: SELECT table1.column, table2.column FROM table1 [NATURAL JOIN table2] | [JOIN table2 USING (column_name)] | [JOIN table2 ON (table1.column_name = table2.column_name)]| [LEFT|RIGHT|FULL OUTER JOIN table2 ON (table1.column_name = table2.column_name)]| [CROSS JOIN table2];
  • 6. Copyright © 2004, Oracle. All rights reserved. Creating Natural Joins • The NATURAL JOIN clause is based on all columns in the two tables that have the same name. • It selects rows from the two tables that have equal values in all matched columns. • If the columns having the same names have different data types, an error is returned.
  • 7. Copyright © 2004, Oracle. All rights reserved. SELECT department_id, department_name, location_id, city FROM departments NATURAL JOIN locations ; Retrieving Records with Natural Joins
  • 8. Copyright © 2004, Oracle. All rights reserved. Creating Joins with the USING Clause • If several columns have the same names but the data types do not match, the NATURAL JOIN clause can be modified with the USING clause to specify the columns that should be used for an equijoin. • Use the USING clause to match only one column when more than one column matches. • Do not use a table name or alias in the referenced columns. • The NATURAL JOIN and USING clauses are mutually exclusive.
  • 9. Copyright © 2004, Oracle. All rights reserved. Joining Column Names EMPLOYEES DEPARTMENTS Foreign key Primary key … …
  • 10. Copyright © 2004, Oracle. All rights reserved. SELECT employees.employee_id, employees.last_name, departments.location_id, department_id FROM employees JOIN departments USING (department_id) ; Retrieving Records with the USING Clause …
  • 11. Copyright © 2004, Oracle. All rights reserved. Qualifying Ambiguous Column Names • Use table prefixes to qualify column names that are in multiple tables. • Use table prefixes to improve performance. • Use column aliases to distinguish columns that have identical names but reside in different tables. • Do not use aliases on columns that are identified in the USING clause and listed elsewhere in the SQL statement.
  • 12. Copyright © 2004, Oracle. All rights reserved. SELECT e.employee_id, e.last_name, d.location_id, department_id FROM employees e JOIN departments d USING (department_id) ; Using Table Aliases • Use table aliases to simplify queries. • Use table aliases to improve performance.
  • 13. Copyright © 2004, Oracle. All rights reserved. Creating Joins with the ON Clause • The join condition for the natural join is basically an equijoin of all columns with the same name. • Use the ON clause to specify arbitrary conditions or specify columns to join. • The join condition is separated from other search conditions. • The ON clause makes code easy to understand.
  • 14. Copyright © 2004, Oracle. All rights reserved. SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON (e.department_id = d.department_id); Retrieving Records with the ON Clause …
  • 15. Copyright © 2004, Oracle. All rights reserved. Self-Joins Using the ON Clause MANAGER_ID in the WORKER table is equal to EMPLOYEE_ID in the MANAGER table. EMPLOYEES (WORKER) EMPLOYEES (MANAGER) … …
  • 16. Copyright © 2004, Oracle. All rights reserved. Self-Joins Using the ON Clause SELECT e.last_name emp, m.last_name mgr FROM employees e JOIN employees m ON (e.manager_id = m.employee_id); …
  • 17. Copyright © 2004, Oracle. All rights reserved. SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e JOIN departments d ON (e.department_id = d.department_id) AND e.manager_id = 149 ; Applying Additional Conditions to a Join
  • 18. Copyright © 2004, Oracle. All rights reserved. SELECT employee_id, city, department_name FROM employees e JOIN departments d ON d.department_id = e.department_id JOIN locations l ON d.location_id = l.location_id; Creating Three-Way Joins with the ON Clause …
  • 19. Copyright © 2004, Oracle. All rights reserved. Non-Equijoins EMPLOYEES JOB_GRADES Salary in the EMPLOYEES table must be between lowest salary and highest salary in the JOB_GRADES table. …
  • 20. Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, e.salary, j.grade_level FROM employees e JOIN job_grades j ON e.salary BETWEEN j.lowest_sal AND j.highest_sal; Retrieving Records with Non-Equijoins …
  • 21. Copyright © 2004, Oracle. All rights reserved. Outer Joins EMPLOYEESDEPARTMENTS There are no employees in department 190. …
  • 22. Copyright © 2004, Oracle. All rights reserved. INNER Versus OUTER Joins • In SQL:1999, the join of two tables returning only matched rows is called an inner join. • A join between two tables that returns the results of the inner join as well as the unmatched rows from the left (or right) tables is called a left (or right) outer join. • A join between two tables that returns the results of an inner join as well as the results of a left and right join is a full outer join.
  • 23. Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, e.department_id, d.department_name FROM employees e LEFT OUTER JOIN departments d ON (e.department_id = d.department_id) ; LEFT OUTER JOIN …
  • 24. Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, e.department_id, d.department_name FROM employees e RIGHT OUTER JOIN departments d ON (e.department_id = d.department_id) ; RIGHT OUTER JOIN …
  • 25. Copyright © 2004, Oracle. All rights reserved. SELECT e.last_name, d.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON (e.department_id = d.department_id) ; FULL OUTER JOIN …
  • 26. Copyright © 2004, Oracle. All rights reserved. Cartesian Products • 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.
  • 27. Copyright © 2004, Oracle. All rights reserved. Generating a Cartesian Product Cartesian product: 20 x 8 = 160 rows EMPLOYEES (20 rows) DEPARTMENTS (8 rows) … …
  • 28. Copyright © 2004, Oracle. All rights reserved. SELECT last_name, department_name FROM employees CROSS JOIN departments ; Creating Cross Joins • The CROSS JOIN clause produces the cross- product of two tables. • This is also called a Cartesian product between the two tables. …
  • 29. Copyright © 2004, Oracle. All rights reserved. Summary In this lesson, you should have learned how to use joins to display data from multiple tables by using: • Equijoins • Non-equijoins • Outer joins • Self-joins • Cross joins • Natural joins • Full (or two-sided) outer joins
  • 30. Copyright © 2004, Oracle. All rights reserved. Practice 5: Overview This practice covers the following topics: • Joining tables using an equijoin • Performing outer and self-joins • Adding conditions