SlideShare a Scribd company logo
6SQL SERVER: JOININGDATABASETABLES
SQL JOIN: PrerequisitesBefore 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 recordEg: Consider a dream databaseHere, 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: PrerequisitesForeign Key:A Foreign key is a field/attribute in one table which is used as a primary key in another tableEg: Consider a dream databaseEvery 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 TableNotice that foreign key entries can repeat( where-as primary key entries can’t!)Foreign KeyPrimary KeyLuck TableRefer-ences
SQL JOIN: Prerequisites3.SELECT StatementThe 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 tableNameEg: select * from DreamTable lists the entire tableExample: 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 JOINAn SQL JOIN command combines records from two or more tables in a database.Types of Joins:
Inner JoinAn 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 TaleMoogle Department TableNOTE: 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 JoinIllustration: Now, on performing the Inner joint on the above given table:Moogle Employee TaleMoogle Department TableLet us call the field that is used for joining the tables as joining fieldJoin based on the EmpID fieldNew Table (Inner Joint)
Inner JoinThe 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 table2on 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.EmpIDBottom-line: Inner Joints are highly strict. They only accept records that have perfect matching in the other table.
Inner JoinThis is an illustration of an Inner Joint:
Outer JoinThe 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 JoinA 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 TaleMoogle Department TableLeft outer Join based on the EmpID fieldCan you guess what the result might be? Go to the next slide to find out!
Left Outer JoinLeft outer Join based on the EmpID field
Understanding Left Outer JoinThe 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:StartGet 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 valuesend
Understanding Left Outer JoinConfused 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 TableTable1: Gifts table
Understanding Left Outer Join
Left Outer JoinHaving 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 JoinA 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 TaleMoogle Department TableRight outer Join based on the EmpID fieldNow, Can you deduce what the result might be?
Right Outer JoinRight outer Join based on the EmpID fieldNOTE: 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 JoinThe SQL command for the right outer join resembles the left outer joinSelect * 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 JoinA 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
Full Outer JoinThe SQL command for the full outer join resembles the right outer joinSelect * 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 JoinA 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 slideNOTE: 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 JoinIllustration: 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.BoysGirlsBoys X GirlsX
Summary6. Joining Databases  SQL Joins
  Inner Join

More Related Content

Similar to MS SQL SERVER: Joining Databases (20)

Day-06-Joining presentation of SQL lecture form uni .pptx
Day-06-Joining presentation of SQL lecture form uni .pptx
joynulabeden2
 
SQL Joins
SQL Joins
Paul Harkins
 
Ch1_s2_QueryMultipleTablesByUsingJoins.pptx
Ch1_s2_QueryMultipleTablesByUsingJoins.pptx
PatrykAdamczyk6
 
joins dbms.pptx
joins dbms.pptx
barathvikraman08
 
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
kailasmanoj
 
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
kailasmanoj
 
Querying_with_T-SQL_-_03 (1).pptx
Querying_with_T-SQL_-_03 (1).pptx
MAHIN33
 
Querying_with_T-SQL_-_03.pptx
Querying_with_T-SQL_-_03.pptx
MAHIN33
 
Ms sql server ii
Ms sql server ii
Iblesoft
 
SQL JOINS
SQL JOINS
Swapnali Pawar
 
SQL JOIN.pptx
SQL JOIN.pptx
johnwick814916
 
askndmf,dskmlf,bvdmk,v nmdsvjkmc,dvjkcmsdcvjnkmd
askndmf,dskmlf,bvdmk,v nmdsvjkmc,dvjkcmsdcvjnkmd
talhahakeem295
 
Tipos de Joins para consultas em banco de dados.pdf
Tipos de Joins para consultas em banco de dados.pdf
PabloLpez168453
 
SQL joins for Database Testing easy .pdf
SQL joins for Database Testing easy .pdf
RohitSharma189763
 
Joins.pptxjjbmmmnnnnnjjjxrhjfluflurulrdudlu
Joins.pptxjjbmmmnnnnnjjjxrhjfluflurulrdudlu
c4x6vgk2n7
 
Sql joins
Sql joins
Vivek Singh
 
types of SQL Joins
types of SQL Joins
vikram rajpurohit
 
Joins
Joins
Mritunjay Sharma
 
Assignment 4
Assignment 4
SneaK3
 
