Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
24 views9 pages

Joins in SQL

The document explains how to query multiple tables using different types of joins in SQL, including inner joins, left joins, right joins, full outer joins, cross joins, and self joins. It provides syntax examples for each type of join and describes their behavior in terms of matching records between tables. The document emphasizes the importance of primary key and foreign key relationships in performing these joins.

Uploaded by

TechnoExcel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views9 pages

Joins in SQL

The document explains how to query multiple tables using different types of joins in SQL, including inner joins, left joins, right joins, full outer joins, cross joins, and self joins. It provides syntax examples for each type of join and describes their behavior in terms of matching records between tables. The document emphasizes the importance of primary key and foreign key relationships in performing these joins.

Uploaded by

TechnoExcel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Click to edit

Master subtitle
style

Querying Multiple Tables with Joins


Join Concepts
Combine rows from multiple tables by specifying matching criteria
◦ Usually based on primary key – foreign key relationships
◦ For example, return rows that combine data from the Employee and SalesOrder tables by matching the
Employee.EmployeeID primary key to the SalesOrder.EmployeeID foreign key

It helps to think of the tables as sets in a Venn diagram

Sales orders
that were
taken by
employees

Employee SalesOrder
Join Syntax
Tables joined by JOIN operator in FROM Clause
Preferred syntax

SELECT ...
FROM Table1 JOIN Table2
ON <on_predicate>;
Inner Joins
Return only rows where a match is found in both input tables
Match rows based on attributes supplied in predicate
If join predicate operator is =, also known as equi-join

SELECT emp.FirstName, ord.Amount Set returned


FROM HR.Employee AS emp by inner join
[INNER] JOIN Sales.SalesOrder AS ord
ON emp.EmployeeID = ord.EmployeeID

Employee SalesOrder
Left Join
The LEFT JOIN keyword returns all records from the left table (table1), and the matching
records from the right table (table2). The result is 0 records from the right side, if there is no
match.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Set returned
by inner join

Employee SalesOrder
Right Join
The RIGHT JOIN keyword returns all records from the right table (table2), and the matching
records from the left table (table1). The result is 0 records from the left side, if there is no
match.

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name; Set returned
by inner join

Employee SalesOrder
Full Join
The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right
(table2) table records.
Tip: Full Outer Join and FULL JOIN are the same.

SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2 Set returned
ON table1.column_name = table2.column_name by inner join
WHERE condition;

Employee SalesOrder
Cross Joins Employee
EmployeeID FirstName
Product
ProductID Name
1 Dan 1 Widget
2 Aisha 2 Gizmo
Combine each row from first table with each row from
second table
All possible combinations output SELECT emp.FirstName, prd.Name
FROM HR.Employee AS emp
Logical foundation for inner and outer joins CROSS JOIN Production.Product AS prd;
◦ Inner join starts with Cartesian product, adds filter
◦ Outer join takes Cartesian output, filtered, adds back non-
Result
matching rows (with NULL placeholders)
FirstName Name
Due to Cartesian product output, not typically a desired Dan Widget
form of join Dan Gizmo
◦ Some useful exceptions: Aisha Widget
◦ Table of numbers, generating data for testing Aisha Gizmo
Self Joins
Compare rows in same table to each other SELECT emp.FirstName AS Employee,
Create two instances of same table in FROM clause man.FirstName AS Manager
◦ At least one alias required FROM HR.Employee AS emp
LEFT JOIN HR.Employee AS man
Example: Return all employees and ON emp.ManagerID = man.EmployeeID;
the name of the employee’s manager

Employee Result
EmployeeID FirstName ManagerID Employee Manager
1 Dan NULL Dan NULL
2 Aisha 1 Aisha Dan
3 Rosie 1 Rosie Dan
4 Naomi 2 Naomi Aisha

You might also like