Presentation is loading. Please wait.

Presentation is loading. Please wait.

20761B 12: Using Set Operators Module 12   Using Set Operators.

Similar presentations


Presentation on theme: "20761B 12: Using Set Operators Module 12   Using Set Operators."— Presentation transcript:

1 20761B 12: Using Set Operators Module 12 Using Set Operators

2 20761B Module Overview 12: Using Set Operators Using APPLY

3 Lesson 1: Writing Queries with the UNION Operator
20761B Lesson 1: Writing Queries with the UNION Operator 12: Using Set Operators Demonstration: Using UNION and UNION ALL Question True or false? The results from a UNION query can contain duplicate rows. ( )False ( )True Answer (√)False When combining the output of two sets, UNION and UNION ALL queries cannot include rows with NULL values, because NULL values cannot be compared.

4 Interactions Between Sets
12: Using Set Operators The results of two input queries may be further manipulated Sets may be combined, compared, or operated against each other Both sets must have the same number of compatible columns ORDER BY not allowed in input queries, but may be used for result of set operation NULLs considered equal when comparing sets This module builds on the set theory discussion from Module 2 of this course. Consider calling attention to the note at the bottom of this topic, along with the additional references provided in the module Introduction to Transact-SQL Querying. More information on set theory and its application to SQL Server queries can be found in T-SQL Querying (Developer Reference) - Itzik Ben-Gan et al (Microsoft Press, 2015). <SELECT query_1> <set_operator> <SELECT query_2> [ORDER BY <sort_list>];

5 Using the UNION Operator
20761B Using the UNION Operator 12: Using Set Operators UNION returns a result set of distinct rows combined from both input sets Duplicates are removed during query processing (affects performance) The Venn diagram uses the blue to represent rows returned from the Employees table. The blue color signifies that the query will return all records from Employees and those that also occur in Customers. Note that duplicates are filtered by UNION, regardless of which input result sets they occur in. To be filtered, duplicate data does not have to exist in both input result sets. If you are comfortable with execution plans and feel that the class won’t be too confused, consider displaying and comparing the execution plans from the UNION example in this topic and the UNION ALL example in the next one. Employees Customers -- only distinct rows from both queries are returned SELECT country, region, city FROM HR.Employees UNION SELECT country, region, city FROM Sales.Customers;

6 Using the UNION ALL Operator
20761B Using the UNION ALL Operator 12: Using Set Operators UNION ALL returns a result set with all rows from both input sets To avoid the performance penalty caused by filtering duplicates, use UNION ALL over UNION whenever requirements allow it -- all rows from both queries will be returned SELECT country, region, city FROM HR.Employees UNION ALL SELECT country, region, city FROM Sales.Customers;

7 Lesson 2: Using EXCEPT and INTERSECT
20761B Lesson 2: Using EXCEPT and INTERSECT 12: Using Set Operators Demonstration: Using EXCEPT and INTERSECT Question You have a table of employees and a table of customers, both of which contain a column holding the name of the country where the customer or employee is located. You want to know which countries have at least one customer and at least one employee. Which set operator should you use? ( )Option 1: UNION ALL ( )Option 2: UNION ( )Option 3: EXCEPT ( )Option 4: INTERSECT ( )Option 5: None of the above Answer (√) Option -2: INTERSECT

8 Using the INTERSECT Operator
20761B Using the INTERSECT Operator 12: Using Set Operators INTERSECT returns the distinct set of rows that appear in both input result sets The Venn diagram uses the darker blue color to represent rows returned from the Employees table. All rows from Employees will be returned, when they are also found in the Customers table. Employees Customers -- only rows that exist in both queries will be returned SELECT country, region, city FROM HR.Employees INTERSECT SELECT country, region, city FROM Sales.Customers;

9 Using the EXCEPT Operator
20761B Using the EXCEPT Operator 12: Using Set Operators EXCEPT returns only distinct rows that appear in the left set but not the right The order in which sets are specified matters The Venn diagram uses the darker color (blue) to represent rows returned from the Employees table. All rows from Employees will be returned, except those found in the Customers table. Employees Customers -- only rows from Employees will be returned SELECT country, region, city FROM HR.Employees EXCEPT SELECT country, region, city FROM Sales.Customers;

