SlideShare une entreprise Scribd logo
1  sur  35
From Multiple Tables  http://ecomputernotes.com
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 by using a self join  http://ecomputernotes.com
Obtaining Data from Multiple Tables  EMPLOYEES  DEPARTMENTS  «  «  http://ecomputernotes.com
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  "T o avoid a Cartesian product, always include a  valid join condition in a   WHERE   clause.  http://ecomputernotes.com
Generating a Cartesian Product  EMPLOYEES   (20 rows)  DEPARTMENTS   (8 rows)  «  Cartesian product:  0x8=160 rows  «  http://ecomputernotes.com
Types of Joins  Oracle Proprietary  SQL: 1999  Compliant Joins:  Joins (8 ii   and prior):  "  Equijoin  "  Cross joins  "  "  Non-equijoin  Natural joins  "  "  Outer join  Using clause  "  "  Self join  Full or two sided outer  joins  "  Arbitrary join conditions  for outer joins  http://ecomputernotes.com
Joining Tables Using Oracle Syntax  Use a join to query data from more than one table.  SELECT  table1.column, table2.column  FRO M  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.  http://ecomputernotes.com
What is an Equijoin?  EMPLOYEES  DEPARTMENTS  «  «  Foreign key  Primary key  http://ecomputernotes.com
Retrieving Records  with Equijoins  SELECT employees.employee_id, employees.last_name,  employees.department_id, departments.department_id,  departments.location_id  FROM  employees, departments  WHERE  employees.department_id = departments.department_id;  «  http://ecomputernotes.com
Additional Search Conditions Using the   AND   Operator  EMPLOYEES  DEPARTMENTS  «  «  http://ecomputernotes.com
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.  http://ecomputernotes.com
Using Table Aliases  "  Simplify queries by using table aliases.  "  Improve performance by using table prefixes.  SELECT e.employee_id, e.last_name, e.department_id,  d.department_id, d.location_id  FROM  employees e , departments d WHERE  e.department_id = d.department_id;  http://ecomputernotes.com
Joining More than Two Tables  MPLOYEE  DEPARTMENTS  LOCATIONS  "T o join  n   t ables together, you need a minimum of  n-1 join conditions. For example, to join three tables, a minimum of two joins is required.  http://ecomputernotes.com
Non-Equijoins  EMPLOYEES  JOB_GRADES  Salary in the   EMPLOYEES  table must be between  «  lowest salary and highest salary in the   JOB_GRADES  table.  http://ecomputernotes.com
Retrieving Records with Non-Equijoins  SELECT e.last_name, e.salary, j.grade_level  FROM  employees e, job_grades j  WHERE  e.salary  BETWEEN j.lowest_sal AND j.highest_sal;  «  http://ecomputernotes.com
Outer Joins  DEPARTMENTS  EMPLOYEES  «  There are no employees in  department 190.  http://ecomputernotes.com
Outer Joins Syntax  "  You use an outer join to also see rows that do not  meet the join condition.  "  The Outer join operator is the plus sign (+).  SELECT   table1.column, table2.column  FRO M  table1, table2  WHERE  table1.column(+)(+)  ==   table2.column;  SELECT   table1.column, table2.column  FRO M  table1, table2  WHERE  table1.column   ==  table2.column(+);(+);  http://ecomputernotes.com
Using Outer Joins  SELECT e.last_name, e.department_id, d.department_name  FROM  employees e, departments d  WHERE  e.department_id(+) = d.department_id ;  «  http://ecomputernotes.com
Self Joins  EMPLOYEES (WORKER)  EMPLOYEES (MANAGER)  «  «  MANAGER_ID   in the   WORKER   table is equal to  EMPLOYEE_ID   in the   MANAGER   table.  http://ecomputernotes.com
Joining a Table to Itself  SELECT worker.last_name || ' works for '  || manager.last_name  FROM  employees worker, employees manager WHERE  worker.manager_id = manager.employee_id ;  «  http://ecomputernotes.com
Practice 4, Part One: Overview  This practice covers writing queries to join tables  together using Oracle syntax.  http://ecomputernotes.com
Joining Tables Using SQL: 1999 Syntax  Use a join to query data from more than one table.  SELECT  table1.column, table2.column  FRO M  table1  [CROSS JOIN   table2 ] |] |  [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 )];
Creating Cross Joins  "  The   CROSS JOIN   clause produces the cross- product of two tables.  "T his is the same as a Cartesian product between  the two tables.  SELECT last_name, department_name  FROM  employees  CROSS JOIN departments ;  «
Creating Natural Joins  "T he  N ATURAL JOIN  c lause 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.  "I f the columns having the same names have  different data types, an error is returned.
Retrieving Records with Natural Joins  SELECT department_id, department_name,  location_id, city  FROM  departments NATURAL JOIN locations ;
Creating Joins with the   USING   Clause  "I f 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.  "U se the  U SING  c lause to match only one column  when more than one column matches.  "D o not use a table name or alias in the referenced  columns.  "  The   NATURAL JOIN   and   USING   clauses are mutually exclusive.
Retrieving Records with the   USING   Clause  SELECT e.employee_id, e.last_name, d.location_id  FROM  employees e JOIN departments d USING (department_id) ;  «
Creating Joins with the   ON   Clause  "T he join condition for the natural join is basically  an equijoin of all columns with the same name.  "T o specify arbitrary conditions or specify columns  to join, the   ON   clause is used.  "T he join condition is separated from other  s earch  conditions.  "  The   ON   clause makes code easy to understand.
Retrieving Records with the   ON   Clause  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);  «
Creating Three-Way Joins with  the   ON  Clause  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;  «
INNER   Versus   OUTER   Joins  "I n SQL: 1999, the join of two tables returning only  matched rows is an inner join.  "A  join between two tables that returns the results  of the inner join as well as unmatched rows left (or  right) tables is 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.
LEFT OUTER JOIN  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) ;  «
RIGHT OUTER JOIN  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) ;  «
FULL OUTER JOIN  SELECT e.last_name, e.department_id, d.department_name FROM  employees e  FULL OUTER JOIN departments d  ON  (e.department_id = d.department_id) ;  «
Additional Conditions  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 ;

