Multiple Table Queries (Inner Joins) Week 3
Objective –Write SELECT statements to display data from more than one table using inner joins
NAME DEPTID... IT 60 Sales 80 Executive NAME DEPTID... IT 60 Sales 80 Executive EMPLOYEES DEPARTMENTS ID NAME DEPTID 100 King Kochhar De Haan 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.
ID DEPTIDNAME Executive Executive Executive IT... ID DEPTIDNAME Executive Executive Executive IT... EMPLOYEES DEPARTMENTS ID NAME DEPTID 100 King Kochhar De Haan Hunold60... NAME DEPTID... IT 60 Sales 80 Executive Display Data from Related Tables Rows of output containing information from both tables are to be displayed
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)
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
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)
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
ID NAME DEPTID 100King90 101Kochhar De Haan Hunold60... ID NAME DEPTID 100King90 101Kochhar De Haan Hunold60... EMPLOYEES ID NAMELOCATION_ID 10 Administration Marketing Shipping IT ID NAMELOCATION_ID 10 Administration Marketing Shipping IT DEPARTMENTS CITY LOCATION_ID Roma 1000 Venice 1100 Tokyo 1200 Hiroshima1300 Southlake CITY LOCATION_ID Roma 1000 Venice 1100 Tokyo 1200 Hiroshima1300 Southlake How many join conditions needed? Joining More Than Two Tables LOCATIONS
SELECT employee_id ID, department_name NAME, city FROM employees JOIN departments USING (department_id) NATURAL JOIN locations ORDER BY employee_id