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

Excel Chapter 2
Excel Chapter 2Excel Chapter 2
Excel Chapter 2mindysholder
 
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
 
joins in database
 joins in database joins in database
joins in databaseSultan Arshad
 
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
 
Excel text function
Excel text functionExcel text function
Excel text functionAnthony Obiora
 
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

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 JoinSouma Maiti
 
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 analysisSanSan149
 
Day-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxDay-2 SQL Theory_V1.pptx
Day-2 SQL Theory_V1.pptxuzmasulthana3
 
SQL PPT on joins
SQL PPT on joinsSQL PPT on joins
SQL PPT on joinsVijaysukumarV
 
Assignment 4
Assignment 4Assignment 4
Assignment 4SneaK3
 
Presentation of Joins In Database
Presentation of Joins In DatabasePresentation of Joins In Database
Presentation of Joins In DatabaseShashank Pathak
 
Sql(structured query language)
Sql(structured query language)Sql(structured query language)
Sql(structured query language)Ishucs
 
Database design normalization note and exercise
Database design normalization note and exerciseDatabase design normalization note and exercise
Database design normalization note and exercisedilnawaz56788
 
Advance database system(part 8)
Advance database system(part 8)Advance database system(part 8)
Advance database system(part 8)Abdullah Khosa
 
Dbms interview questions
Dbms interview questionsDbms interview questions
Dbms interview questionsambika93
 

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
 
Dbms interview questions
Dbms interview questionsDbms interview questions
Dbms interview questions
 
SQL Tips Joins.pptx
SQL Tips Joins.pptxSQL Tips Joins.pptx
SQL Tips Joins.pptx
 

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:Creating Views
MS SQLSERVER:Creating ViewsMS SQLSERVER:Creating Views
MS SQLSERVER:Creating Viewssqlserver 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

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Recently uploaded (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

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.