Contenu connexe

Tendances (18)

Displaying data from multiple tables
Displaying data from multiple tablesDisplaying data from multiple tables
Displaying data from multiple tables
 
Sql joins
Sql joinsSql joins
Sql joins
 
Sql joins
Sql joinsSql joins
Sql joins
 
SQL JOIN
SQL JOINSQL JOIN
SQL JOIN
 
SQL Joinning.Database
SQL Joinning.DatabaseSQL Joinning.Database
SQL Joinning.Database
 
Join sql
Join sqlJoin sql
Join sql
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
SQL UNION
SQL UNIONSQL UNION
SQL UNION
 
1 introduction to my sql
1 introduction to my sql1 introduction to my sql
1 introduction to my sql
 
Sql join
Sql  joinSql  join
Sql join
 
Sql joins inner join self join outer joins
Sql joins inner join self join outer joinsSql joins inner join self join outer joins
Sql joins inner join self join outer joins
 
Join query
Join queryJoin query
Join query
 
SQL Joins
SQL JoinsSQL Joins
SQL Joins
 
MySQL JOIN & UNION
MySQL JOIN & UNIONMySQL JOIN & UNION
MySQL JOIN & UNION
 
SQL
SQLSQL
SQL
 
Inner join and outer join
Inner join and outer joinInner join and outer join
Inner join and outer join
 
Les05
Les05Les05
Les05
 
Types Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql ServerTypes Of Join In Sql Server - Join With Example In Sql Server
Types Of Join In Sql Server - Join With Example In Sql Server
 

En vedette

