SlideShare a Scribd company logo
1 of 27
6 SQL SERVER: JOININGDATABASETABLES
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
Understanding Left Outer Join
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
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
Summary 6. Joining Databases ,[object Object]
  Inner Join

More Related Content

What's hot

Office productivity tools
Office productivity toolsOffice productivity tools
Office productivity toolsUllees Selim
 
Common MS Excel and MS Excel 2013 useful tricks. By Ashot Engibaryan
Common MS Excel and MS Excel 2013 useful tricks. By Ashot EngibaryanCommon MS Excel and MS Excel 2013 useful tricks. By Ashot Engibaryan
Common MS Excel and MS Excel 2013 useful tricks. By Ashot EngibaryanAshot Engibaryan
 
Ms Excel Basic to Advance Tutorial
Ms Excel Basic to Advance TutorialMs Excel Basic to Advance Tutorial
Ms Excel Basic to Advance TutorialBikal Shrestha
 
Introduction to Excel - Excel 2013 Tutorial
Introduction to Excel - Excel 2013 TutorialIntroduction to Excel - Excel 2013 Tutorial
Introduction to Excel - Excel 2013 TutorialSpreadsheetTrainer
 
Excel Tutorials - Random Value Selection from a List
Excel Tutorials - Random Value Selection from a ListExcel Tutorials - Random Value Selection from a List
Excel Tutorials - Random Value Selection from a ListMerve Nur Taş
 
Introduction to spreadsheets
Introduction to spreadsheetsIntroduction to spreadsheets
Introduction to spreadsheetsCasey Robertson
 
Ms excel 2013 data management
Ms excel 2013 data managementMs excel 2013 data management
Ms excel 2013 data managementJesus Obenita Jr.
 
Excel Tutorials - VLOOKUP and HLOOKUP Functions
Excel Tutorials - VLOOKUP and HLOOKUP FunctionsExcel Tutorials - VLOOKUP and HLOOKUP Functions
Excel Tutorials - VLOOKUP and HLOOKUP FunctionsMerve Nur Taş
 
Spreadsheet advanced functions ppt (2)
Spreadsheet advanced functions ppt (2)Spreadsheet advanced functions ppt (2)
Spreadsheet advanced functions ppt (2)Tammy Carter
 
Etech. mitch. [autosaved]
Etech. mitch. [autosaved]Etech. mitch. [autosaved]
Etech. mitch. [autosaved]MaridelBajeta
 

What's hot (19)

Excel Chapter 2
Excel Chapter 2Excel Chapter 2
Excel Chapter 2
 
Join
JoinJoin
Join
 
Office productivity tools
Office productivity toolsOffice productivity tools
Office productivity tools
 
Common MS Excel and MS Excel 2013 useful tricks. By Ashot Engibaryan
Common MS Excel and MS Excel 2013 useful tricks. By Ashot EngibaryanCommon MS Excel and MS Excel 2013 useful tricks. By Ashot Engibaryan
Common MS Excel and MS Excel 2013 useful tricks. By Ashot Engibaryan
 
Sql join
Sql  joinSql  join
Sql join
 
Mastering Excel Formulas and Functions
Mastering Excel Formulas and FunctionsMastering Excel Formulas and Functions
Mastering Excel Formulas and Functions
 
Ms Excel Basic to Advance Tutorial
Ms Excel Basic to Advance TutorialMs Excel Basic to Advance Tutorial
Ms Excel Basic to Advance Tutorial
 
Introduction to Excel - Excel 2013 Tutorial
Introduction to Excel - Excel 2013 TutorialIntroduction to Excel - Excel 2013 Tutorial
Introduction to Excel - Excel 2013 Tutorial
 
Excel Tutorials - Random Value Selection from a List
Excel Tutorials - Random Value Selection from a ListExcel Tutorials - Random Value Selection from a List
Excel Tutorials - Random Value Selection from a List
 
Introduction to spreadsheets
Introduction to spreadsheetsIntroduction to spreadsheets
Introduction to spreadsheets
 
joins in database
 joins in database joins in database
joins in database
 
Ms excel 2013 data management
Ms excel 2013 data managementMs excel 2013 data management
Ms excel 2013 data management
 
SQL
SQLSQL
SQL
 
Excel Tutorials - VLOOKUP and HLOOKUP Functions
Excel Tutorials - VLOOKUP and HLOOKUP FunctionsExcel Tutorials - VLOOKUP and HLOOKUP Functions
Excel Tutorials - VLOOKUP and HLOOKUP Functions
 
