What Is The Difference Between Inner and Outer Join? Explain With Example
What Is The Difference Between Inner and Outer Join? Explain With Example
Inner Join
Inner join is the most common type of Join which is used to combine the rows from two tables and create a result set containing only such records that are present in both the tables based on the joining condition (predicate).
Inner join returns rows when there is at least one match in both tables
If none of the record matches between two tables, then INNER J IN will return a N!"" set. #elow is an e$ample of INNER J IN and the resulting set. SELECT dept.name DEPARTMENT, emp.name EMPLOYEE FROM DEPT dept, EMPLOYEE emp WHERE emp.dept_id = dept.id Department %R %R Engineering Engineering Engineering Engineering *ar+eting *ar+eting ,ales ,ales Employee Inno &ri'y Robo %ash (nno )arl &ete *eme -omiti #huti
Outer Join
uter Join, on the other hand, will return matching rows from both tables as well as any unmatched rows from one or both the tables (based on whether it is single outer or full outer join respecti'ely). Notice in our record set that there is no employee in the department . ("ogistics). #ecause of this if we perform inner join, then )epartment . does not appear in the abo'e result. %owe'er in the below /uery we perform an outer join (dept left outer join emp), and we can see this department. SELECT dept.name DEPARTMENT, emp.name EMPLOYEE FROM DEPT dept, EMPLOYEE emp WHERE dept.id = emp.dept_id (+ Department %R %R Engineering Engineering Engineering Engineering *ar+eting *ar+eting ,ales ,ales "ogistics -he (0) sign on the emp side of the predicate indicates that emp is the outer table here. -he abo'e ,1" can be alternati'ely written as below (will yield the same result as abo'e)2 SELECT dept.name DEPARTMENT, emp.name EMPLOYEE FROM DEPT dept LEFT O!TER "O#N EMPLOYEE emp ON dept.id = emp.dept_id Employee Inno &ri'y Robo %ash (nno )arl &ete *eme -omiti #huti
!NI N operation allows us to add 6 similar data sets to create resulting data set that contains all the data from the source data sets. !nion does not re/uire any condition for joining. 5or e$ample, if you ha'e 6 employee tables with same structure, you can !NI N them to create one result set that will contain all the employees from both of the tables. SELECT $ FROM EMP% !N#ON SELECT $ FROM EMP&'
SELECT $ FROM EMPLOYEE WHERE #D = ( !N#ON SELECT $ FROM EMPLOYEE WHERE #D = ( ID ..8 MGR_ID 6.8 DEPT_ID 6.8 NAME (nno SAL 98.8 DOJ 8:;5eb;68:6
Ne$t, suppose we want to see only those )epartments where ('erage salary is greater than 98. %ere the condition is associated with a non;static aggregated information which is 3a'erage of salary4. <e will need to use %(=IN> clause here2 SELECT dept.name DEPARTMENT, a+,(emp.-a. FROM DEPT dept, EMPLOYEE emp WHERE dept.id = emp.dept_id (+ 0RO!P 1Y dept.name HA/#N0 A/0(emp.-a. DEPARTMENT Engineering ) 23 AVG_SAL A8 A/0_SAL
(s you see abo'e, there is only one department (Engineering) where a'erage salary of employees is greater than 98.
UNION
SELECT $ FROM EMPLOYEE WHERE #D = ( !N#ON SELECT $ FROM EMPLOYEE WHERE #D = 4 ID . MGR_ID 6 DEPT_ID 6.8 NAME (nno SAL 98.8 DOJ 8:;5eb;68:6
6.8
)arl
98.8
::;5eb;68:6
MINUS
SELECT $ FROM EMPLOYEE M#N!S SELECT $ FROM EMPLOYEE WHERE #D ) & ID : 6 : MGR_ID DEPT_ID 6 6 NAME %ash Robo SAL :88.8 :88.8 DOJ 8:;Jan;68:6 8:;Jan;68:6
INTERSECT
SELECT $ FROM EMPLOYEE WHERE #D #N (&, *, ( #NTERSECT SELECT $ FROM EMPLOYEE WHERE #D #N (%, &, 5, ( ID . 6 MGR_ID 6 : DEPT_ID 6 6 NAME (nno Robo SAL 98.8 :88.8 DOJ 8:;5eb;68:6 8:;Jan;68:6
Self Join is often very useful to convert a hierarchical structure into a flat structure
In our employee table e$ample abo'e, we ha'e +ept the manager I) of each employee in the same row as that of the employee. -his is an e$ample of how a hierarchy (in this case employee;manager hierarchy) is stored in the R)#*, table. Now, suppose if we need to print out the names of the manager of each employee right beside the employee, we can use self join. ,ee the e$ample below2 SELECT e.name EMPLOYEE, m.name MANA0ER FROM EMPLOYEE e, EMPLOYEE m WHERE e.m,6_id = m.id (+ EMPLOYEE &ete MANAGER %ash
-he only reason we ha'e performed a left outer join here (instead of INNER J IN) is we ha'e one employee in this table without a manager (employee I) D :). If we perform inner join, this employee will not show;up.
.8 C8 F8 .8 :88 F8
. C F 9 A :8
-he column that is used in the row number generation logic is called 3sort +ey4. %ere sort +ey is 3name4 column. 5or this techni/ue to wor+, the sort +ey needs to be uni/ue. <e ha'e chosen the column 3name4 because this column happened to be uni/ue in our Employee table. If it was not uni/ue but some other collection of columns was, then we could ha'e used those columns as our sort +ey (by concatenating those columns to form a single sort +ey). (lso notice how the rows are sorted in the result set. <e ha'e done an e$plicit sorting on the rowGnum column, which gi'es us all the row numbers in the sorted order. #ut notice that name column is also sorted (which is probably the reason why this column is referred as sort;+ey). If you want to change the order of the sorting from ascending to descending, you will need to change 3HD4 sign to 3ID4 in the /uery. (s I said before, this method is not 'ery generic. -his is why many databases already implement other methods to achie'e this. 5or e$ample, in racle database, e'ery ,1" result set contains a hidden column called R <N!*. <e can just e$plicitly select R <N!* to get se/uence numbers.
SELECT $ FROM EMP WHERE ROWN!M <= (' In ,1" ,er'er, SELECT TOP ( $ FROM EMP' >eneric solution, I belie'e a generic solution can be de'ised for this problem if and only if there e$ists at least one distinct column in the table. 5or e$ample, in our E*& table I) is distinct. <e can use that distinct column in the below way to come up with a generic solution of this /uestion that does not re/uire database specific functions such as R <N!*, - & etc.
SELECT
name
FROM EMPLOYEE 7 WHERE (SELECT =79nt($ name Inno (nno )arl *eme #huti I ha'e ta+en 3name4 column in the abo'e e$ample since 3name4 is happened to be uni/ue in this table. I could 'ery well ta+e I) column as well. In this e$ample, if the chosen column was not distinct, we would ha'e got more than . records returned in our output. )o you ha'e a better solution to this problemJ If yes, post your solution in the comment. FROM EMPLOYEE i WHERE i.name < 7.name < (
What is the difference between "OWNU' pseudo column and "OW3NU'4E".1 function?
R <N!* is a pseudo column present in e'aluated. ,o racle database returned result set prior to R)ER #K being R)ER #K R <N!* does not wor+. =ER() clause wherein we
R <GN!*#ER() is an analytical function which is used in conjunction to can specify R)ER #K and also &(R-I-I N #K columns.
,uppose if you want to generate the row numbers in the order of ascending employee salaries for e$ample, R <N!* will not wor+. #ut you may use R <GN!*#ER() SELECT name, -a., 678_n9m:e6( FROM EMPLOYEE 7 name %ash Robo (nno Sal :88 :88 98 ROWN M_!Y_SAL : 6 ? 7+e6(76de6 :; -a. de-= =ER() li+e shown below2 678n9m_:;_-a.
98 F8 F8 C8 C8 .8 .8
@ . C F 9 A :8
)EN,EGR(NL, li+e R(NL, does not assign uni/ue numbers, but it does assign contiguous numbers. E'en though two records tied for second place, there is a third;place record. ,ee below2
SELECT name, -a., den-e_6an>( FROM EMPLOYEE 7 name %ash Robo (nno )arl -omiti &ete #huti *eme Inno &ri'y Sal :88 :88 98 98 F8 F8 C8 C8 .8 .8
den-e_6an>_:;_-a.
DENSE_RAN"_!Y_SAL : : 6 6 ? ? @ @ . .