LTW Electronics Certifications
LTW Electronics CertificationsLTW Electronics Certifications
LTW Electronics CertificationsSherryltw
 
Factory direction(english)
Factory direction(english)Factory direction(english)
Factory direction(english)Sherryltw
 
e computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clausee computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clauseecomputernotes
 
e computer notes - Priority queue
e computer notes - Priority queuee computer notes - Priority queue
e computer notes - Priority queueecomputernotes
 
General agreement on tariffs and trade (gatt)
General agreement on tariffs and trade (gatt)General agreement on tariffs and trade (gatt)
General agreement on tariffs and trade (gatt)Jean Tralala
 
computer notes - Const keyword
computer notes - Const keywordcomputer notes - Const keyword
computer notes - Const keywordecomputernotes
 

En vedette (6)

LTW Electronics Certifications
LTW Electronics CertificationsLTW Electronics Certifications
LTW Electronics Certifications
 
Factory direction(english)
Factory direction(english)Factory direction(english)
Factory direction(english)
 
e computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clausee computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clause
 
e computer notes - Priority queue
e computer notes - Priority queuee computer notes - Priority queue
e computer notes - Priority queue
 
General agreement on tariffs and trade (gatt)
General agreement on tariffs and trade (gatt)General agreement on tariffs and trade (gatt)
General agreement on tariffs and trade (gatt)
 
computer notes - Const keyword
computer notes - Const keywordcomputer notes - Const keyword
computer notes - Const keyword
 

Similaire à e computer notes - From multiple tables

Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Achmad Solichin
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueriesecomputernotes
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxuzmasulthana3
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementsecomputernotes
 
e computer notes - Restricting and sorting data
e computer notes -  Restricting and sorting datae computer notes -  Restricting and sorting data
e computer notes - Restricting and sorting dataecomputernotes
 
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
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankpptnewrforce
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating dataecomputernotes
 
Interacting with Oracle Database
Interacting with Oracle DatabaseInteracting with Oracle Database
Interacting with Oracle DatabaseChhom Karath
 
e computer notes - Aggregating data using group functions
e computer notes -  Aggregating data using group functionse computer notes -  Aggregating data using group functions
e computer notes - Aggregating data using group functionsecomputernotes
 
e computer notes - Using set operator
e computer notes - Using set operatore computer notes - Using set operator
e computer notes - Using set operatorecomputernotes
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server iiIblesoft
 
e computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql pluse computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql plusecomputernotes
 

Similaire à e computer notes - From multiple tables (20)

Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)Les05 (Displaying Data from Multiple Table)
Les05 (Displaying Data from Multiple Table)
 
Les04
Les04Les04
Les04
 
e computer notes - Subqueries
e computer notes - Subqueriese computer notes - Subqueries
e computer notes - Subqueries
 
Les18
Les18Les18
Les18
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
0808.pdf
0808.pdf0808.pdf
0808.pdf
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
 
e computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statementse computer notes - Writing basic sql select statements
e computer notes - Writing basic sql select statements
 
e computer notes - Restricting and sorting data
e computer notes -  Restricting and sorting datae computer notes -  Restricting and sorting data
e computer notes - Restricting and sorting data
 
Joins.ppt
Joins.pptJoins.ppt
Joins.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
 
Chinabankppt
ChinabankpptChinabankppt
Chinabankppt
 
e computer notes - Manipulating data
e computer notes - Manipulating datae computer notes - Manipulating data
e computer notes - Manipulating data
 
Interacting with Oracle Database
Interacting with Oracle DatabaseInteracting with Oracle Database
Interacting with Oracle Database
 
e computer notes - Aggregating data using group functions
e computer notes -  Aggregating data using group functionse computer notes -  Aggregating data using group functions
e computer notes - Aggregating data using group functions
 
Lab4 join - all types listed
Lab4   join - all types listedLab4   join - all types listed
Lab4 join - all types listed
 
