Presentation is loading. Please wait.

Presentation is loading. Please wait.

Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following:  Write SELECT statements to access.

Similar presentations


Presentation on theme: "Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following:  Write SELECT statements to access."— Presentation transcript:

1 Displaying Data from Multiple Tables

2 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  View data that generally does not meet a join condition by using outer joins  Join a table to itself

3 Obtaining Data from Multiple Tables ORDER_IDORDER_DATECUSTOMER_I D 100121-OCT-001 100221-OCT-008 … 101005-NOV-001 CUSTOM ER_ID CUSTOMER_NAM E …POSTAL_ CODE 1Contemporary Casuals 32601 2Value Furniture75094 … 15Mountain Scenes84403 CUSTOMER_TORDER_T ORDER_IDORDER_DATECUSTOMER_IDCUSTOMER_NAMEPOSTAL_CODE 100121-OCT-001Contemporary Casuals 32601 100221-OCT-008California Classics96915 …… 101005-NOV-001Contemporary Casuals 32601

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

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

6 Types of Joins EquijoinNon-EquijoinOuter-joinSelf-join

7 What Is an Equijoin? EMP DEPT EMPNO ENAME DEPTNO ------ ------- ------- 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. DEPTNO DNAME LOC ------- ---------- -------- 10 ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 20 RESEARCHDALLAS... 14 rows selected. Primary key Foreign key

8 Retrieving Records with Equijoins SELECT order_t.order_id, order_t.order_date, order_t.customer_id, customer_t.customer_id, customer_t.customer_name FROM order_t, customer_t WHERE order_t.customer_id=customer_t.customer_id

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

10 Using Table Aliases Simplify queries using table aliases SELECT order_t.order_id, order_t.order_date, order_t.customer_id, customer_t.customer_id, customer_t.customer_name FROM order_t, customer_t WHERE order_t.customer_id=customer_t.customer_id SELECT o.order_id, o.order_date, o.customer_id, c.customer_id, c.customer_name FROM order_t o, customer_t c WHERE o.customer_id=c.customer_id

11 Non-Equijoins EMPSALGRADE “salary in the EMP table is between low salary and high salary in the SALGRADE table” EMPNO ENAME SAL ------ ------- ------ 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 LOSAL HISAL ----- ----- ------ 1 7001200 2 12011400 3 14012000 420013000 5 30019999

12 Retrieving Records with Non-Equijoins ENAME SAL GRADE ---------- --------- --------- JAMES 950 1 SMITH 800 1 ADAMS 1100 1... 14 rows selected. SQL> SELECT e.ename, e.sal, s.grade 2FROMemp e, salgrade s 3WHERE e.sal 4BETWEEN s.losal AND s.hisal;

13 Outer Joins You use an outer join to also see rows that do not usually meet the join condition. SELECTtable.column, table.column FROMtable1 LEFT/RIGHT JOIN table2 ON(table1.column = table2.column); SELECTtable.column, table.column FROMtable1 LEFT/RIGHT JOIN table2 ON(table1.column = table2.column);

14 Using Outer Joins SELECTo.order_id, c.customer_name FROMorder_t o LEFT JOIN customer_t c ON(o.customer_id = c.customer_id); SELECTo.order_id, c.customer_name FROMorder_t o LEFT JOIN customer_t c ON(o.customer_id = c.customer_id); SELECTo.order_id, c.customer_name FROMorder_t o RIGHT JOIN customer_t c ON(o.customer_id = c.customer_id); SELECTo.order_id, c.customer_name FROMorder_t o RIGHT JOIN customer_t c ON(o.customer_id = c.customer_id);

15 Self Joins EMP (WORKER) EMP (MANAGER) "MGR in the WORKER table is equal to EMPNO in the MANAGER table" EMPNOENAME MGR --------------- 7839KING 7698BLAKE7839 7782CLARK7839 7566JONES7839 7654MARTIN7698 7499ALLEN7698 EMPNOENAME ------------- 7839KING 7698BLAKE

16 Joining a Table to Itself WORKER.ENAME||'WORKSFOR'||MANAG ------------------------------- BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE... 13 rows selected. WORKER.ENAME||'WORKSFOR'||MANAG ------------------------------- BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE... 13 rows selected. SQL> SELECT worker.ename||' works for '||manager.ename 2 FROM emp worker, emp manager 3 WHERE worker.mgr = manager.empno;

17 Summary SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; SELECTtable1.column, table2.column FROMtable1, table2 WHEREtable1.column1 = table2.column2; EquijoinNon-EquijoinOuter-joinSelf-join


Download ppt "Displaying Data from Multiple Tables. Objectives After completing this lesson, you should be able to do the following:  Write SELECT statements to access."

Similar presentations


Ads by Google