2. SUBQUERIES AND JOINS
JOINS JOINS
SUBQUERIES
With Select
Joins are combined
With Insert
With update
With Delete rows from multiple
Correlated sub queries
Set Opertors
UNION
tables.
INTERSECT
MINUS
To form a join ‘FROM’ clause is used
3. SUBQUERIES AND JOINS
Equi Joins Equi JOINS
SUBQUERIES
With Select
With Insert
Equi Joins Returns all rows
With update
With Delete
Correlated sub queries
from multiple tables,
Set Opertors
specified in the from clause
UNION
INTERSECT
• = operator is used
MINUS • Also called inner join
SELECT e.LoginID SELECT e.LoginID
FROM HumanResources.Employee AS FROM HumanResources.Employee AS e
e,sales.SalesPerson as s or INNER JOIN Sales.SalesPerson AS s
where e.BusinessEntityID = s.BusinessEntityID; ON e.BusinessEntityID = s.BusinessEntityID;
4. SUBQUERIES AND JOINS
NonEqui Joins Non-Equi JOINS
SUBQUERIES
With Select
With Insert
Non-Equi Joins are similar
With update
With Delete
Correlated sub queries
to equi-join except for the
Set Opertors operators used.
UNION
INTERSECT
<>, >, < operators are used
MINUS
SELECT p1.ProductSubcategoryID, p1.ListPrice
FROM Production.Product p1,Production.Product p2
WHERE p1.ListPrice <> p2.ListPrice and p1.ListPrice < $15
AND p2.ListPrice < $15
5. SUBQUERIES AND JOINS
Self Joins Self-JOINS
SUBQUERIES
With Select
With Insert
Self join is a join of a table
With update
With Delete
Correlated sub queries
to itself using alias names.
Set Opertors
UNION
INTERSECT
MINUS
SELECT p1.ProductSubcategoryID, p1.ListPrice
FROM Production.Product p1,Production.Product p2
WHERE p1.ListPrice = p2.ListPrice
6. SUBQUERIES AND JOINS
Outer Joins Outer-JOINS
SUBQUERIES
With Select
With Insert
Outer join returns all the
With update
With Delete
rows from one table and the
Correlated sub queries rows from another table that
Set Opertors
UNION satisfies the join condition
INTERSECT
MINUS • Tyeps: LEFT OUTER JOIN RIGHT OUTER JOIN
7. SUBQUERIES AND JOINS
Outer Joins LEFT-Outer-JOIN
SUBQUERIES
With Select
With Insert
Left Outer join returns
With update
With Delete
Correlated sub queries
unmatched rows from
Set Opertors first or left table.
UNION
INTERSECT
MINUS
SELECT p.Name, pr.ProductReviewID
FROM Production.Product p
LEFT OUTER JOIN
Production.ProductReview pr
ON p.ProductID = pr.ProductID;
8. SUBQUERIES AND JOINS
Outer Joins RIGHT-Outer-JOIN
SUBQUERIES
With Select
With Insert
Right Outer join returns
With update
With Delete
Correlated sub queries
unmatched rows from
Set Opertors last or right table.
UNION
INTERSECT
MINUS
SELECT st.Name AS Territory,
sp.BusinessEntityID
FROM Sales.SalesTerritory st
RIGHT OUTER JOIN Sales.SalesPerson sp
ON st.TerritoryID = sp.TerritoryID;
9. SUBQUERIES AND JOINS
SUBQUERIES Subquery is a query written within a query.
With Select
With Insert • Written in WHERE or HAVING clauses
With update
With Delete • In single row subquery, Inner query returns
Correlated sub queries
single row to outer query
Set Operators • In Multi row subquery, Inner query returns multiple
UNION
INTERSECT
rows to outer query
MINUS • Single row subquery uses =, >,<,<> operators
• Multi row subquery uses IN operator
10. SUBQUERIES AND JOINS
SUBQUERIES Subquery Can be used to.
With Select
With Insert • Insert records in target table
With update
With Delete
Correlated sub queries
• Update records in the target table
Set Operators • Ceate views
UNION
INTERSECT • Provide values for condition in the WHERE,
MINUS
HAVING, IN, SELECT, UPDATE and DELETE statemens
SELECT Name FROM Production.Product
WHERE ListPrice =
(SELECT ListPrice FROM Production.Product WHERE Name = 'Chainring Bolts');
11. SUBQUERIES AND JOINS
SUBQUERIES Subquery Can be used to.
With Select
Subquery with SELECT Statement
Correlated sub queries SELECT Name FROM Production.Product
Set Operators WHERE ListPrice in (IN, NOT IN, =)
UNION (SELECT ListPrice FROM Production.Product WHERE Name = 'Chainrin
INTERSECT
MINUS
12. SUBQUERIES AND JOINS
SUBQUERIES Subquery Can be used to.
With Insert
Subquery with INSERT Statement
Correlated sub queries Step1: Create a table
Set Operators CREATE TABLE MySalesReason (
UNION SalesReasonID int NOT NULL,
INTERSECT Name nvarchar(50),
MINUS ModifiedDate datetime);
Step2: Subquery to Select rows from existing table and insert into newly created table
INSERT INTO MySalesReason
SELECT SalesReasonID, Name, ModifiedDate Setp3: retrieving data
FROM Sales.SalesReason Select * from mysalesreason
WHERE ReasonType = N'Marketing';
13. SUBQUERIES AND JOINS
SUBQUERIES Subquery Can be used to.
With update Subquery with UPDATE Statement
With Delete
Correlated sub queries
Set Operators
UNION ??-----Please discover query---?
INTERSECT
MINUS
14. SUBQUERIES AND JOINS
SUBQUERIES Subquery Can be used to.
Subquery with DELETE Statement
With Delete
Correlated sub queries
Set Operators
UNION ??-----Please discover query---?
INTERSECT
MINUS
15. SUBQUERIES AND JOINS
In co-related Subquery, First outer query
executes and provide condition or the key
Correlated sub to inner query(Subquery)
queries
Set Operators
UNION
INTERSECT
MINUS
SELECT ProductID, OrderQty
FROM Sales.SalesOrderDetail s1
WHERE s1.OrderQty <
(SELECT AVG (s2.OrderQty)
FROM Sales.SalesOrderDetail s2
WHERE s2.ProductID = s1.ProductID);
16. SUBQUERIES AND JOINS
Set operators combine results from two
Set Operators or more queries into a single result set.
UNION
INTERSECT
MINUS Types: Union Intersect Minus
Union Merges queries output eliminating duplicates
SELECT ProductID
FROM Production.Product
UNION
SELECT ProductID
FROM Production.WorkOrder;
17. SUBQUERIES AND JOINS
Set operators combine results from two
Set Operators or more queries into a single result set.
UNION
INTERSECT
MINUS Types: Union Intersect Minus
Intersect includes common rows that are retrieved by each
query
SELECT ProductID
FROM Production.Product
INTERSECT
SELECT ProductID
FROM Production.WorkOrder;
18. SUBQUERIES AND JOINS
Set operators combine results from two
Set Operators or more queries into a single result set.
UNION
INTERSECT
MINUS Types: Union Intersect Minus
Minus includes only rows retrieved by first query, removing
rows common in second query.
SELECT ProductID
FROM Production.Product
Minus
SELECT ProductID
FROM Production.WorkOrder;