e computer notes - Using set operator
e computer notes - Using set operatore computer notes - Using set operator
e computer notes - Using set operator
 
Ms sql server ii
Ms sql server  iiMs sql server  ii
Ms sql server ii
 
e computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql pluse computer notes - Producing readable output with i sql plus
e computer notes - Producing readable output with i sql plus
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 

Plus de ecomputernotes

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraintsecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functionsecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueriesecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objectsecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueriesecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functionsecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36ecomputernotes
 

Plus de ecomputernotes (20)

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 

Dernier

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17Celine George
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Celine George
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmStan Meyer
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research DiscourseAnita GoswamiGiri
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6Vanessa Camilleri
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...DhatriParmar
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxMichelleTuguinay1
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxlancelewisportillo
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvRicaMaeCastro1
 

Dernier (20)

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 
How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17How to Fix XML SyntaxError in Odoo the 17
How to Fix XML SyntaxError in Odoo the 17
 
Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17Tree View Decoration Attribute in the Odoo 17
Tree View Decoration Attribute in the Odoo 17
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
Oppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and FilmOppenheimer Film Discussion for Philosophy and Film
Oppenheimer Film Discussion for Philosophy and Film
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Scientific Writing :Research Discourse
Scientific  Writing :Research  DiscourseScientific  Writing :Research  Discourse
Scientific Writing :Research Discourse
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6ICS 2208 Lecture Slide Notes for Topic 6
ICS 2208 Lecture Slide Notes for Topic 6
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
Beauty Amidst the Bytes_ Unearthing Unexpected Advantages of the Digital Wast...
 
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptxDIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
DIFFERENT BASKETRY IN THE PHILIPPINES PPT.pptx
 
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptxQ4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
Q4-PPT-Music9_Lesson-1-Romantic-Opera.pptx
 
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnvESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
ESP 4-EDITED.pdfmmcncncncmcmmnmnmncnmncmnnjvnnv
 

