470 likes | 488 Views
Displaying Data from Multiple Tables (Join). Obtaining Data from Multiple Tables. EMP. DEPT. EMPNO ENAME ... DEPTNO ------ ----- ... ------ 7839 KING ... 10 7698 BLAKE ... 30 ... 7934 MILLER ... 10. DEPTNO DNAME LOC ------ ---------- --------
E N D
Obtaining Data from Multiple Tables EMP DEPT EMPNO ENAME ... DEPTNO------ ----- ... ------ 7839 KING ... 10 7698 BLAKE ... 30 ... 7934 MILLER ... 10 DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON EMPNO DEPTNO LOC ----- ------- -------- 7839 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLAS 7654 30 CHICAGO 7499 30 CHICAGO ... 14 rows selected.
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.column FROM table1, table2 WHERE table1.column1 = table2.column2;
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.
“Cartesianproduct: 14*4=56 rows” Generating a Cartesian Product EMP (14 rows) DEPT (4 rows) EMPNO ENAME ... DEPTNO------ ----- ... ------ 7839 KING ... 10 7698 BLAKE ... 30 ... 7934 MILLER ... 10 DEPTNO DNAME LOC ------ ---------- -------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON ENAME DNAME ------ ---------- KING ACCOUNTING BLAKE ACCOUNTING ... KING RESEARCH BLAKE RESEARCH ... 56 rows selected.
Types of Joins • Equijoin • Non-equijoin • Outer join • Self join
Foreign key Primary key 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 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.
Retrieving Records with Equijoins SQL> SELECT emp.empno, emp.ename, emp.deptno, 2 dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; EMPNO ENAME DEPTNO DEPTNO LOC ----- ------ ------ ------ --------- 7839 KING 10 10 NEW YORK 7698 BLAKE 30 30 CHICAGO 7782 CLARK 10 10 NEW YORK 7566 JONES 20 20 DALLAS ... 14 rows selected.
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 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 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 Table Aliases • Simplify queries by using table aliases. SQL> SELECT emp.empno, emp.ename, emp.deptno, 2 dept.deptno, dept.loc 3 FROM emp, dept 4 WHERE emp.deptno=dept.deptno; SQL> SELECT e.empno, e.ename, e.deptno, 2 d.deptno, d.loc 3 FROM emp e, dept d 4 WHERE e.deptno= d.deptno;
ORD CUSTID ORDID ------- ------- 101 610 102 611 104 612 106 601 102 602 106 604 106 605 ... 21 rows selected. ITEM ORDID ITEMID ------ ------- 610 3 611 1 612 1 601 1 602 1 ... 64 rows selected. Joining More Than Two Tables CUSTOMER NAME CUSTID ----------- ------ JOCKSPORTS 100 TKB SPORT SHOP 101 VOLLYRITE 102 JUST TENNIS 103 K+T SPORTS 105 SHAPE UP 106 WOMENS SPORTS 107 ... ... 9 rows selected.
“salary in the EMP table is between low salary and high salary in the SALGRADE table” Non-Equijoins EMP SALGRADE 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 700 1200 2 1201 1400 3 1401 2000 4 2001 3000 5 3001 9999
Retrieving Records with Non-Equijoins SQL> SELECT e.ename, e.sal, s.grade 2 FROM emp e, salgrade s 3 WHERE e.sal 4 BETWEEN s.losal AND s.hisal; ENAME SAL GRADE ---------- --------- --------- JAMES 950 1 SMITH 800 1 ADAMS 1100 1 ... 14 rows selected.
No employee in theOPERATIONS department Outer Joins EMP DEPT ENAME DEPTNO----- ------KING 10 BLAKE 30 CLARK 10 JONES 20 ... DEPTNO DNAME ------ ---------- 10 ACCOUNTING 30 SALES 10 ACCOUNTING 20 RESEARCH ... 40 OPERATIONS
Outer Joins • You use an outer join to also see rows that do not usually meet the join condition. • Outer join operator is the plus sign (+). SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column(+)= table2.column; SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column = table2.column(+);
Using Outer Joins SQL> SELECT e.ename, d.deptno, d.dname 2 FROM emp e, dept d 3 WHERE e.deptno(+) = d.deptno 4 ORDER BY e.deptno; ENAME DEPTNO DNAME ---------- --------- ------------- KING 10 ACCOUNTING CLARK 10 ACCOUNTING ... 40 OPERATIONS 15 rows selected.
Outer Joins • Student 105 (Michael Connoly) does not have any ENROLLMENT records
Outer Joins • No records retrieved for Michael:
Outer Joins • To include records in first (inner) table, even when they do not have matching records in second (outer) table, place outer join marker (+) beside outer table name in join clause
Outer Joins Outer join marker
“MGR in the WORKER table is equal to EMPNO in the MANAGER table” Self Joins EMP (WORKER) EMP (MANAGER) EMPNO ENAME MGR----- ------ ---- 7839 KING 7698 BLAKE 7839 7782 CLARK 7839 7566 JONES 7839 7654 MARTIN 7698 7499 ALLEN 7698 EMPNO ENAME----- -------- 7839 KING 7839 KING 7839 KING 7698 BLAKE 7698 BLAKE
Joining a Table to Itself SQL> SELECT worker.ename||' works for '||manager.ename 2 FROM emp worker, emp manager 3 WHERE worker.mgr = manager.empno; WORKER.ENAME||'WORKSFOR'||MANAG ------------------------------- BLAKE works for KING CLARK works for KING JONES works for KING MARTIN works for BLAKE ... 13 rows selected.
Summary SELECT table1.column, table2.column FROM table1, table2 WHERE table1.column1 = table2.column2; • Equijoin • Non-equijoin • Outer join • Self join
Subqueries SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table); • The subquery (inner query) executes once before the main query. • The result of the subquery is used by the main query (outer query).
2975 Using a Subquery SQL> SELECT ename 2 FROM emp 3 WHERE sal > 4 (SELECT sal 5 FROM emp 6 WHERE empno=7566); ENAME ---------- KING FORD SCOTT
Guidelines for Using Subqueries • Enclose subqueries in parentheses. • Place subqueries on the right side of the comparison operator. • Do not add an ORDER BY clause to a subquery. • Use single-row operators with single-row subqueries. • Use multiple-row operators with multiple-row subqueries.
Main query returns Subquery • Multiple-row subquery Main query CLERK MANAGER returns Subquery • Multiple-column subquery Main query returns CLERK 7900MANAGER 7698 Subquery Types of Subqueries • Single-row subquery CLERK
CLERK 1100 Executing Single-Row Subqueries SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4 (SELECT job 5 FROM emp 6 WHERE empno = 7369) 7 AND sal > 8 (SELECT sal 9 FROM emp 10 WHERE empno = 7876); ENAME JOB ---------- --------- MILLER CLERK
800 Using Group Functions in a Subquery SQL> SELECT ename, job, sal 2 FROM emp 3 WHERE sal = 4 (SELECT MIN(sal) 5 FROM emp); ENAME JOB SAL ---------- --------- --------- SMITH CLERK 800
800 HAVING Clause with Subqueries • The Oracle Server executes subqueries first. • The Oracle Server returns results into the HAVING clause of the main query. SQL> SELECT deptno, MIN(sal) 2 FROM emp 3 GROUP BY deptno 4 HAVING MIN(sal) > 5 (SELECT MIN(sal) 6 FROM emp 7 WHERE deptno = 20);
What Is Wrong with This Statement? SQL> SELECT empno, ename 2 FROM emp 3 WHERE sal = 4 (SELECT MIN(sal) 5 FROM emp 6 GROUP BY deptno); Single-row operator with multiple-row subquery ERROR: ORA-01427: single-row subquery returns more thanone row no rows selected
Will This Statement Work? SQL> SELECT ename, job 2 FROM emp 3 WHERE job = 4 (SELECT job 5 FROM emp 6 WHERE ename='SMYTHE'); no rows selected Subquery returns no values
Multiple-Row Subqueries • Return more than one row • Use multiple-row comparison operators Operator IN ANY ALL Meaning Equal to any member in the list Compare value to each value returned by the subquery Compare value to every value returned by the subquery
Database Views • Logical table based on a query • Does not physically exist in the database • Presents data in a different format from underlying tables • Uses: • Security • Simplifying complex queries
Simple Views and Complex Views Feature Simple Views Complex Views Number of tables One One or more Contain functions No Yes Contain groups of data No Yes DML through view Yes Not always
Creating a View • You embed a subquery within the CREATE VIEW statement. • The subquery can contain complex SELECT syntax. • The subquery cannot contain an ORDER BY clause. CREATE [OR REPLACE] VIEW view_name AS subquery;
Creating a View • Create a view, EMPVU10, that contains details of employees in department 10. SQL> CREATE VIEW empvu10 2 AS SELECT empno, ename, job 3 FROM emp 4 WHERE deptno = 10; View created. • Describe the structure of the view by using the SQL*Plus DESCRIBE command. SQL> DESCRIBE empvu10
Retrieving Data from a View SQL> SELECT * 2 FROM salvu30; EMPLOYEE_NUMBER NAME SALARY --------------- ---------- --------- 7698 BLAKE 2850 7654 MARTIN 1250 7499 ALLEN 1600 7844 TURNER 1500 7900 JAMES 950 7521 WARD 1250 6 rows selected.
Removing a View • Remove a view without losing data because a view is based on underlying tables in the database. DROP VIEW view; SQL> DROP VIEW empvu10; View dropped.
Using Set Operators in Queries • Performs set operations on outputs of two unrelated queries • Both queries must have: • same number of display fields • corresponding display fields must have same data type
Query Set Operators • UNION: combines results, suppresses duplicate rows • UNION ALL: combines results, displays duplicates • INTERSECT: finds matching rows • MINUS: returns the difference between returned record sets
Synonyms • Alternate name for a table • Allows you to not have to preface table with owner’s username when you are querying a table that belongs to another user
Public Synonyms • Can only be created by a DBA • Syntax: CREATE PUBLIC SYNONYM synonym_name FOR owner_name.tablename; • All users with privileges to use table can then use synonym instead of owner_name.tablename
Private Synonyms • You can create private synonyms for any tables that you have privileges to use • Only you can use the synonym • Syntax: CREATE SYNONYM synonym_name FOR table_name.table_name;