O slideshow foi denunciado.
Utilizamos seu perfil e dados de atividades no LinkedIn para personalizar e exibir anúncios mais relevantes. Altere suas preferências de anúncios quando desejar.
SUBQUERIES AND JOINS
SUBQUERIES AND JOINS JOINS                                       JOINSSUBQUERIES  With Select                           Jo...
SUBQUERIES AND JOINS  Equi Joins                                         Equi JOINSSUBQUERIES  With Select  With Insert   ...
SUBQUERIES AND JOINS  NonEqui Joins                             Non-Equi JOINSSUBQUERIES  With Select  With Insert        ...
SUBQUERIES AND JOINS   Self Joins                           Self-JOINSSUBQUERIES  With Select  With Insert                ...
SUBQUERIES AND JOINS   Outer Joins                          Outer-JOINSSUBQUERIES  With Select  With Insert               ...
SUBQUERIES AND JOINS   Outer Joins                      LEFT-Outer-JOINSUBQUERIES  With Select  With Insert               ...
SUBQUERIES AND JOINS   Outer Joins                        RIGHT-Outer-JOINSUBQUERIES  With Select  With Insert            ...
SUBQUERIES AND JOINSSUBQUERIES                Subquery is a query written within a query.  With Select  With Insert       ...
SUBQUERIES AND JOINSSUBQUERIES                            Subquery Can be used to.  With Select  With Insert            • ...
SUBQUERIES AND JOINS SUBQUERIES                       Subquery Can be used to.   With Select                              ...
SUBQUERIES AND JOINSSUBQUERIES                               Subquery Can be used to.  With Insert                        ...
SUBQUERIES AND JOINSSUBQUERIES                 Subquery Can be used to.   With update             Subquery with UPDATE Sta...
SUBQUERIES AND JOINSSUBQUERIES                 Subquery Can be used to.                           Subquery with DELETE Sta...
SUBQUERIES AND JOINS                    In co-related Subquery, First outer query                   executes and provide c...
SUBQUERIES AND JOINS                 Set operators combine results from twoSet Operators    or more queries into a single ...
SUBQUERIES AND JOINS                 Set operators combine results from twoSet Operators    or more queries into a single ...
SUBQUERIES AND JOINS                 Set operators combine results from twoSet Operators    or more queries into a single ...
Subqueriesandjoins unit6
Próximos SlideShares
Carregando em…5
×

Subqueriesandjoins unit6

260 visualizações

Publicada em

  • Seja o primeiro a comentar

  • Seja a primeira pessoa a gostar disto

Subqueriesandjoins unit6

  1. 1. SUBQUERIES AND JOINS
  2. 2. SUBQUERIES AND JOINS JOINS JOINSSUBQUERIES With Select Joins are combined With Insert With update With Delete rows from multipleCorrelated sub queriesSet Opertors UNION tables. INTERSECT MINUS To form a join ‘FROM’ clause is used
  3. 3. SUBQUERIES AND JOINS Equi Joins Equi JOINSSUBQUERIES With Select With Insert Equi Joins Returns all rows With update With DeleteCorrelated sub queries from multiple tables,Set Opertors specified in the from clause UNION INTERSECT • = operator is used MINUS • Also called inner joinSELECT e.LoginID SELECT e.LoginIDFROM HumanResources.Employee AS FROM HumanResources.Employee AS ee,sales.SalesPerson as s or INNER JOIN Sales.SalesPerson AS swhere e.BusinessEntityID = s.BusinessEntityID; ON e.BusinessEntityID = s.BusinessEntityID;
  4. 4. SUBQUERIES AND JOINS NonEqui Joins Non-Equi JOINSSUBQUERIES With Select With Insert Non-Equi Joins are similar With update With DeleteCorrelated sub queries to equi-join except for theSet 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. 5. SUBQUERIES AND JOINS Self Joins Self-JOINSSUBQUERIES With Select With Insert Self join is a join of a table With update With DeleteCorrelated 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. 6. SUBQUERIES AND JOINS Outer Joins Outer-JOINSSUBQUERIES With Select With Insert Outer join returns all the With update With Delete rows from one table and theCorrelated sub queries rows from another table thatSet Opertors UNION satisfies the join condition INTERSECT MINUS • Tyeps: LEFT OUTER JOIN RIGHT OUTER JOIN
  7. 7. SUBQUERIES AND JOINS Outer Joins LEFT-Outer-JOINSUBQUERIES With Select With Insert Left Outer join returns With update With DeleteCorrelated sub queries unmatched rows fromSet 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. 8. SUBQUERIES AND JOINS Outer Joins RIGHT-Outer-JOINSUBQUERIES With Select With Insert Right Outer join returns With update With DeleteCorrelated sub queries unmatched rows fromSet 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. 9. SUBQUERIES AND JOINSSUBQUERIES 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 returnsCorrelated sub queries single row to outer querySet 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. 10. SUBQUERIES AND JOINSSUBQUERIES Subquery Can be used to. With Select With Insert • Insert records in target table With update With DeleteCorrelated sub queries • Update records in the target tableSet 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. 11. SUBQUERIES AND JOINS SUBQUERIES Subquery Can be used to. With Select Subquery with SELECT StatementCorrelated sub queries SELECT Name FROM Production.ProductSet Operators WHERE ListPrice in (IN, NOT IN, =) UNION (SELECT ListPrice FROM Production.Product WHERE Name = Chainrin INTERSECT MINUS
  12. 12. SUBQUERIES AND JOINSSUBQUERIES Subquery Can be used to. With Insert Subquery with INSERT StatementCorrelated sub queries Step1: Create a tableSet 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 = NMarketing;
  13. 13. SUBQUERIES AND JOINSSUBQUERIES Subquery Can be used to. With update Subquery with UPDATE Statement With DeleteCorrelated sub queriesSet Operators UNION ??-----Please discover query---? INTERSECT MINUS
  14. 14. SUBQUERIES AND JOINSSUBQUERIES Subquery Can be used to. Subquery with DELETE Statement With DeleteCorrelated sub queriesSet Operators UNION ??-----Please discover query---? INTERSECT MINUS
  15. 15. SUBQUERIES AND JOINS In co-related Subquery, First outer query executes and provide condition or the keyCorrelated sub to inner query(Subquery)queriesSet 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. 16. SUBQUERIES AND JOINS Set operators combine results from twoSet 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. 17. SUBQUERIES AND JOINS Set operators combine results from twoSet 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. 18. SUBQUERIES AND JOINS Set operators combine results from twoSet 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;

×