multiple table queries (inner joins, equijoins) week 3

16
Queries (Inner Joins, Equijoins) Week 3

Upload: melissa-bryan

Post on 13-Dec-2015

216 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Multiple Table Queries (Inner Joins, Equijoins)

Week 3

Page 2: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Objective

– Write SELECT statements to display data from more than one table using equijoins (i.e. equality joins, also known as simple or inner joins)

Page 3: Multiple Table Queries (Inner Joins, Equijoins) Week 3

EMPLOYEESEMPLOYEES DEPARTMENTS DEPARTMENTS

EMP_NO ENAME .. DEPT_NO 101 Cohen ... 30 102 Huang ... 30 103 Smith ... 20 . . . 114 Miller ... 10

DEPT_NO DEPT_NAME LOCN 10 Accounting TORONTO 20 Research OTTAWA 30 Sales Toronto 40 Operations Markham 60 Service Toronto

Related Tables

Foreign keyForeign key Primary keyPrimary 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 another table.

Page 4: Multiple Table Queries (Inner Joins, Equijoins) Week 3

EMP_NO DEPT_NO LOCN----- ------- -------- 101 30 Toronto 102 30 Toronto 103 20 OTTAWA... 114 10 TORONTO 14 rows selected.

EMP_NO DEPT_NO LOCN----- ------- -------- 101 30 Toronto 102 30 Toronto 103 20 OTTAWA... 114 10 TORONTO 14 rows selected.

EMPLOYEESEMPLOYEES DEPARTMENTS DEPARTMENTS

EMP_NO ENAME .. DEPT_NO 101 Cohen ... 30 102 Huang ... 30 103 Smith ... 20 . . . 114 Miller ... 10

DEPT_NO DEPT_NAME LOCN 10 Accounting TORONTO 20 Research OTTAWA 30 Sales Toronto 40 Operations Markham 60 Service Toronto

Display Data from Related Tables

14 rows of output are to be displayed - one row for each employee

Page 5: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Equijoins Use an equijoin to display data from each row

of one table with a related row in another table

Joins are normally made between primary key columns and related foreign key columns

Use = between related columns to join rows Join condition(s) specified in WHERE clause Good to prefix all column names with the

table name for clarity and efficiency Must prefix the column name with the table

name when the same column name appears in more than one of the tables

Page 6: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Equijoin SyntaxSELECT table1.column1, table1.column3, table2.column4

FROM table1, table2

WHERE table1.column3 = table2.column1

Assuming that column1 is the primary key of table2 and column 3 in table 1 contains the same data and is a foreign key

Page 7: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Equijoin Example

SELECT employees.emp_no, employees.dept_no, departments.locn

FROM employees, departments

WHERE employees.dept_no = departments.dept_no

Page 8: Multiple Table Queries (Inner Joins, Equijoins) Week 3

What if you forget to use a Join Condition?

If a join condition between tables is omitted in a multi-table query then every row from the first table will be joined with every row from the second table

If table 1 has m rows and table 2 has n rows then result will have m*n rows

Result is called a Cartesian Product Result is normally meaningless

Page 9: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Cartesian Product Example

SELECT employees.emp_no, employees.dept_no, departments.locn

FROM employees, departments

( this query is missing a join condition between dept_no of employees table and dept_no of departments table)

WRONG…….WRONG………

WRONG…….WRONG………

WRONG…….WRONG………

WRONG…….WRONG………

Page 10: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Cartesian Product Example Result

EMP_NO DEPT_NO LOCN----- ------- -------- 101 30 TORONTO 102 30 TORONTO 103 20 TORONTO... 114 10 Toronto 70 rows selected.

EMP_NO DEPT_NO LOCN----- ------- -------- 101 30 TORONTO 102 30 TORONTO 103 20 TORONTO... 114 10 Toronto 70 rows selected.

EMPLOYEES (14 rows)EMPLOYEES (14 rows) DEPARTMENTS (5 rows) DEPARTMENTS (5 rows) EMP_NO ENAME .. DEPT_NO 101 Cohen ... 30 102 Huang ... 30 103 Smith ... 20 . . . 114 Miller ... 10

DEPT_NO DEPT_NAME LOCN 10 Accounting TORONTO 20 Research OTTAWA 30 Sales Toronto 40 Operations Markham 60 Service Toronto

70 rows of output are produced but there are only 14 employees, and each employee will be associated with all locations!

Page 11: Multiple Table Queries (Inner Joins, Equijoins) Week 3

How to Avoid Cartesian Product–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 in a WHERE clause.

Page 12: Multiple Table Queries (Inner Joins, Equijoins) Week 3

NAME CUSTID----------- ------JOCKSPORTS 100TKB SPORT SHOP 101VOLLYRITE 102JUST TENNIS 103K+T SPORTS 105SHAPE UP 106WOMENS SPORTS 107... ...9 rows selected.

NAME CUSTID----------- ------JOCKSPORTS 100TKB SPORT SHOP 101VOLLYRITE 102JUST TENNIS 103K+T SPORTS 105SHAPE UP 106WOMENS SPORTS 107... ...9 rows selected.

CUSTOMER CUSTOMER

CUSTID ORDID------- ------- 101 610 102 611 104 612 106 601 102 602 106 604 106 605... 21 rows selected.

CUSTID ORDID------- ------- 101 610 102 611 104 612 106 601 102 602 106 604 106 605... 21 rows selected.

ORD ORD

ORDID ITEMID------ ------- 610 3 611 1 612 1 601 1 602 1...64 rows selected.

ORDID ITEMID------ ------- 610 3 611 1 612 1 601 1 602 1...64 rows selected.

ITEM ITEM

How many join conditions needed? How many rows of output could be produced?

How many join conditions needed? How many rows of output could be produced?

Joining More Than Two Tables

Page 13: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Joining More Than Two Tables

SELECT customer.name, ord.ordid, item.ordid, item.itemid

FROM customer, ord, item

WHERE customer.custid = ord.custid AND ord.ordid = item.ordid

Page 14: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Number of Equijoin Conditions Required

To join n tables together, you need a minimum of (n-1) join conditions.

Therefore, to join three tables, a minimum of two joins are required.

However if data is being related by a concatenated primary key then more than (n-1) joins will be required.

Page 15: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Table Aliases Use table aliases instead of lengthy table names.

Table aliases are defined in the FROM clause: the table name is specified, followed by a space and then the table alias.

Table alias is only valid for the SELECT statement in which it is defined.

If a table alias is defined in the FROM clause then that table alias must be substituted for the table name throughout the SELECT statement.

Page 16: Multiple Table Queries (Inner Joins, Equijoins) Week 3

Table Alias Example

SELECT e.emp_no, e.dept_no, d.locn

FROM employees e, departments d

WHERE e.dept_no = d.dept_no