SlideShare a Scribd company logo
1 of 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 ;

More Related Content

What's hot (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
 

Viewers also liked

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
 

Viewers also liked (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
 

Similar to 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
 

Similar to 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
 

More from 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
 

More from 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
 

Recently uploaded

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseAnaAcapella
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 

Recently uploaded (20)

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

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 ;