SQL Joins

SQL Joins

Basics of Joins for Beginners

Rajalakshumi K

© 2010 Wipro Ltd - Confidential

Confidential .Agenda 1 Introduction 2 SQL Joins 3 Types of Join 4 Performance tuning on Joins(In Oracle) 5 Conclusion 2 © 2010 Wipro Ltd .Confidential © 2009 Wipro Ltd .

Confidential .Introduction 3 © 2010 Wipro Ltd .

It creates a temporary table by getting values from one or more table.Introduction:  What is meant by SQL Join? An SQL Join clause combine one or more tables in a database. (There should be a matching column information though) From the below specified table we can join Employee and deptno table by using the deptno.Confidential .Confidential © 2009 Wipro Ltd . Ex: Employee table EmPName Rahul Roshan Raji David Joy 4 Department table DeptNo 20 10 30 40 DeptName Engineering Sales Finance Marketing DeptNo 30 30 10 20 Null © 2010 Wipro Ltd .

Confidential .Types of Join 5 © 2010 Wipro Ltd .Confidential © 2009 Wipro Ltd .

Confidential .Types of Join ‡ Inner Join Equi Join Natural Join Cross Join ‡ Outer Join Left Outer Join Right Outer Join Full Outer Join ‡ Self Join 6 © 2010 Wipro Ltd .Confidential © 2009 Wipro Ltd .

SQL specifies two different ways to express joins: "explicit join notation" and "implicit join notation".deptno Implicit join notation: SELECT * FROM employee.Confidential © 2009 Wipro Ltd . department Where employee.It creates a new result table by combining column values of two tables (A and B) based upon the join-predicate .deptno= department.deptno 7 © 2010 Wipro Ltd . Inner Join An Inner Join is the default join type and it is used widely in most applications .Confidential . Example: Explicit join notation: SELECT * FROM employee INNER JOIN department ON employee.deptno= department.

tName DeptNo Finance Sales Engineering Finance 30 10 20 30 8 © 2010 Wipro Ltd .EmPN Employee.Results of the Implicit query: Employee.Dep Department.Confidential .DeptNo ame Rahul Raji David Roshan 30 10 20 30 Department.Confidential © 2009 Wipro Ltd .

 If the columns having the same names have different data types. an error is returned.Confidential © 2009 Wipro Ltd .Natural Join(Natural Join is a Specialized form of Equi join ) The NATURAL JOIN clause is based on all columns in the two tables that have the same name. Departmen EmPNam DeptNo t. Retrieving Records with Natural Joins  SELECT empname.deptname.  It selects rows from the two tables that have equal values in all matched columns.DeptNam e e(?) Rahul Raji David Roshan 30 10 20 30 Finance Sales Engineering Finance 9 © 2010 Wipro Ltd .Confidential .deptno FROM employee NATURAL JOIN department Employee. Employee.

deptname FROM employee CROSS JOIN department Employee. 24 rows will get selected« «.Cross Join(Cartesian product)  Cross Join is Cartesian product which provides the ground work for all the inner joins  This is the same as a Cartesian product between the two tables.Dep ame tName Rahul Joy Raji David Roshan Finance null Sales Engineering Finance « . Retrieving Records with Cross Joins SELECT empname.Confidential .Confidential © 2009 Wipro Ltd ..EmPN Department. « 10 © 2010 Wipro Ltd .

DeptNo  11 © 2010 Wipro Ltd .Outer Join A join between two tables that returns the results of the inner join as well as unmatched rows left (or right) tables is a left (or right) outer join.Confidential .DeptNo = department.  A join between two tables that returns the results of an inner join as well as the results of a left and right join is a full outer join. Retrieving Records with Left Outer Joins SELECT * FROM employee LEFT OUTER JOIN department ON employee.DeptNo = department.DeptNo Retrieving Records with Right Outer Joins SELECT * FROM employee RIGHT OUTER JOIN department ON employee.Confidential © 2009 Wipro Ltd .DeptNo Retrieving Records with Full Outer Joins SELECT * FROM employee FULL OUTER JOIN department ON employee.DeptNo = department.

Confidential .D Department.  Left Outer Join Employee.Confidential © 2009 Wipro Ltd .Results of Outer Join.EmP Name Rahul Raji David Roshan Joy 12 Employee . eptName DeptNo Finance Sales Engineering Finance Null 30 10 20 30 Null © 2010 Wipro Ltd .DeptNo 30 10 20 30 Null Department.

DeptNo 30 10 20 30 35 13 © 2010 Wipro Ltd .Confidential © 2009 Wipro Ltd .De ptNo 30 10 20 30 Null Department. DeptName Finance Sales Engineering Finance Marketing Department. Right Outer Join: Employee.Confidential .Em PName Rahul Raji David Roshan Null Employee.

De ptNo 30 10 Null 30 20 Null © 2010 Wipro Ltd .EmPName Employee. DeptName Finance Sales Null Finance Engineering Marketing Department.Confidential Department. DeptNo 30 10 Null 30 20 35 Rahul Raji Joy Roshan David Null 14 .Confidential © 2009 Wipro Ltd . Full Outer Join Employee.

Oracle always joins two row sources at a time.Confidential . scores each option based on a fixed set of rules and chooses the option with the best score. Performance tuning Oracle¶s optimizer must choose an execution plan for each SQL statement submitted. The plan will indicate how data will be accessed²which indexes will be used and so forth.Confidential © 2009 Wipro Ltd . The cost-based optimizer makes this decision by estimating the cost of each option and choosing the one with the lowest cost. 15 © 2010 Wipro Ltd . The rule-based optimizer. Oracle will actually join two tables together and then join the results with the third table. if a SQL statement joins three tables. The order in which row sources are joined can have a significant impact on performance. and the method to be used for each join operation Join Order:    A row source is a logical set of rows from a partially complete query. If the SQL statement involves a join or a subquery. Therefore. The optimizer makes a list of all physically possible join orders and then chooses what it believes will be the best option. meanwhile. then the optimizer must also choose the order in which tables will be joined together. The row source may be rows in an actual table or rows in a temporary segment stored on disk or in memory. Outer joins limit the number of possible join orders because a table being outer joined must appear in the join order after the table it joins to.

 Conclusion: Hence Join plays a vital role in database to (manipulate and access data) view or creating data accordingly. 16 © 2010 Wipro Ltd . The better the schema design.Confidential © 2009 Wipro Ltd . The Oracle optimizer needn¶t be explicitly given what join methods to use or the order in which to join tables when executing the query.Confidential . but understanding how Oracle processes joins will help in designing better database schemas and write more efficient SQL. and the database as a whole have been tuned for joins. When the schema. individual SQL. applications will run better all around. the less tuning of individual statements will be necessary.

kra@wipro.Confidential .com © 2010 Wipro Ltd .Thank You Rajalakshumi K rajalakshumi.

Sign up to vote on this title
UsefulNot useful