Joins and Views.pptx
Joins and Views.pptx
SangitaKabi
 
Day-06-Joining presentation of SQL lecture form uni .pptx
Day-06-Joining presentation of SQL lecture form uni .pptx
joynulabeden2
 
Ch1_s2_QueryMultipleTablesByUsingJoins.pptx
Ch1_s2_QueryMultipleTablesByUsingJoins.pptx
PatrykAdamczyk6
 
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
sql joinsubdjbrjdbjrjnfkjcnkrnfknrkfkrfkrfkrk
kailasmanoj
 
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
sqlyyybdbyehduheufhuehfuheuwehfiewifhewihfiehfiwf
kailasmanoj
 
Querying_with_T-SQL_-_03 (1).pptx
Querying_with_T-SQL_-_03 (1).pptx
MAHIN33
 
Querying_with_T-SQL_-_03.pptx
Querying_with_T-SQL_-_03.pptx
MAHIN33
 
Ms sql server ii
Ms sql server ii
Iblesoft
 
askndmf,dskmlf,bvdmk,v nmdsvjkmc,dvjkcmsdcvjnkmd
askndmf,dskmlf,bvdmk,v nmdsvjkmc,dvjkcmsdcvjnkmd
talhahakeem295
 
Tipos de Joins para consultas em banco de dados.pdf
Tipos de Joins para consultas em banco de dados.pdf
PabloLpez168453
 
SQL joins for Database Testing easy .pdf
SQL joins for Database Testing easy .pdf
RohitSharma189763
 
Joins.pptxjjbmmmnnnnnjjjxrhjfluflurulrdudlu
Joins.pptxjjbmmmnnnnnjjjxrhjfluflurulrdudlu
c4x6vgk2n7
 
Assignment 4
Assignment 4
SneaK3
 
Joins and Views.pptx
Joins and Views.pptx
SangitaKabi
 

More from sqlserver content (20)

MS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining tools
sqlserver content
 
MS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data mining
sqlserver content
 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
sqlserver content
 
MS SQL SERVER: Olap cubes and data mining
MS SQL SERVER: Olap cubes and data mining
sqlserver content
 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithm
sqlserver content
 
MS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rules
sqlserver content
 
MS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regression
sqlserver content
 
MS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithm
sqlserver content
 
MS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithm
sqlserver content
 
MS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmx
sqlserver content
 
MS Sql Server: Reporting models
MS Sql Server: Reporting models
sqlserver content
 
MS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating data
sqlserver content
 
MS Sql Server: Reporting introduction
MS Sql Server: Reporting introduction
sqlserver content
 
MS Sql Server: Reporting basics
MS Sql Server: Reporting basics
sqlserver content
 
MS Sql Server: Datamining Introduction
MS Sql Server: Datamining Introduction
sqlserver content
 
MS Sql Server: Business Intelligence
MS Sql Server: Business Intelligence
sqlserver content
 
MS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into Database
sqlserver content
 
MS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With Functions
sqlserver content
 
MS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A Database
sqlserver content
 
MS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base Design
sqlserver content
 
MS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining tools
sqlserver content
 
MS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data mining
sqlserver content
 
MS SQL SERVER: Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
sqlserver content
 
MS SQL SERVER: Olap cubes and data mining
MS SQL SERVER: Olap cubes and data mining
sqlserver content
 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithm
sqlserver content
 
MS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rules
sqlserver content
 
MS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regression
sqlserver content
 
MS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithm
sqlserver content
 
MS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithm
sqlserver content
 
MS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmx
sqlserver content
 
MS Sql Server: Reporting models
MS Sql Server: Reporting models
sqlserver content
 
MS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating data
sqlserver content
 
MS Sql Server: Reporting introduction
MS Sql Server: Reporting introduction
sqlserver content
 
MS Sql Server: Reporting basics
MS Sql Server: Reporting basics
sqlserver content
 
MS Sql Server: Datamining Introduction
MS Sql Server: Datamining Introduction
sqlserver content
 
MS Sql Server: Business Intelligence
MS Sql Server: Business Intelligence
sqlserver content
 
MS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into Database
sqlserver content
 
MS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With Functions
sqlserver content
 
MS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A Database
sqlserver content
 
MS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base Design
sqlserver content
 
