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 ------------ -------- -------- 7839 10 New York 7698 30 Chicago 7782 10 New York 7566 20 Dallas 7654 30 Chicago 7499 30 Chicago... 14 rows selected. 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. EmployeeDepartment 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

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... 56 rows selected. name dept_name ------ ---------- King Accounting Blake Accounting... King Research Blake Research... 56 rows selected. Employee (14 rows)Department(4 rows) employee_nbr name...dept_nbr ----------- -----...-------- 7839 King... 10 7698 Blake... 30... 7934 Miller... 10 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 dept_nbr dept_name location -------- ------- -------- 10 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 ------------ ---- -------- 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 20 Research Dallas... 14 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 ----------- ------- -------- -------- ---------- 7839King10 10 New York 7698 Blake 30 30 Chicago 7782Clark10 10 New York 7566 Jones20 20 Dallas... 14 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 ------------ ---- -------- 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 20 Research Dallas... 14 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 107... 9 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 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. 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. 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 cust_id order_id payment_amt ------- -------- ----------- 100 610 1050 103 601 1230 106 604 1155 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 ----------- ----- ---- 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 1200 2 1201 1400 3 1401 2000 4 2001 3000 5 3001 9999 “salary in the employee table is between low salary and high salary in the salary_grade table”

18 name salary grade ---------- --------- --------- James 950 1 Smith 800 1 Adams 1100 1... 14 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 20... dept_nbr dept_name ------ --------- 10 Accounting 30 Sales 10 Accounting 20 Research... 40 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... 40 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 ---------------- --- 7839King 7698Blake7839 7782Clark7839 7566Jones7839 7654Martin7698 7499Allen7698 employee_nbrname ---------------- 7839King 7698Blake

23 WORKER.name||'WORKSFOR'||MANAG ------------------------------- Blake works for King Clark works for King Jones works for King Martin works for Blake... 13 rows selected. WORKER.name||'WORKSFOR'||MANAG ------------------------------- Blake works for King Clark works for King Jones works for King Martin 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;

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