Real World Excel Formulas
Real World Excel FormulasReal World Excel Formulas
Real World Excel Formulas
 
Spreadsheet advanced functions ppt (2)
Spreadsheet advanced functions ppt (2)Spreadsheet advanced functions ppt (2)
Spreadsheet advanced functions ppt (2)
 
Excel text function
Excel text functionExcel text function
Excel text function
 
Sql joins
Sql joinsSql joins
Sql joins
 
Etech. mitch. [autosaved]
Etech. mitch. [autosaved]Etech. mitch. [autosaved]
Etech. mitch. [autosaved]
 

Viewers also liked

MS SQLSERVER:Creating A Database
MS SQLSERVER:Creating A DatabaseMS SQLSERVER:Creating A Database
MS SQLSERVER:Creating A Databasesqlserver content
 
MS Sql Server: Datamining Introduction
MS Sql Server: Datamining IntroductionMS Sql Server: Datamining Introduction
MS Sql Server: Datamining Introductionsqlserver content
 
MS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With FunctionsMS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With Functionssqlserver content
 
MS SQL SERVER: Creating A Database
MS SQL SERVER: Creating A DatabaseMS SQL SERVER: Creating A Database
MS SQL SERVER: Creating A Databasesqlserver content
 
MS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rulesMS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rulessqlserver content
 
MS Sql Server: Business Intelligence
MS Sql Server: Business IntelligenceMS Sql Server: Business Intelligence
MS Sql Server: Business Intelligencesqlserver content
 
MS SQLSERVER:Manipulating Database
MS SQLSERVER:Manipulating DatabaseMS SQLSERVER:Manipulating Database
MS SQLSERVER:Manipulating Databasesqlserver content
 
MS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regressionMS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regressionsqlserver content
 
MS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining toolsMS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining toolssqlserver content
 
MS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into DatabaseMS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into Databasesqlserver content
 
MS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A DatabaseMS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A Databasesqlserver content
 
MS Sql Server: Reporting introduction
MS Sql Server: Reporting introductionMS Sql Server: Reporting introduction
MS Sql Server: Reporting introductionsqlserver content
 
MS SQL SERVER: Introduction To Database Concepts
MS SQL SERVER: Introduction To Database ConceptsMS SQL SERVER: Introduction To Database Concepts
MS SQL SERVER: Introduction To Database Conceptssqlserver content
 
MS Sql Server: Reporting basics
MS Sql  Server: Reporting basicsMS Sql  Server: Reporting basics
MS Sql Server: Reporting basicssqlserver content
 
MS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data miningMS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data miningsqlserver content
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functionsAmrit Kaur
 
MS SQL SERVER: Getting Started With Sql Server 2008
MS SQL SERVER: Getting Started With Sql Server 2008MS SQL SERVER: Getting Started With Sql Server 2008
MS SQL SERVER: Getting Started With Sql Server 2008sqlserver content
 
Sql server performance tuning
Sql server performance tuningSql server performance tuning
Sql server performance tuningngupt28
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-PresentationChuck Walker
 

Viewers also liked (20)

MS SQLSERVER:Creating A Database
MS SQLSERVER:Creating A DatabaseMS SQLSERVER:Creating A Database
MS SQLSERVER:Creating A Database
 
MS Sql Server: Datamining Introduction
MS Sql Server: Datamining IntroductionMS Sql Server: Datamining Introduction
MS Sql Server: Datamining Introduction
 
MS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With FunctionsMS SQLSERVER:Doing Calculations With Functions
MS SQLSERVER:Doing Calculations With Functions
 
MS SQL SERVER: Creating A Database
MS SQL SERVER: Creating A DatabaseMS SQL SERVER: Creating A Database
MS SQL SERVER: Creating A Database
 
MS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rulesMS SQL SERVER: Microsoft sequence clustering and association rules
MS SQL SERVER: Microsoft sequence clustering and association rules
 
MS Sql Server: Business Intelligence
MS Sql Server: Business IntelligenceMS Sql Server: Business Intelligence
MS Sql Server: Business Intelligence
 
MS SQLSERVER:Manipulating Database
MS SQLSERVER:Manipulating DatabaseMS SQLSERVER:Manipulating Database
MS SQLSERVER:Manipulating Database
 
MS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regressionMS SQL SERVER: Neural network and logistic regression
MS SQL SERVER: Neural network and logistic regression
 