10 Demonstration: Using CROSS APPLY and OUTER APPLY
20761B Lesson 3: Using APPLY 12: Using Set Operators Demonstration: Using CROSS APPLY and OUTER APPLY Students may have conceptual difficulties with APPLY. It may be helpful to discuss the logical similarity between CROSS APPLY and INNER JOIN, OUTER APPLY and OUTER JOIN before moving on to examples using table-valued functions (TVFs). It might also be useful to use the demonstration file examples as you go through the next three topics, rather than wait until the end. Question What is the difference between CROSS APPLY and CROSS JOIN? Answer CROSS JOIN returns all the possible combinations of the left and right table sources; CROSS APPLY returns only the values from the left table source where a value is found in the right table source.

11 Using the APPLY Operator
20761B Using the APPLY Operator 12: Using Set Operators APPLY is a table operator used in the FROM clause Two forms—CROSS APPLY and OUTER APPLY Operates on two input tables, referred to as left and right Right table may be any table expression including a derived table or a table-valued function Remind students that use of “left table” and “right table” in the context of APPLY refers to the order of the tables as listed in the FROM clause, much as in a JOIN. SELECT <column_list> FROM <left_table_source> AS <alias> [CROSS]|[OUTER] APPLY <right_table_source> AS <alias>;

12 The CROSS APPLY Operator
20761B The CROSS APPLY Operator 12: Using Set Operators CROSS APPLY applies the right table source to each row in the left table source Only rows with results in both the left table source and right table source are returned Most INNER JOIN statements can be rewritten as CROSS APPLY statements USE TSQL; GO SELECT o.orderid, o.orderdate, od.productid, od.unitprice, od.qty FROM Sales.Orders AS o INNER JOIN Sales.OrderDetails AS od ON o.orderid = od.orderid ; CROSS APPLY ( SELECT productid, unitprice, qty FROM Sales.OrderDetails AS so WHERE so.orderid = o.orderid ) AS od; If you are comfortable with query execution plans and feel that it won’t confuse the class, demonstrate that the execution plans for these statements are identical. SELECT o.orderid, o.orderdate, od.productid, od.unitprice, od.qty FROM Sales.Orders AS o CROSS APPLY (SELECT productid, unitprice, qty FROM Sales.OrderDetails AS so WHERE so.orderid = o.orderid ) AS od;

13 The OUTER APPLY Operator
20761B The OUTER APPLY Operator 12: Using Set Operators OUTER APPLY applies the right table source to each row in the left table source All rows from the left table source are returned—values from the right table source are returned where they exist, otherwise NULL is returned Most LEFT OUTER JOIN statements can be rewritten as OUTER APPLY statements USE TSQL; GO SELECT DISTINCT s.country AS supplier_country, c.country as customer_country FROM Production.Suppliers AS s LEFT OUTER JOIN Sales.Customers AS c ON c.country = s.country ORDER BY supplier_country; OUTER APPLY ( SELECT country FROM Sales.Customers AS cu WHERE cu.country = s.country ) AS c If you are comfortable with query execution plans and feel that it won’t confuse the class, demonstrate that, although the result sets returned by the queries match, the execution plans for these statements are different. In this case, the OUTER APPLY statement is slightly more efficient. SELECT DISTINCT s.country AS supplier_country, c.country as customer_country FROM Production.Suppliers AS s OUTER APPLY (SELECT country FROM Sales.Customers AS cu WHERE cu.country = s.country ) AS c ORDER BY supplier_country;

14 CROSS APPLY and OUTER APPLY Features
20761B CROSS APPLY and OUTER APPLY Features 12: Using Set Operators CROSS APPLY and OUTER APPLY allow query expressions that could not appear in a JOIN to return as part of a single result set For example, table-valued functions (TVFs) SELECT S.supplierid, s.companyname, P.productid, P.productname, P.unitprice FROM Production.Suppliers AS S CROSS APPLY dbo.fn_TopProductsByShipper(S.supplierid) AS P;


Download ppt "20761B 12: Using Set Operators Module 12   Using Set Operators."

Similar presentations


Ads by Google