0% found this document useful (0 votes)
50 views

What Is The Difference Between Inner and Outer Join? Explain With Example

Inner join returns rows when there is at least one match in both tables, while outer join returns matching rows and any unmatched rows from one or both tables. UNION combines result sets and removes duplicates, UNION ALL returns all rows including duplicates. JOIN looks up records between tables based on conditions, while UNION combines similar data sets without conditions. Self join joins a table to itself to convert hierarchical to flat structures. Rows can be selected without ROW_NUMBER by using a unique column as a sort key and counting rows.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

What Is The Difference Between Inner and Outer Join? Explain With Example

Inner join returns rows when there is at least one match in both tables, while outer join returns matching rows and any unmatched rows from one or both tables. UNION combines result sets and removes duplicates, UNION ALL returns all rows including duplicates. JOIN looks up records between tables based on conditions, while UNION combines similar data sets without conditions. Self join joins a table to itself to convert hierarchical to flat structures. Rows can be selected without ROW_NUMBER by using a unique column as a sort key and counting rows.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

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

Outer Join can be full outer or single outer

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

What is the difference between JOIN and UNION?


,1" J IN allows us to 3loo+up4 records on other table based on the gi'en conditions between two tables. 5or e$ample, if we ha'e the department I) of each employee, then we can use this department I) of the employee table to join with the department I) of department table to loo+up department names.

!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&'

What is the difference between UNION and UNION A ?


!NI N and !NI N ("" both unify for add two structurally similar data sets, but !NI N operation returns only the uni/ue records from the resulting data set whereas !NI N ("" will return all the rows, e'en if one or more rows are duplicated to each other. In the following e$ample, I am choosing e$actly the same employee from the emp table and performing !NI N and !NI N ("". 7hec+ the difference in the result. SELECT $ FROM EMPLOYEE WHERE #D = ( !N#ON ALL SELECT $ FROM EMPLOYEE WHERE #D = ( ID ..8 ..8 MGR_ID 6.8 6.8 DEPT_ID 6.8 6.8 NAME (nno (nno SAL 98.8 98.8 DOJ 8:;5eb;68:6 8:;5eb;68:6

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

What is the difference between W!E"E clause and !A#IN$ clause?


<%ERE and %(=IN> both filters out records based on one or more conditions. -he difference is, <%ERE clause can only be applied on a static non;aggregated column whereas we will need to use %(=IN> for aggregated columns. -o understand this, consider this e$ample. ,uppose we want to see only those departments where department I) is greater than ?. -here is no aggregation operation and the condition needs to be applied on a static field. <e will use <%ERE clause here2

SELECT $ FROM DEPT WHERE #D ) * ID @ . NAME ,ales "ogistics

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.

What is the difference amon% UNION& 'INU( and IN)E"(E*)?


!NI N combines the results from 6 tables and eliminates duplicate records from the result set. *IN!, operator when used between 6 tables, gi'es us all the rows from the first table e$cept the rows which are present in the second table. IN-ER,E7- operator returns us only the matching or common rows between 6 result sets. -o understand these operators, letBs see some e$amples. <e will use two different /ueries to e$tract data from our emp table and then we will perform !NI N, *IN!, and IN-ER,E7- operations on these two sets of data.

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

What is (elf Join and wh+ is it re,uired?


,elf Join is the act of joining one table with itself.

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

)arl Inno Robo -omiti (nno &ri'y *eme #huti %ash

%ash %ash %ash Robo Robo Robo &ete -omiti

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

!ow can we transpose a table usin% (.chan%in% rows to column or /ice0/ersa1 ?


-he usual way to do it in ,1" is to use 7(,E statement or )E7 )E statement.

!ow to %enerate row number in (Without "OWNU'


>enerating a row number E that is a running se/uence of numbers for each row is not easy using plain ,1". In fact, the method I am going to show below is not 'ery generic either. -his method only wor+s if there is at least one uni/ue column in the table. -his method will also wor+ if there is no single uni/ue column, but collection of columns that is uni/ue. (nyway, here is the /uery2 SELECT name, -a., (SELECT CO!NT($ 678_n9m FROM EMPLOYEE 7 76de6 :; 678_n9m NAME (nno #huti )arl %ash SAL 98 C8 98 :88 ROW_N M : 6 ? @ FROM EMPLOYEE i WHERE 7.name )= i.name

Inno *eme &ete &ri'y Robo -omiti

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

!ow to select first 2 records from a table?


-his /uestion, often as+ed in many inter'iews, does not ma+e any sense to me. -he problem here is how do you define which record is first and which is second. <hich record is retrie'ed first from the database is not deterministic. It depends on many uncontrollable factors such as how database wor+s at that moment of e$ecution etc. ,o the /uestion should really be E 3how to select any . records from the tableJ4 #ut whate'er it is, here is the solution2 In racle,

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.

)arl -omiti &ete #huti *eme Inno &ri'y

98 F8 F8 C8 C8 .8 .8

@ . C F 9 A :8

What are the differences amon% "OWNU'& "AN5 and 6EN(E3"AN5?


R <GN!*#ER assigns contiguous, uni/ue numbers from :.. N to a result set. R(NL does not assign uni/ue numbersMnor does it assign contiguous numbers. If two records tie for second place, no record will be assigned the ?rd ran+ as no one came in third, according to R(NL. ,ee below2 SELECT name, -a., 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 RAN"_!Y_SAL : : ? ? . . F F A A 7+e6(76de6 :; -a. de-= 6an>_:;_-a.

)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

7+e6(76de6 :; -a. de-=

den-e_6an>_:;_-a.

DENSE_RAN"_!Y_SAL : : 6 6 ? ? @ @ . .

This article is to be extended with more questions

You might also like