Ad

Recently uploaded (20)

Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Supporting the NextGen 911 Digital Transformation with FME
Supporting the NextGen 911 Digital Transformation with FME
Safe Software
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Floods in Valencia: Two FME-Powered Stories of Data Resilience
Safe Software
 
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Seminar: Authentication for a Billion Consumers - Amazon.pptx
FIDO Alliance
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
No-Code Workflows for CAD & 3D Data: Scaling AI-Driven Infrastructure
Safe Software
 
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Integration of Utility Data into 3D BIM Models Using a 3D Solids Modeling Wor...
Safe Software
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
Can We Use Rust to Develop Extensions for PostgreSQL? (POSETTE: An Event for ...
NTT DATA Technology & Innovation
 
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Scaling GenAI Inference From Prototype to Production: Real-World Lessons in S...
Anish Kumar
 
June Patch Tuesday
June Patch Tuesday
Ivanti
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Providing an OGC API Processes REST Interface for FME Flow
Providing an OGC API Processes REST Interface for FME Flow
Safe Software
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Oracle Cloud and AI Specialization Program
Oracle Cloud and AI Specialization Program
VICTOR MAESTRE RAMIREZ
 
Analysis of the changes in the attitude of the news comments caused by knowin...
Analysis of the changes in the attitude of the news comments caused by knowin...
Matsushita Laboratory
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Ad

MS SQL SERVER: Joining Databases

  • 2. SQL JOIN: PrerequisitesBefore 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 recordEg: Consider a dream databaseHere, 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
  • 3. SQL JOIN: PrerequisitesForeign Key:A Foreign key is a field/attribute in one table which is used as a primary key in another tableEg: Consider a dream databaseEvery 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 TableNotice that foreign key entries can repeat( where-as primary key entries can’t!)Foreign KeyPrimary KeyLuck TableRefer-ences
  • 4. SQL JOIN: Prerequisites3.SELECT StatementThe 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 tableNameEg: select * from DreamTable lists the entire tableExample: To select Select the Dreams and Times from this table: select dreams, time from DreamTable
  • 5. 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:
  • 6. SQL JOINAn SQL JOIN command combines records from two or more tables in a database.Types of Joins:
  • 7. Inner JoinAn 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 TaleMoogle Department TableNOTE: 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.
  • 8. Inner JoinIllustration: Now, on performing the Inner joint on the above given table:Moogle Employee TaleMoogle Department TableLet us call the field that is used for joining the tables as joining fieldJoin based on the EmpID fieldNew Table (Inner Joint)
  • 9. Inner JoinThe 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 table2on 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.EmpIDBottom-line: Inner Joints are highly strict. They only accept records that have perfect matching in the other table.
  • 10. Inner JoinThis is an illustration of an Inner Joint:
  • 11. Outer JoinThe 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:
  • 12. Left Outer JoinA 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 TaleMoogle Department TableLeft outer Join based on the EmpID fieldCan you guess what the result might be? Go to the next slide to find out!
  • 13. Left Outer JoinLeft outer Join based on the EmpID field
  • 14. Understanding Left Outer JoinThe 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:StartGet 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 valuesend
  • 15. Understanding Left Outer JoinConfused 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 TableTable1: Gifts table
  • 17. Left Outer JoinHaving 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.
  • 18. Right Outer JoinA 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 TaleMoogle Department TableRight outer Join based on the EmpID fieldNow, Can you deduce what the result might be?
  • 19. Right Outer JoinRight outer Join based on the EmpID fieldNOTE: 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.
  • 20. Right Outer JoinThe SQL command for the right outer join resembles the left outer joinSelect * from Table1 right outer join Table2 on Table1.JoiningFieldName1 = Table2.JoiningFieldName2;Now let us consider the final outer join type: Full outer join
  • 21. Full Outer JoinA 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
  • 23. Full Outer JoinThe SQL command for the full outer join resembles the right outer joinSelect * 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
  • 24. Cross JoinA 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 slideNOTE: 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.
  • 25. Cross JoinIllustration: 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.BoysGirlsBoys X GirlsX
  • 27. Inner Join
  • 28. Outer Join
  • 29. Left Outer Join
  • 30. Right Outer Join
  • 31. Full Outer Join
  • 32. Cross JoinVisit more self help tutorialsPick 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