JOINS: why we need to join?? Suppose we need to get/retrieve the data from multiple columns which exists in multiple tables...then we use joins.. what is the condition for doing joins??...yes at least we have one common column (or common format column) from the both tables Lets say EMP table has "deptno" column and DEPT has "deptno" column. So we can join both the tables. TYPES Better to use tables aliases/column aliases when doing joins else we get column ambiguty. Cross Joins Equi Join/ Simple Join/ Normal Join Non equi Joins Self Join Outer Joins Cross Joins: Suppose we have EMP(14) and DEPT(4) tables eg: select e.empno, e.ename, d.deptno, d.dname from emp e, dept d Output we get like 56(14*4) rows selected..as internally Cartesian join performed.. So we have to analyze that, is that result correct or not?? --we have only 14 employees but we got 56 employees existing in the result...so this is not the correct output. So what should we do... Here we need to mention the 'JOIN' condition to get the proper results.. select e.empno, e.ename, d.deptno, d.dname from emp e, dept d
where e.deptno=d.deptno Here we mentioned in 'where' clause that...matching deptno of both tables... Question: suppose table A has 5 rows and B has 4 rows.. Then what is the output for below query? sql>select * from A,B analyze output is correct or not..what's the reason TITBITS >select * from emp where depno=10 >select * from emp where 1=0 Here we get "no rows selected" as output. Cause 'where' condition is not satisfied. We should notice that..if 'where' condition is correct then only we get correct results else "no rows selected" >select * from emp where 1=1 >select 1 from emp here we get 14 ones(1) as 14 records are existing >select 'srinu' from emp >select * from emp where null=null here we get "no rows selected" as 'null is not equal to null'...but check below one >select * from emp where null<>null here also we get "no rows selected" as we don t know the proper value for the null null is not zero value. It just an empty space..
then what will be correct one?? >select * from emp where null is null this is correct one. We have data for both EMP table and DEPT like above. Now we want get the dept name for each employee. But departname(dname) existing in DEPT table and ename is existing in EMP table. So if we want to get, we want to JOIN the tables (EMP and DEPT) Natural Join or Equi Join
So here we need to check how oracle perform JOIN operation. Here we joined both EMP and DEPT table..and we can see how oracle performing join. Oracle taking deptno of both tables and comparing and print the MATCHING results We can also write the above query in ANSI syntax (recommended) Select e.empno empno, e.ename emp_name, e.sal emp_sal, e.deptno emp_deptno, d.deptno dept_deptno, d.dname dept_name, d.loc dept_loc From emp e JOIN dept d ON(e.deptno=d.deptno)
But the above result is not the Proper(perfect result) as we have DEPTNO 40 also in DEPT table So Oracle should display DEPT has DEPTNO 40. But no employee existing in that..then it is perfect result Here OUTER JOINS comes into the picture. 1.RIGHT OUTER JOIN ANSI example
Observe carefully the above result. As we got 15 rows So here RIGHT OUTER JOIN giving all the results of right side table[dept].( see we get 40 deptno and there is no employees existing ie getting null values to the corresponding row(deptno 40)) You can just check LEFT OUTER JOIN by placing LEFT in RIGHT place of above query. You need to get the point here..that.. OUTER JOINS Gives both matching and un matching results ( Here deptno 40(DEPT table) is the un matching record) If we want to display left side table total info then use LEFT OUTER JOIN.. and try the below query..observe the difference. select e.empno,nvl(e.ename,'no EMPLOYEE') ename,e.sal,e.deptno,d.deptno,d.dname,d.loc from emp e right outer join dept d on (e.deptno=d.deptno) Here NVL replacing NULL value by our given value('no EMPLOYEE') SELF JOINS By the name we can understand that, we are joining the same table
Suppose we have EMP table Then SELF JOIN like EMP e1 join EMP e2) Eg: Get the employee managers.. Here in EMP table we can observe that. Manager column is there. But they are only EMPNO of managers. Here by doing SELF JOIN we are displaying the manager names of the employees CONFUSED???? Here we get the clear picture... here 1 st table is EMP (E) table and joining with same ( EMP (M) ) table..see query
first one is EMP table and second one is EMP(mgr table lets say)--self join for empno 7369 manager is 7902 and if we observe matching arrow( matching to same table MGR) That 7902 ename is ford and he is analyst If we check our output.. The record for employee num 7369 details come like below like above self join done for all empnos.. But here also a mistake... whats that??
Yes there is no employee 7839 in our result( see we got only 13 results instead of 14 employees) So why 7839 is not comes into the picture...as 7839 is President and has no manager..(i marked in sky blue line you can check). So 7839 is the un matching record.. there is no matching in the other table(here in our case sama(emp) table as self join)... so what will be the perfect output then?? we have to get like.. 7839 is existing in EMP table but has no manager...as he is president.. So which comes again into our picture??? OUTER JOINS Here we have used LEFT OUTER JOIN why??? Cause we need to have all employees...(we dont miss the employee :) ). so we get all the empno of emp table(e).. FULL OUTER JOIN:
You can understand yourself what is that?? Just place FULL instead of LEFT in the above query... So by FOJ we get both matching and un matching results from both the tables What You Need to Write Efficient SQL Understand Access Paths Full Scans Full Scans and Multiblock Read Counts Full Scans and the High-Water Mark ROWID Access ROWIDs to Speed Searches ROWID Ranges Index Scans B*Tree Structure Index-Unique Scans Index-Range Scans Index-Skip Scan Index Full Scan Index Fast-Full Scan Index Joins Cluster Scans Understand Joins Nested Loops Natural Joins with Nested Loops Outer Joins with Nested Loops Hash Joins Hash Natural Joins Hash Outer Joins Sort-Merge Joins Cartesian Joins Anti-Joins Full Outer Joins Schema Matters (Physical) Really Know SQL The ROWNUM Pseudo Column How ROWNUM Works Tuning with ROWNUM Reduce Function Calls Join Queries Pagination with ROWNUM
Top-N Query Processing with ROWNUM ROWNUM Wrap-up Scalar Subqueries Remove an Outer Join Use Two Scalar Subqueries Use a Single Scalar Subquery Use an Object Type Aggregate from Multiple Tables Select from Different Tables Join Rows to a Set of Tables Perform Lookups Analytics Find a Specific Row in a Partition Top-N in a Group Transposing (Pivoting) Prior Row/Next Row Parallelization Bin the Extents Group the Data Split into ROWID Ranges Parallelizing Analytics Wrap-up