MS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining toolsMS SQL SERVER: Using the data mining tools
MS SQL SERVER: Using the data mining tools
 
MS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into DatabaseMS SQLSERVER:Feeding Data Into Database
MS SQLSERVER:Feeding Data Into Database
 
MS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A DatabaseMS SQLSERVER:Retrieving Data From A Database
MS SQLSERVER:Retrieving Data From A Database
 
MS Sql Server: Reporting introduction
MS Sql Server: Reporting introductionMS Sql Server: Reporting introduction
MS Sql Server: Reporting introduction
 
MS SQL SERVER: Introduction To Database Concepts
MS SQL SERVER: Introduction To Database ConceptsMS SQL SERVER: Introduction To Database Concepts
MS SQL SERVER: Introduction To Database Concepts
 
MS Sql Server: Reporting basics
MS Sql  Server: Reporting basicsMS Sql  Server: Reporting basics
MS Sql Server: Reporting basics
 
MS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data miningMS SQL SERVER: SSIS and data mining
MS SQL SERVER: SSIS and data mining
 
SQL Server Stored procedures
SQL Server Stored proceduresSQL Server Stored procedures
SQL Server Stored procedures
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
MS SQL SERVER: Getting Started With Sql Server 2008
MS SQL SERVER: Getting Started With Sql Server 2008MS SQL SERVER: Getting Started With Sql Server 2008
MS SQL SERVER: Getting Started With Sql Server 2008
 
Sql server performance tuning
Sql server performance tuningSql server performance tuning
Sql server performance tuning
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 

Similar to MS SQLSERVER:Joining Databases

Similar to MS SQLSERVER:Joining Databases (20)

Join in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer JoinJoin in SQL - Inner, Self, Outer Join
Join in SQL - Inner, Self, Outer Join
 
joins and subqueries in big data analysis
joins and subqueries in big data analysisjoins and subqueries in big data analysis
joins and subqueries in big data analysis
 
database .pptx
database .pptxdatabase .pptx
database .pptx
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptx
 
SQL PPT on joins
SQL PPT on joinsSQL PPT on joins
SQL PPT on joins
 
Assignment 4
Assignment 4Assignment 4
Assignment 4
 
JOINS.pptx
JOINS.pptxJOINS.pptx
JOINS.pptx
 
Joins
JoinsJoins
Joins
 
Presentation of Joins In Database
Presentation of Joins In DatabasePresentation of Joins In Database
Presentation of Joins In Database
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)
 
Joins
JoinsJoins
Joins
 
Database design normalization note and exercise
Database design normalization note and exerciseDatabase design normalization note and exercise
Database design normalization note and exercise
 
Advance database system(part 8)
Advance database system(part 8)Advance database system(part 8)
Advance database system(part 8)
 
Mysql joins
Mysql joinsMysql joins
Mysql joins
 
SQL JOIN.pptx
SQL JOIN.pptxSQL JOIN.pptx
SQL JOIN.pptx
 
Sql joins
Sql joinsSql joins
Sql joins
 
types of SQL Joins
 types of SQL Joins types of SQL Joins
types of SQL Joins
 
V19 join method-c
V19 join method-cV19 join method-c
V19 join method-c
 
SQL Tips Joins.pptx
SQL Tips Joins.pptxSQL Tips Joins.pptx
SQL Tips Joins.pptx
 
SQL Server Learning Drive
SQL Server Learning Drive SQL Server Learning Drive
SQL Server Learning Drive
 

More from sqlserver content

MS SQL SERVER: Programming sql server data mining
MS SQL SERVER:  Programming sql server data miningMS SQL SERVER:  Programming sql server data mining
MS SQL SERVER: Programming sql server data miningsqlserver content
 
MS SQL SERVER: Olap cubes and data mining
MS SQL SERVER:  Olap cubes and data miningMS SQL SERVER:  Olap cubes and data mining
MS SQL SERVER: Olap cubes and data miningsqlserver content
 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithmMS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithmsqlserver content
 
MS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithmMS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithmsqlserver content
 
MS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithmMS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithmsqlserver content
 
MS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmxMS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmxsqlserver content
 
MS Sql Server: Reporting models
MS Sql Server: Reporting modelsMS Sql Server: Reporting models
MS Sql Server: Reporting modelssqlserver content
 
MS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating dataMS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating datasqlserver content
 
MS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A DatabaseMS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A Databasesqlserver content
 
MS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base DesignMS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base Designsqlserver content
 
