Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1

2 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

3 employee_nbr dept_nbr location New York Chicago New York Dallas Chicago Chicago rows selected. employee_nbr dept_nbr location New York Chicago New York Dallas Chicago Chicago rows selected. EmployeeDepartment employee_nbr name... dept_nbr King Blake Miller dept_nbr dept_name location Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston

4 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. SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;

5 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.

6 name dept_name King Accounting Blake Accounting... King Research Blake Research rows selected. name dept_name King Accounting Blake Accounting... King Research Blake Research rows selected. Employee (14 rows)Department(4 rows) employee_nbr name...dept_nbr King Blake Miller employee_nbr name...dept_nbr King Blake Miller dept_nbr dept_name location Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston dept_nbr dept_name location Accounting New York 20 Research Dallas 30 Sales Chicago 40 Operations Boston “Cartesian product: 14*4=56 rows”

7 Equijoin Non-equijoin Outer join Self join

8 Employee Department employee_nbr name dept_nbr King Blake Clark Jones Martin Allen Turner James Ward Ford Smith rows selected. dept_nbr dept_name location Accounting New York 30 Sales Chicago 10 Accounting New York 20 Research Dallas 30 Sales Chicago 20 Research Dallas rows selected. Foreign keyPrimary key

9 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 King10 10 New York 7698 Blake Chicago 7782Clark10 10 New York 7566 Jones20 20 Dallas rows selected.

10 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;

11 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.

12 EmployeeDepartment employee_nbr name dept_nbr King Blake Clark Jones Martin Allen Turner James Ward Ford Smith rows selected. dept_nbr dept_name location Accounting New York 30 Sales Chicago 10 Accounting New York 20 Research Dallas 30 Sales Chicago 20 Research Dallas rows selected.

13 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;

14 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;

15 Namecust_id Jocksports 100 TKB Sport Shop 101 Vollyrite 102 Just Tennis 103 K+T Sports 105 Shape Up 106 Women's Sports rows selected. Namecust_id Jocksports 100 TKB Sport Shop 101 Vollyrite 102 Just Tennis 103 K+T Sports 105 Shape Up 106 Women's Sports rows selected. customer cust_id order_id rows selected. cust_id order_id rows selected. orders order_id item_ID rows selected. order_id item_ID rows selected. item cust_id order_id payment_amt cust_id order_id payment_amt payments

16 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;

17 EmployeeSalary_grade employee_nbr name salary King Blake Clark Jones Martin Allen Turner James rows selected. Grade low_salary high_salary “salary in the employee table is between low salary and high salary in the salary_grade table”

18 name salary grade James Smith Adams rows selected. MySQL> SELECT name, salary, grade -> FROMemployee, salary_grade -> WHERE salary -> BETWEEN low_salary AND high_salary;

19 Employee Department No employee in the Operations department name dept_nbr King 10 Blake 30 Clark 10 Jones dept_nbr dept_name Accounting 30 Sales 10 Accounting 20 Research Operations

20 You 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 SELECTtable1.column, table2.column FROMtable1 LEFT [OUTER] JOIN table2 ONtable1.column = table2.column; SELECTtable1.column, table2.column FROMtable1 LEFT [OUTER] JOIN table2 ONtable1.column = table2.column; SELECTtable1.column, table2.column FROMtable1 RIGHT [OUTER] JOIN table2 ONtable1.column = table2.column; SELECTtable1.column, table2.column FROMtable1 RIGHT [OUTER] JOIN table2 ONtable1.column = table2.column;

21 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 Accounting Clark 10 Accounting Operations 15 rows selected.

22 Employee(Worker)Employee (Manager) “MGR in the Worker table is equal to employee_nbr in the Manager table” employee_nbrname MGR King 7698Blake Clark Jones Martin Allen7698 employee_nbrname King 7698Blake

23 WORKER.name||'WORKSFOR'||MANAG Blake works for King Clark works for King Jones works for King Martin works for Blake rows selected. WORKER.name||'WORKSFOR'||MANAG Blake works for King Clark works for King Jones works for King Martin works for Blake rows selected. MySQL> SELECT worker.name||' works for '||manager.name -> FROM employee worker INNER JOIN employee manager -> ON worker.manager = manager.employee_nbr;

24 Equijoin Non-equijoin Outer join Self join SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2;


Download ppt "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."

Similar presentations


Ads by Google