Professional Documents
Culture Documents
Fundamentals of Joins
Joins:
Select Specific Columns from Multiple Tables
JOIN keyword specifies that tables are joined and how to join them
Outer Join
Cross Join
Self Join
Example:
SELECT e.LoginID FROM HumanResources.Employee AS e INNER JOIN Sales.SalesPerson AS s ON e.BusinessEntityID = s.BusinessEntityID
Result Set:
LoginID ------------------------adventure-works\syed0 adventure-works\david8 adventure-works\garrett1 ... (17 row(s) affected)
Example:
SELECT p.Name, pr.ProductReviewID FROM Production.Product p LEFT OUTER JOIN Production.ProductReview pr ON p.ProductID = pr.ProductID
Result Set:
Name ProductReviewID ---------------------------------Adjustable Race NULL Bearing Ball NULL ... (505 row(s) affected)
Example:
SELECT p.BusinessEntityID, t.Name AS Territory FROM Sales.SalesPerson p CROSS JOIN Sales.SalesTerritory t ORDER BY p.BusinessEntityID
Result Set:
BusinessEntityID Territory ---------------------------274 Northwest 274 Northeast ... (170 row(s) affected) Use CROSS JOINs with caution if you do not need a true Cartesian Product
Is defined as all possible combinations of rows in all tables Results in a rowset containing the number of rows in the first table times the number of rows in the second Can result in huge result sets that take several hours to complete!
Example:
SELECT p.Name, v.Name FROM Production.Product p JOIN Purchasing.ProductVendor pv ON p.ProductID = pv.ProductID JOIN Purchasing.Vendor v ON pv.BusinesEntityID = v.BusinessEntityID WHERE ProductSubcategoryID = 15 ORDER BY v.Name
Result Set:
Name Name ----------------------------------------------LL Mountain Seat/Saddle Chicago City Saddles ML Mountain Seat/Saddle Chicago City Saddles ... (18 row(s) affected)
Example:
SELECT DISTINCT pv1.ProductID, pv1.BusinessEntityID FROM Purchasing.ProductVendor pv1 INNER JOIN Purchasing.ProductVendor pv2 ON pv1.ProductID = pv2.ProductID AND pv1.BusinessEntityID <> pv2.BusinessEntityID ORDER BY pv1.ProductID
Result Set:
ProductID BusinessEntityID -----------------------------317 1578 317 1678 ... (347 row(s) affected)
Example:
SELECT DISTINCT p1.ProductSubcategoryID, p1.ListPrice FROM Production.Product p1 INNER JOIN Production.Product p2 ON p1.ProductSubcateogoryID = p2.ProductSubcategoryID AND p1.ListPrice <> p2.ListPrice WHERE p1.ListPrice < $15 AND p2.ListPrice < $15 ORDER BY ProductSubcategoryID
Result Set:
ProductSubcateogoryID ListPrice ----------------------------------23 8.99 23 9.50 ... (8 row(s) affected)
Operators
and INTERSECT
Operators
Result Set:
columna columnb -----------------100 test 100 test ... (8 row(s) affected)
The number and order of columns must be the same in all queries and all data types must be compatible
EXCEPT Example:
SELECT ProductID FROM Production.Product EXCEPT SELECT ProductID FROM Production.WorkOrder
Result Sets
ProductID -----------------429 ... (266 row(s) affected)
INTERSECT Example:
SELECT ProductID FROM Production.Product INTERSECT SELECT ProductID FROM Production.WorkOrder
ProductID -----------------3 ... (238 row(s) affected)
EXCEPT, INTERSECT, and UNION are evaluated in the context of the following precedence:
1 2 3
Expressions in parentheses
The INTERSECT operand EXCEPT and UNION evaluated from Left to Right based on their position in the expression
Result Sets TOP Example: SELECT TOP (15) FirstName, LastName FROM Person.Person
FirstName LastName -------------------Syed Abbas Catherine Abel ... (15 row(s) affected)
FirstName LastName -------------------Eduardo Barnes Edward Barnes ... (199 row(s) affected)
Combines the results of two or more SELECT statements into a single result set
Compares the results of two or more SELECT statements and return distinct values
TOP
Specifies that only the first set of rows will be returned from the query result
TABLESAMPLE
Limits the number of rows returned from a table in the FROM clause to a sample number or PERCENT of rows
Logon information
Lab Scenario
You are a database developer at Adventure Works. You have
been asked by the various managers to prepare several reports for use in the quarterly financial statements being produced by the company. To create these reports you will use several different joins and join operators.
Lab Review
What results did the Inner Join in Exercise 1 return? What results did the Left Outer Join and Right Outer Join
in Exercise 1 return?