JOINS IN ORACLE DATABASE Batch – AHD 12 LG – AEN 52/1516 Presented by Archana Subba (970316) Arindam Sarkar (972394) Avik Dey (972499) Bilas Sarkar (972492) Chandrima Mandal(970437) Debayan Bhowmik (1004155) Contents Introduction ● What is Join? ● Inner Join ● Inner Join Operation ● Left Outer Join ● Right Outer Join ● Full Outer Join ● Self Join ● Cross Join ● Conclusion ● References ● Introduction ➢ ➢ ➢ ➢ Oracle database is a collection of data treated as a unit. Structured Query Language (SQL) – computer programming language used for getting information into and out of a database. SQL is an ANSI (American National Standards Institute) standard. Joins can be used to combine tables. What is Join? ➢ ➢ SQL join is an instruction to a database to combine data from more than one table. There are different kinds of joins, which have different rules for the data they retrieve Inner join ➢ Outer join ● Left Outer join ● Right Outer Join ● Full Outer Join ➢ Self join ➢ Cross join ➢ Inner Join ➢ An inner join produces a result set that is limited to the rows where there is a match in both tables for what we're looking for. ➢ Syntax for Inner Join: SELECT column_1,column_2.......column_n FROM Table1 INNER JOIN Table2 ON Table1.col_name=Table2.col_name Inner Join Operation Enroll Student ID Name ID Code 123 124 125 126 John Mary Mark Jane 123 124 124 126 DBS PRG DBS PRG Query: SELECT * FROM Student INNER JOIN Enroll ON Student.id = Enroll.id ID Name ID Code 123 John 123 DBS 124 Mary 124 PRG 124 Mary 124 DBS 126 Jane 126 PRG Left Outer Join ➢ ➢ A left outer join, or left join, results in a set where all of the rows from the first, or left table are preserved. Syntax for Left Outer Join SELECT * FROM Table1 LEFT OUTER JOIN Table2 ON Table1.common_field = Table2.common_field Customers Left Outer Join Operation Orders ID Name Age Add Salary 1 Ram 32 Ahd 2000 Or_id Year Cust_I d Amou nt 102 2009 3 3000 2 Khilan 25 Delhi 1500 100 2009 3 1500 3 Shyam 23 Kota 2000 101 2011 2 1560 4 Rani 25 Mum 6500 103 2008 4 2060 Query: SELECT ID, NAME, AMOUNT,YEAR FROM CUSTOMERS LEFT OUTER JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUST_ID; Left Outer Join Operation (contd...) Output Table : ID Name Amount Year 1 Ram null null 2 Khilan 1560 2011 3 Shyam 3000 2009 3 Shyam 1500 2009 4 Rani 2060 2008 Right Outer Join ➢ ➢ A right outer join, or right join, is the same as a left join, except the roles are reversed. Syntax for Right Outer Join: SELECT * FROM Table1 RIGHT OUTER JOIN Table2 ON Table1.common_field = Table2.common_field Right Outer Join Operation Customers Orders ID Name Age Add Salary 1 Ram 32 Ahd 2000 Or_id Year Cust_I d Amou nt 102 2009 3 3000 2 Khilan 25 Delhi 1500 100 2009 3 1500 3 Shyam 23 Kota 2000 101 2011 2 1560 4 Rani 25 Mum 6500 103 2008 4 2060 Query: SELECT ID, NAME, AMOUNT,YEAR FROM CUSTOMERS RIGHT OUTER JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUST_ID; Right Outer Join Operation (contd...) Output Table : ID Name Amount Year 3 Shyam 3000 2009 3 Shyam 1500 2009 2 Khailan 1560 2011 4 Rani 2060 2008 Full Outer Join ➢ ➢ A full outer join, or just outer join, produces a result set with all of the rows of both tables, regardless of whether there are any matches. Syntax for Full Outer Join: SELECT * FROM Table1 FULL OUTER JOIN Table2 ON Table1.common_field = Table2.common_field Full Outer Join Operation Customers Orders ID Name Age Add Salary 1 Ram 32 Ahd 2000 2 Khilan 25 Delhi 1500 3 Shyam 23 Kota 2000 4 Rani 25 Mum 6500 Or_id Year 102 2009 3 3000 100 2009 3 1500 101 2011 2 1560 103 2008 4 2060 Query: SELECT ID, NAME, AMOUNT,YEAR FROM CUSTOMERS FULL OUTER JOIN ORDERS ON CUSTOMERS.ID = ORDERS.CUST_ID; Cust_I Amou d nt Full Outer Join Operation (contd...) Output Table : ID Name Amount Year 1 Ram null null 2 Khilan 1560 2011 3 Shyam 3000 2009 3 Shyam 1500 2009 4 Rani 2060 2008 Self Join ➢ We can join a single table to itself. In this case, we are using the same table twice ➢ Syntax for Self Join SELECT a.column_1, b.column_2 FROM table1 a, table 1 b WHERE a.common_field = b.common_field Self Join Operation gid first_name last_name favorite_tool 1 Albert Einstein mind 2 Albert Slater singlet 3 Christian Slater spade 4 Christian Bale videotapes 5 Bruce Wayne shovel 6 Wayne Knight spade Gardener's Table Query SELECT G1.gid, G1.first_name, G1.last_name, G2.gid, G2.first_name, G2.last_name FROM Gardners G1, Gardners G2 WHERE G1.first_name = G2.first_name gid 1 3 first_name last_name Albert Einstein Christian Slater gid 2 4 first_name last_name Albert Slater Output Table Christian Bale Cross Join ➢ The cross join returns a table with a potentially very large number of rows. ➢ The row count of the result is equal to the number of rows in the first table times the number of rows in the second table. ➢ Each row is a combination of the rows of the first and second table. ➢ Syntax for Cross Join SELECT * FROM Table1 CROSS JOIN Table2; Cross Join Operation Student ID NAME 123 124 125 126 John Mary Mark Jane Enrollment ID CODE 123 124 124 126 DBS PRG DBS PRG Query - SELECT * FROM Student CROSS JOIN Enrollment; Output Table ID NAME CODE 123 John DBS 123 John PRG 123 John DBS 123 John PRG 124 Mary DBS 124 Mary PRG 124 Mary DBS 124 Mary PRG And so on.... Conclusion ➢ ➢ ➢ ➢ ➢ SQL Joins are used to combine records from two or more tables in a database. INNER JOIN combines column values of two tables. FULL OUTER JOIN is a combination of Left Outer Join and Right Outer Join. The SELF JOIN is used to join a table to itself. CARTESIAN or CROSS JOIN returns the Cartesian product of the sets of records from the joined tables. References ➢ www.tutorialspoint.com/sql/sql-using-joins.htmL ➢ http://www.sql-join.com ➢ http://www.w3schools.com ➢ The Programming Language of ORACLE by IVAN BAYROSS. ➢ http://www.books24x7.com THANK YOU...