MS SQLSERVER:Advanced Query Concepts Copy
MS SQLSERVER:Advanced Query Concepts   CopyMS SQLSERVER:Advanced Query Concepts   Copy
MS SQLSERVER:Advanced Query Concepts Copysqlserver content
 
MS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And ProceduresMS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And Proceduressqlserver content
 
MS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And ProceduresMS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And Proceduressqlserver content
 
MS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A DatabaseMS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A Databasesqlserver content
 
MS SQL SERVER: Manipulating Database
MS SQL SERVER: Manipulating DatabaseMS SQL SERVER: Manipulating Database
MS SQL SERVER: Manipulating Databasesqlserver content
 
MS SQL SERVER: Joining Databases
MS SQL SERVER: Joining DatabasesMS SQL SERVER: Joining Databases
MS SQL SERVER: Joining Databasessqlserver content
 

More from sqlserver content (17)

MS SQL SERVER: Programming sql server data mining
MS SQL SERVER:  Programming sql server data miningMS SQL SERVER:  Programming sql server data mining
MS SQL SERVER: Programming sql server data mining
 
MS SQL SERVER: Olap cubes and data mining
MS SQL SERVER:  Olap cubes and data miningMS SQL SERVER:  Olap cubes and data mining
MS SQL SERVER: Olap cubes and data mining
 
MS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithmMS SQL SERVER: Microsoft time series algorithm
MS SQL SERVER: Microsoft time series algorithm
 
MS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithmMS SQL SERVER: Microsoft naive bayes algorithm
MS SQL SERVER: Microsoft naive bayes algorithm
 
MS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithmMS SQL SERVER: Decision trees algorithm
MS SQL SERVER: Decision trees algorithm
 
MS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmxMS SQL Server: Data mining concepts and dmx
MS SQL Server: Data mining concepts and dmx
 
MS Sql Server: Reporting models
MS Sql Server: Reporting modelsMS Sql Server: Reporting models
MS Sql Server: Reporting models
 
MS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating dataMS Sql Server: Reporting manipulating data
MS Sql Server: Reporting manipulating data
 
MS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A DatabaseMS SQLSERVER:Deleting A Database
MS SQLSERVER:Deleting A Database
 
MS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base DesignMS SQLSERVER:Customizing Your D Base Design
MS SQLSERVER:Customizing Your D Base Design
 
MS SQLSERVER:Creating Views
MS SQLSERVER:Creating ViewsMS SQLSERVER:Creating Views
MS SQLSERVER:Creating Views
 
MS SQLSERVER:Advanced Query Concepts Copy
MS SQLSERVER:Advanced Query Concepts   CopyMS SQLSERVER:Advanced Query Concepts   Copy
MS SQLSERVER:Advanced Query Concepts Copy
 
MS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And ProceduresMS SQLSERVER:Sql Functions And Procedures
MS SQLSERVER:Sql Functions And Procedures
 
MS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And ProceduresMS SQL SERVER: Sql Functions And Procedures
MS SQL SERVER: Sql Functions And Procedures
 
MS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A DatabaseMS SQL SERVER: Retrieving Data From A Database
MS SQL SERVER: Retrieving Data From A Database
 
MS SQL SERVER: Manipulating Database
MS SQL SERVER: Manipulating DatabaseMS SQL SERVER: Manipulating Database
MS SQL SERVER: Manipulating Database
 
MS SQL SERVER: Joining Databases
MS SQL SERVER: Joining DatabasesMS SQL SERVER: Joining Databases
MS SQL SERVER: Joining Databases
 

Recently uploaded

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 

Recently uploaded (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 

MS SQLSERVER:Joining Databases

  • 1. 6 SQL SERVER: JOININGDATABASETABLES
  • 2. 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
  • 3. 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
  • 4. 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
  • 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 JOIN An SQL JOIN command combines records from two or more tables in a database. Types of Joins:
  • 7. 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.
  • 8. 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)
  • 9. 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.
  • 10. Inner Join This is an illustration of an Inner Joint:
  • 11. 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:
  • 12. 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!
  • 13. Left Outer Join Left outer Join based on the EmpID field
  • 14. 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
  • 15. 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
  • 17. 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.
  • 18. 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?
  • 19. 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.
  • 20. 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
  • 21. 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
  • 23. 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
  • 24. 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.
  • 25. 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
  • 26.
  • 27. Inner Join
  • 28. Outer Join
  • 29. Left Outer Join
  • 30. Right Outer Join
  • 31. Full Outer Join
  • 32.