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 OCT OCT-008 … NOV-001 CUSTOM ER_ID CUSTOMER_NAM E …POSTAL_ CODE 1Contemporary Casuals Value Furniture75094 … 15Mountain Scenes84403 CUSTOMER_TORDER_T ORDER_IDORDER_DATECUSTOMER_IDCUSTOMER_NAMEPOSTAL_CODE OCT-001Contemporary Casuals OCT-008California Classics96915 …… 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 KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES WARD FORD SMITH rows selected. DEPTNO DNAME LOC ACCOUNTING NEW YORK 30 SALES CHICAGO 10 ACCOUNTINGNEW YORK 20 RESEARCHDALLAS 30 SALES CHICAGO 20 RESEARCHDALLAS 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 KING BLAKE CLARK JONES MARTIN ALLEN TURNER JAMES rows selected. GRADE LOSAL HISAL

12 Retrieving Records with Non-Equijoins ENAME SAL GRADE JAMES SMITH ADAMS 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 KING 7698BLAKE CLARK JONES MARTIN ALLEN7698 EMPNOENAME KING 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 rows selected. WORKER.ENAME||'WORKSFOR'||MANAG BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE 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