Upload
honey
View
40
Download
0
Embed Size (px)
DESCRIPTION
Displaying Data from Multiple Tables. 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 - PowerPoint PPT Presentation
Citation preview
Displaying Data from Multiple Tables
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 joinsView data that generally does not meet a join condition by using outer joinsJoin a table to itself
employee_nbr dept_nbr location------------ -------- -------- 7839 10 New York 7698 30 Chicago 7782 10 New York 7566 20 Dallas 7654 30 Chicago 7499 30 Chicago...14 rows selected.
Obtaining Data from Multiple TablesEmployee Department employee_nbr name ... dept_nbr------ ------ ... ------ 7839 King ... 10 7698 Blake ... 30 ... 7934 Miller ... 10
dept_nbr dept_name location -------- --------- -------- 10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston
What Is a Join?
Use a join to query data from more than one table.
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.
SELECT table1.column, table2.columnFROM table1, table2WHERE table1.column1 = table2.column2;
Cartesian Product
A Cartesian product is formed when:A join condition is omittedA join condition is invalidAll 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.
Generating a Cartesian Product
name dept_name------ ----------King AccountingBlake Accounting ...King ResearchBlake Research...56 rows selected.
Employee (14 rows) Department(4 rows) employee_nbr name ...dept_nbr----------- ----- ...-------- 7839 King ... 10 7698 Blake ... 30 ... 7934 Miller... 10
dept_nbr dept_name location -------- ------- -------- 10 Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston
“Cartesianproduct:
14*4=56 rows”
Types of Joins
Equijoin Non-equijoin Outer join Self join
What Is an Equijoin?Employee Department
employee_nbr name dept_nbr ------------ ---- -------- 7839 King 10 7698 Blake 30 7782 Clark 10 7566 Jones 20 7654 Martin 30 7499 Allen 30 7844 Turner 30 7900 James 30 7521 Ward 30 7902 Ford 20 7369 Smith 20 ... 14 rows selected.
dept_nbr dept_name location -------- ---------- -------- 10 Accounting New York 30 Sales Chicago 10 Accounting New York 20 Research Dallas 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 20 Research Dallas 20 Research Dallas...14 rows selected.
Foreign key Primary key
Retrieving Records with Equijoins
MySQL> SELECT employee.employee_nbr,employee.name, -> employee.dept_nbr, -> dept.dept_nbr, dept.location -> FROM employee, dept -> WHERE employee.dept_nbr=dept.dept_nbr;
employee_nbr name dept_nbr dept_nbr location----------- ------- -------- -------- ---------- 7839 King10 10 New York 7698 Blake 30 30 Chicago 7782 Clark 10 10 New York 7566 Jones 20 20 Dallas...14 rows selected.
Inner Join Syntax
The Keyword INNER is optional
MySQL> SELECT employee_nbr, name, -> department.dept_nbr, location -> FROM employee [INNER] JOIN department -> ON employee.dept_nbr = department.dept_nbr;
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.
Additional Search ConditionsUsing the AND Operator
Employee Department employee_nbr name dept_nbr------------ ---- -------- 7839 King 10 7698 Blake 30 7782 Clark 10 7566 Jones 20 7654 Martin 30 7499 Allen 30 7844 Turner 30 7900 James 30 7521 Ward 30 7902 Ford 20 7369 Smith 20...14 rows selected.
dept_nbr dept_name location -------- --------- -------- 10 Accounting New York 30 Sales Chicago 10 Accounting New York 20 Research Dallas 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 30 Sales Chicago 20 Research Dallas 20 Research Dallas...14 rows selected.
Using AND with Composite Keys
If the Primary key is a composite key use the AND operator in the ON clause to set the composite key columns in equal to the foreign key columns.
MySQL> SELECT customer_case_id, orders.order_id, -> payment_id, payment_amt -> FROM orders INNER JOIN payments -> ON orders.cust_id = payments.cust_id -> AND orders.order_id = payments_order_id;
Using Table Aliases
Simplify queries by using table aliases.
MySQL> SELECT employee.employee_nbr,employee.name, -> employee.dept_nbr, -> dept.dept_nbr, dept.location -> FROM employee, dept -> WHERE employee.dept_nbr = dept.dept_nbr;
MySQL> SELECT employee_nbr, e.name, e.dept_nbr, -> d.dept_nbr, d.location -> FROM employee e, dept d -> WHERE e.dept_nbr = d.dept_nbr;
Joining More Than Two TablesName cust_id----------- -------Jocksports 100TKB Sport Shop 101Vollyrite 102Just Tennis 103K+T Sports 105Shape Up 106Women's Sports 107... ...9 rows selected.
customer cust_id order_id------- -------- 101 610 102 611 104 612 106 601 102 602 106 604 106 605 ... 21 rows selected.
orders
order_id item_ID ------ ------- 610 3 611 1 612 1 601 1 602 1 ...64 rows selected.
item
cust_id order_id payment_amt------- -------- ----------- 100 610 1050 103 601 1230 106 604 1155
payments
Joining More Than Two Tables
If you have N tables to join you will need at least N-1 join statements
MySQL> SELECT customer.cust_id, name, -> orders.order_id, item_id -> FROM customer JOIN orders -> ON customer.cust_id = orders.cudt_id -> JOIN item -> ON orders.order_id = item.order_id;
Non-EquijoinsEmployee Salary_gradeemployee_nbr name salary----------- ----- ---- 7839 King 5000 7698 Blake 2850 7782 Clark 2450 7566 Jones 2975 7654 Martin 1250 7499 Allen 1600 7844 Turner 1500 7900 James 950...14 rows selected.
Grade low_salary high_salary----- ---------- -----------1 700 12002 1201 14003 1401 20004 2001 30005 3001 9999
“salary in the employee table is between low salary and high salary in the salary_gradetable”
Retrieving Records with Non-Equijoins
name salary grade---------- --------- ---------James 950 1Smith 800 1Adams 1100 1...14 rows selected.
MySQL> SELECT name, salary, grade -> FROM employee, salary_grade -> WHERE salary -> BETWEEN low_salary AND high_salary;
Outer Joins
Employee Department
No employee in theOperations department
name dept_nbr----- ------King 10Blake 30Clark 10Jones 20...
dept_nbr dept_name------ ---------10 Accounting30 Sales10 Accounting20 Research...40 Operations
Outer JoinsYou use an outer join to also see rows that do not usually meet the join condition.Outer joins require the keyword LEFT or RIGHT.RIGHT or LEFT keyword specify the table from which the missing data is to come The only difference between a RIGHT and LEFT outer join ... ... ...
SELECT table1.column, table2.columnFROM table1 LEFT [OUTER] JOIN table2ON table1.column = table2.column;
SELECT table1.column, table2.columnFROM table1 RIGHT [OUTER] JOIN table2ON table1.column = table2.column;
Using Outer Joins
MySQL> SELECT name, department.dept_nbr, dept_name -> FROM employee RIGHT OUTER JOIN department -> ON employee.dept_nbr = department.dept_nbr -> ORDER BY employee.dept_nbr;
name dept_nbr dept_name--------- --------- -------------King 10 AccountingClark 10 Accounting... 40 Operations15 rows selected.
Self Joins
Employee(Worker) Employee (Manager)
“MGR in the Worker table is equal to employee_nbr in the Manager table”
employee_nbr name MGR------------ ---- --- 7839 King 7698 Blake 7839 7782 Clark 7839 7566 Jones 7839 7654 Martin 7698 7499 Allen 7698
employee_nbr name------------ ----
7839 King 7839 King 7839 King 7698 Blake 7698 Blake
Joining a Table to Itself
WORKER.name||'WORKSFOR'||MANAG-------------------------------Blake works for KingClark works for KingJones works for KingMartin works for Blake...13 rows selected.
MySQL> SELECT worker.name||' works for '||manager.name -> FROM employee worker INNER JOIN employee manager -> ON worker.manager = manager.employee_nbr;
Summary
Equijoin Non-equijoin Outer join Self join
SELECT table1.column, table2.columnFROM table1, table2WHERE table1.column1 = table2.column2;