Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins.

Similar presentations


Presentation on theme: "Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins."— Presentation transcript:

1 Multiple Table Queries (Inner Joins) Week 3

2 Objective –Write SELECT statements to display data from more than one table using inner joins

3 NAME DEPTID... IT 60 Sales 80 Executive 90... NAME DEPTID... IT 60 Sales 80 Executive 90... EMPLOYEES DEPARTMENTS ID NAME DEPTID 100 King90 101 Kochhar 90 102 De Haan 90 103 Hunold60... Related Tables Foreign key Primary key In queries data from several related tables may often have to be shown. These tables should be related through common data: the primary key of one table is often the foreign key of a related table.

4 ID DEPTIDNAME 100 90 Executive 101 90Executive 102 90Executive 103 60IT... ID DEPTIDNAME 100 90 Executive 101 90Executive 102 90Executive 103 60IT... EMPLOYEES DEPARTMENTS ID NAME DEPTID 100 King90 101 Kochhar 90 102 De Haan 90 103 Hunold60... NAME DEPTID... IT 60 Sales 80 Executive 90... Display Data from Related Tables Rows of output containing information from both tables are to be displayed

5 Inner Joins Use inner join to display data from each row of one table with a related row in another table Joins are normally made between primary key column(s) and related foreign key column(s) Join condition(s) specified in FROM clause If the only common columns between the 2 tables are the ones used in the join condition then can use NATURAL JOIN If column names are the same in the 2 tables then use JOIN … USING (ColumnName) If column names are different in the 2 tables use JOIN … ON (Column1Name = column2Name)

6 Inner Join with USING Syntax SELECT column1t1, column3t1, column1t2 FROM table1 JOIN table2 USING (column3)  Assuming that column3 exists as a column in both tables and is both a foreign key in table 1 and the primary key of table2 Example: SELECT employee_id ID, department_id DEPTID, department_name NAME FROM employees JOIN departments USING (department_id) ORDER BY employee_id

7 Inner Join with ON Syntax SELECT t1.column1t1, t1.column3, t2.column1 FROM table1 t1 JOIN table2 t2 ON (t1.column3 = t2.column1)  Assuming that column3 in table 1 is a foreign key referencing column 1 of table 2 which is the primary key of table2 Example: SELECT subjcode, sectcode,instrno, lastname FROM section JOIN instructor ON (section.instrnum = instructor.instrno)

8 INNER NATURAL Join SELECT column1t1, column3t1, column1t2 FROM table1 NATURAL JOIN table2  Assuming that column3 is the only commonly named column in both tables and is both a foreign key in table 1 and the primary key of table2 Example: SELECT department_id, department_name, location_id, city FROM departments NATURAL JOIN locations

9 ID NAME DEPTID 100King90 101Kochhar 90 102De Haan 90 103Hunold60... ID NAME DEPTID 100King90 101Kochhar 90 102De Haan 90 103Hunold60... EMPLOYEES ID NAMELOCATION_ID 10 Administration1700 20 Marketing 1800 50 Shipping 1500 60 IT1400... ID NAMELOCATION_ID 10 Administration1700 20 Marketing 1800 50 Shipping 1500 60 IT1400...DEPARTMENTS CITY LOCATION_ID Roma 1000 Venice 1100 Tokyo 1200 Hiroshima1300 Southlake 1400... CITY LOCATION_ID Roma 1000 Venice 1100 Tokyo 1200 Hiroshima1300 Southlake 1400... How many join conditions needed? Joining More Than Two Tables LOCATIONS

10 SELECT employee_id ID, department_name NAME, city FROM employees JOIN departments USING (department_id) NATURAL JOIN locations ORDER BY employee_id


Download ppt "Multiple Table Queries (Inner Joins) Week 3. Objective –Write SELECT statements to display data from more than one table using inner joins."

Similar presentations


Ads by Google