e computer notes - From multiple tables

  • 1. From Multiple Tables http://ecomputernotes.com
  • 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 by using a self join http://ecomputernotes.com
  • 3. Obtaining Data from Multiple Tables EMPLOYEES DEPARTMENTS « « http://ecomputernotes.com
  • 4. 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 "T o avoid a Cartesian product, always include a valid join condition in a WHERE clause. http://ecomputernotes.com
  • 5. Generating a Cartesian Product EMPLOYEES (20 rows) DEPARTMENTS (8 rows) « Cartesian product: 0x8=160 rows « http://ecomputernotes.com
  • 6. Types of Joins Oracle Proprietary SQL: 1999 Compliant Joins: Joins (8 ii and prior): " Equijoin " Cross joins " " Non-equijoin Natural joins " " Outer join Using clause " " Self join Full or two sided outer joins " Arbitrary join conditions for outer joins http://ecomputernotes.com
  • 7. Joining Tables Using Oracle Syntax Use a join to query data from more than one table. SELECT table1.column, table2.column FRO M 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. http://ecomputernotes.com
  • 8. What is an Equijoin? EMPLOYEES DEPARTMENTS « « Foreign key Primary key http://ecomputernotes.com
  • 9. Retrieving Records with Equijoins SELECT employees.employee_id, employees.last_name, employees.department_id, departments.department_id, departments.location_id FROM employees, departments WHERE employees.department_id = departments.department_id; « http://ecomputernotes.com
  • 10. Additional Search Conditions Using the AND Operator EMPLOYEES DEPARTMENTS « « http://ecomputernotes.com
  • 11. 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. http://ecomputernotes.com
  • 12. Using Table Aliases " Simplify queries by using table aliases. " Improve performance by using table prefixes. SELECT e.employee_id, e.last_name, e.department_id, d.department_id, d.location_id FROM employees e , departments d WHERE e.department_id = d.department_id; http://ecomputernotes.com
  • 13. Joining More than Two Tables MPLOYEE DEPARTMENTS LOCATIONS "T o join n t ables together, you need a minimum of n-1 join conditions. For example, to join three tables, a minimum of two joins is required. http://ecomputernotes.com
  • 14. Non-Equijoins EMPLOYEES JOB_GRADES Salary in the EMPLOYEES table must be between « lowest salary and highest salary in the JOB_GRADES table. http://ecomputernotes.com
  • 15. Retrieving Records with Non-Equijoins SELECT e.last_name, e.salary, j.grade_level FROM employees e, job_grades j WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal; « http://ecomputernotes.com
  • 16. Outer Joins DEPARTMENTS EMPLOYEES « There are no employees in department 190. http://ecomputernotes.com
  • 17. Outer Joins Syntax " You use an outer join to also see rows that do not meet the join condition. " The Outer join operator is the plus sign (+). SELECT table1.column, table2.column FRO M table1, table2 WHERE table1.column(+)(+) == table2.column; SELECT table1.column, table2.column FRO M table1, table2 WHERE table1.column == table2.column(+);(+); http://ecomputernotes.com
  • 18. Using Outer Joins SELECT e.last_name, e.department_id, d.department_name FROM employees e, departments d WHERE e.department_id(+) = d.department_id ; « http://ecomputernotes.com
  • 19. Self Joins EMPLOYEES (WORKER) EMPLOYEES (MANAGER) « « MANAGER_ID in the WORKER table is equal to EMPLOYEE_ID in the MANAGER table. http://ecomputernotes.com
  • 20. Joining a Table to Itself SELECT worker.last_name || ' works for ' || manager.last_name FROM employees worker, employees manager WHERE worker.manager_id = manager.employee_id ; « http://ecomputernotes.com
  • 21. Practice 4, Part One: Overview This practice covers writing queries to join tables together using Oracle syntax. http://ecomputernotes.com
  • 22. Joining Tables Using SQL: 1999 Syntax Use a join to query data from more than one table. SELECT table1.column, table2.column FRO M table1 [CROSS JOIN table2 ] |] | [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 )];
  • 23. Creating Cross Joins " The CROSS JOIN clause produces the cross- product of two tables. "T his is the same as a Cartesian product between the two tables. SELECT last_name, department_name FROM employees CROSS JOIN departments ; «
  • 24. Creating Natural Joins "T he N ATURAL JOIN c lause 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. "I f the columns having the same names have different data types, an error is returned.
  • 25. Retrieving Records with Natural Joins SELECT department_id, department_name, location_id, city FROM departments NATURAL JOIN locations ;
  • 26. Creating Joins with the USING Clause "I f 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. "U se the U SING c lause to match only one column when more than one column matches. "D o not use a table name or alias in the referenced columns. " The NATURAL JOIN and USING clauses are mutually exclusive.
  • 27. Retrieving Records with the USING Clause SELECT e.employee_id, e.last_name, d.location_id FROM employees e JOIN departments d USING (department_id) ; «
  • 28. Creating Joins with the ON Clause "T he join condition for the natural join is basically an equijoin of all columns with the same name. "T o specify arbitrary conditions or specify columns to join, the ON clause is used. "T he join condition is separated from other s earch conditions. " The ON clause makes code easy to understand.
  • 29. Retrieving Records with the ON Clause 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); «
  • 30. Creating Three-Way Joins with the ON Clause 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; «
  • 31. INNER Versus OUTER Joins "I n SQL: 1999, the join of two tables returning only matched rows is an inner join. "A join between two tables that returns the results of the inner join as well as unmatched rows left (or right) tables is 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.
  • 32. LEFT OUTER JOIN 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) ; «
  • 33. RIGHT OUTER JOIN 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) ; «
  • 34. FULL OUTER JOIN SELECT e.last_name, e.department_id, d.department_name FROM employees e FULL OUTER JOIN departments d ON (e.department_id = d.department_id) ; «
  • 35. Additional Conditions 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 ;