SlideShare uma empresa Scribd logo
1 de 19
SUBQUERIES AND JOINS
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
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;
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
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
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
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;
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;
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
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');
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
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';
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
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
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);
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;
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;
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;
Subqueriesandjoins unit6

Mais conteúdo relacionado

Mais procurados (6)

Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
Goldilocks and the Three MySQL Queries
Goldilocks and the Three MySQL QueriesGoldilocks and the Three MySQL Queries
Goldilocks and the Three MySQL Queries
 
Creating Views - oracle database
Creating Views - oracle databaseCreating Views - oracle database
Creating Views - oracle database
 
Factory Girl
Factory GirlFactory Girl
Factory Girl
 
SQL
SQLSQL
SQL
 
Manipulating Data Oracle Data base
Manipulating Data Oracle Data baseManipulating Data Oracle Data base
Manipulating Data Oracle Data base
 

Semelhante a Subqueriesandjoins unit6

e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueries
ecomputernotes
 
Sql basics 2
Sql basics 2Sql basics 2
Sql basics 2
rchakra
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
Tony Wong
 
Complete list of all sap abap keywords
Complete list of all sap abap keywordsComplete list of all sap abap keywords
Complete list of all sap abap keywords
Prakash Thirumoorthy
 

Semelhante a Subqueriesandjoins unit6 (20)

MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueries
 
Sql subquery
Sql  subquerySql  subquery
Sql subquery
 
Nested subqueries and subquery chaining in openCypher
Nested subqueries and subquery chaining in openCypherNested subqueries and subquery chaining in openCypher
Nested subqueries and subquery chaining in openCypher
 
Les06
Les06Les06
Les06
 
Les18
Les18Les18
Les18
 
Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)Les06 (using subqueries to solve queries)
Les06 (using subqueries to solve queries)
 
Les06
Les06Les06
Les06
 
How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?How to work with Subquery in Data Mining?
How to work with Subquery in Data Mining?
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
Sql server lab_4
Sql server lab_4Sql server lab_4
Sql server lab_4
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Sql basics 2
Sql basics 2Sql basics 2
Sql basics 2
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
Complete list of all sap abap keywords
Complete list of all sap abap keywordsComplete list of all sap abap keywords
Complete list of all sap abap keywords
 
SQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdfSQL Lesson 6 - Select.pdf
SQL Lesson 6 - Select.pdf
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
Top MNC'S Interview questions and answers
Top MNC'S Interview questions and answersTop MNC'S Interview questions and answers
Top MNC'S Interview questions and answers
 
Using subqueries to solve queries
Using subqueries to solve queriesUsing subqueries to solve queries
Using subqueries to solve queries
 

Subqueriesandjoins unit6

  • 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;