Your SlideShare is downloading. ×
0
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
MS Sql Server: Joining Databases
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

MS Sql Server: Joining Databases

3,147

Published on

MS Sql Server: Joining Databases

MS Sql Server: Joining Databases

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,147
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. 6<br />SQL SERVER: JOININGDATABASETABLES<br />
  • 2. SQL JOIN: Prerequisites<br />Before we start with SQL Joints, let us go through a few basic DBMS concepts:<br />Primary Key:<br />A Primary key is a field/attribute in a table which is used to uniquely identify a record<br />Eg: Consider a dream database<br />Here, a particular record can be uniquely located using the DreamNumber and hence, it is taken as the Primary key.<br />If there are more than fields, eligible of being the primary key, the decision of choosing one among them lies with the DB designer<br />
  • 3. SQL JOIN: Prerequisites<br />Foreign Key:<br />A Foreign key is a field/attribute in one table which is used as a primary key in another table<br />Eg: Consider a dream database<br />Every foreign key value must be present as a primary key in the referenced table.<br />Eg: A dream number ‘3’ isn’t possible in ‘Luck Table’ unless such a dream number exists in the ‘Dream Table’<br />Dream Table<br />Notice that foreign key entries can repeat( where-as primary key entries can’t!)<br />Foreign Key<br />Primary Key<br />Luck Table<br />Refer-ences<br />
  • 4. SQL JOIN: Prerequisites<br />3.SELECT Statement<br />The Select statement, as the name specifies, is used to select records from a table.<br />The Syntax is:<br />Select &amp;lt;FieldName&amp;gt; from &amp;lt;TableName&amp;gt;;<br />To select all records from a table:<br />Select * from tableName<br />Eg: select * from DreamTable lists the entire table<br />Example: <br />To select Select the Dreams and Times from this table: select dreams, time from DreamTable<br />
  • 5. What does Joining Tables mean?<br />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.<br />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:<br />
  • 6. SQL JOIN<br />An SQL JOIN command combines records from two or more tables in a database.<br />Types of Joins:<br />
  • 7. Inner Join<br />An SQL JOIN command combines records from two or more tables in a database.<br />Alias: JOIN (Both ‘Inner Join’ and ‘Join’ commands can be used)<br />The Inner join returns a column from each table only if there exists an exact match on the other table.<br />Illustration: Consider the ‘Moogle’ Database (See Slide 1)<br />Moogle Employee Tale<br />Moogle Department Table<br />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.<br />
  • 8. Inner Join<br />Illustration: Now, on performing the Inner joint on the above given table:<br />Moogle Employee Tale<br />Moogle Department Table<br />Let us call the field that is used for joining the tables as joining field<br />Join based on the EmpID field<br />New Table (Inner Joint)<br />
  • 9. Inner Join<br />The SQL Syntax for an Inner join is as follows:<br />Syntax:<br />For inner-joining of tables table1 and table2, the SQL Command is:<br />select * from table1inner join table2<br />on table1.Field= table2.Field;<br />Hence, for the creation of the Moogle Table(Previous Slide), the SQL Command is:<br />Select * from EmpTable inner join DepTable on EmpTable.EmpID = DepTable.EmpID<br />Bottom-line: Inner Joints are highly strict. They only accept records that have perfect matching in the other table.<br />
  • 10. Inner Join<br />This is an illustration of an Inner Joint:<br />
  • 11. Outer Join<br />The SQL Syntax for an Inner join is as follows:<br />Outer Join:<br /> 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.<br />Types of Outer Join:<br />
  • 12. Left Outer Join<br />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.<br />Illustration:<br />Moogle Employee Tale<br />Moogle Department Table<br />Left outer Join based on the EmpID field<br />Can you guess what the result might be? Go to the next slide to find out!<br />
  • 13. Left Outer Join<br />Left outer Join based on the EmpID field<br />
  • 14. Understanding Left Outer Join<br />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:<br />Start<br />Get two tables to be joint (table1 and table2)<br />Consider table1:<br /> Consider every record in table1:<br /> search for a match for that record in table 2, for a given value of the joining field<br /> If found, append that record of table2 to that of table1<br /> Else assign null for unmatched values<br />end<br />
  • 15. Understanding Left Outer Join<br />Confused with the algo? Lets make it clear with an example:<br />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.<br />Table2: Person Table<br />Table1: Gifts table<br />
  • 16. Understanding Left Outer Join<br />
  • 17. Left Outer Join<br />Having understood the concept behind left outer joins, its now time to see the sql command behind this:<br />SQL Command:<br />Select * from Table1 left outer join Table2 <br /> on Table1.JoiningFieldName1 = Table2.JoiningFieldName2;<br />Now let us consider the other types of joints.<br />
  • 18. Right Outer Join<br />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.<br />Illustration:<br />Moogle Employee Tale<br />Moogle Department Table<br />Right outer Join based on the EmpID field<br />Now, Can you deduce what the result might be? <br />
  • 19. Right Outer Join<br />Right outer Join based on the EmpID field<br />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.<br />
  • 20. Right Outer Join<br />The SQL command for the right outer join resembles the left outer join<br />Select * from Table1 right outer join Table2 <br /> on Table1.JoiningFieldName1 = Table2.JoiningFieldName2;<br />Now let us consider the final outer join type: Full outer join<br />
  • 21. Full Outer Join<br />A Full outer join is used in situations where two tables are to be combined without loss of data. <br />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.<br />Let us see an illustration:<br /> Consider the Gift table problem that we saw in slide.no: 15<br />
  • 22. Full Outer Join<br />
  • 23. Full Outer Join<br />The SQL command for the full outer join resembles the right outer join<br />Select * from Table1 full outer join Table2 <br /> on Table1.JoiningFieldName1 = Table2.JoiningFieldName2;<br />These are the basic join types. To add fun, let us consider an additional join that SQL Server provides: The Cross join<br />
  • 24. Cross Join<br />A Cross Join is used to produce a Cartesian product between two tables.<br />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.<br />The SQL command for the cross join is as follows:<br />Select * from Table1 cross join Table2;<br />Illustration: See the next slide<br />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.<br />
  • 25. Cross Join<br />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.<br />Boys<br />Girls<br />Boys X Girls<br />X<br />
  • 26. Summary<br />6. Joining Databases<br /><ul><li> SQL Joins
  • 27. Inner Join
  • 28. Outer Join
  • 29. Left Outer Join
  • 30. Right Outer Join
  • 31. Full Outer Join
  • 32. Cross Join</li></li></ul><li>Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />

×