SQL JOIN: Prerequisites Before we start with SQL Joints, let us go through a few basic DBMS concepts: Primary Key: A Primary key is a field/attribute in a table which is used to uniquely identify a record Eg: Consider a dream database Here, a particular record can be uniquely located using the DreamNumber and hence, it is taken as the Primary key. If there are more than fields, eligible of being the primary key, the decision of choosing one among them lies with the DB designer
SQL JOIN: Prerequisites Foreign Key: A Foreign key is a field/attribute in one table which is used as a primary key in another table Eg: Consider a dream database Every foreign key value must be present as a primary key in the referenced table. Eg: A dream number ‘3’ isn’t possible in ‘Luck Table’ unless such a dream number exists in the ‘Dream Table’ Dream Table Notice that foreign key entries can repeat( where-as primary key entries can’t!) Foreign Key Primary Key Luck Table Refer-ences
SQL JOIN: Prerequisites 3.SELECT Statement The Select statement, as the name specifies, is used to select records from a table. The Syntax is: Select <FieldName> from <TableName>; To select all records from a table: Select * from tableName Eg: select * from DreamTable lists the entire table Example: To select Select the Dreams and Times from this table: select dreams, time from DreamTable
What does Joining Tables mean? Joining tables means joining the data contained in the tables. The Programmer may specify various restrictions and rules that the joining must follow so as to suite the Programmer’s needs. For example, suppose that some time in the future, the IT Giants Microsoft and Google merge into a new company ‘Moogle’, it would be easy to join the employee tables of both the companies, as follows:
SQL JOIN An SQL JOIN command combines records from two or more tables in a database. Types of Joins:
Inner Join An SQL JOIN command combines records from two or more tables in a database. Alias: JOIN (Both ‘Inner Join’ and ‘Join’ commands can be used) The Inner join returns a column from each table only if there exists an exact match on the other table. Illustration: Consider the ‘Moogle’ Database (See Slide 1) Moogle Employee Tale Moogle Department Table NOTE: Let us follow the convention that if a field is underlined, it denotes a primary key. If it is italicized then it denotes a foreign key.
Inner Join Illustration: Now, on performing the Inner joint on the above given table: Moogle Employee Tale Moogle Department Table Let us call the field that is used for joining the tables as joining field Join based on the EmpID field New Table (Inner Joint)
Inner Join The SQL Syntax for an Inner join is as follows: Syntax: For inner-joining of tables table1 and table2, the SQL Command is: select * from table1inner join table2 on table1.Field= table2.Field; Hence, for the creation of the Moogle Table(Previous Slide), the SQL Command is: Select * from EmpTable inner join DepTable on EmpTable.EmpID = DepTable.EmpID Bottom-line: Inner Joints are highly strict. They only accept records that have perfect matching in the other table.
Inner Join This is an illustration of an Inner Joint:
Outer Join The SQL Syntax for an Inner join is as follows: Outer Join: It returns all rows from the first table stated in the join operation; only returns data for rows from the second stated table where there is a matching value. This can result in null values in the result when the first stated table in the join has a row with no matching row(s) in the second stated table. Types of Outer Join:
Left Outer Join A Left outer join is a type of an outer join where all the records of the first table are considered and are joined with matches(if found) from the second table. If a match for a record (for the field of joining) is not found in the second table, null values are assigned for those fields. Illustration: Moogle Employee Tale Moogle Department Table Left outer Join based on the EmpID field Can you guess what the result might be? Go to the next slide to find out!
Left Outer Join Left outer Join based on the EmpID field
Understanding Left Outer Join The Example would have made clear the mechanism of the left outer join. As an extra measure, let us see a simple algorithm for the left outer join: Start Get two tables to be joint (table1 and table2) Consider table1: Consider every record in table1: search for a match for that record in table 2, for a given value of the joining field If found, append that record of table2 to that of table1 Else assign null for unmatched values end
Understanding Left Outer Join Confused with the algo? Lets make it clear with an example: Consider Harry who maintains a database of the gifts that he got for his birthday. He wants to join the table so that he could find the addresses of people who gifted him. The field of joining will be Name of the person as it is common in both tables and is the correct choice for the given problem. Table2: Person Table Table1: Gifts table
Left Outer Join Having understood the concept behind left outer joins, its now time to see the sql command behind this: SQL Command: Select * from Table1 left outer join Table2 on Table1.JoiningFieldName1 = Table2.JoiningFieldName2; Now let us consider the other types of joints.
Right Outer Join A Right outer join is a type of an outer join where all the records of the second (which is on the right side) table are considered and are joined with matches(if found) from the first table. If a match for a record (for the field of joining) is not found in the first table, null values are assigned for those fields. Illustration: Moogle Employee Tale Moogle Department Table Right outer Join based on the EmpID field Now, Can you deduce what the result might be?
Right Outer Join Right outer Join based on the EmpID field NOTE: Having understood the left outer join, it is easy to understand the right outer joins. If you find any difficulty, please return to left outer joins.
Right Outer Join The SQL command for the right outer join resembles the left outer join Select * from Table1 right outer join Table2 on Table1.JoiningFieldName1 = Table2.JoiningFieldName2; Now let us consider the final outer join type: Full outer join
Full Outer Join A Full outer join is used in situations where two tables are to be combined without loss of data. In Full outer join, all records from the first table and all records from the second table are considered. If there is a common joining field value found, the records are combined. Else, the records are treated as separate in the new joint table, with NULL values for the left out fields. Let us see an illustration: Consider the Gift table problem that we saw in slide.no: 15
Full Outer Join The SQL command for the full outer join resembles the right outer join Select * from Table1 full outer join Table2 on Table1.JoiningFieldName1 = Table2.JoiningFieldName2; These are the basic join types. To add fun, let us consider an additional join that SQL Server provides: The Cross join
Cross Join A Cross Join is used to produce a Cartesian product between two tables. In mathematical terms, two sets ‘A’ and ‘B’ under cross product means that every variable in set A will be combined with every variable in set B. The same can be applied to tables here. The SQL command for the cross join is as follows: Select * from Table1 cross join Table2; Illustration: See the next slide NOTE: Do remember to use the go command to run any SQL Statement. You can type a list of commands and run them sequentially with a single go statement also.
Cross Join Illustration: Consider an online dating database. All possible matches between the boys and girls following tables are needed. Here, a cross join would prove useful to find all possible combinations. Boys Girls Boys X Girls X
Visit more self help tutorials Pick a tutorial of your choice and browse through it at your own pace. The tutorials section is free, self-guiding and will not involve any additional support. Visit us at www.dataminingtools.net