Presentation is loading. Please wait.

Presentation is loading. Please wait.

20761A 11: Using Set Operators Module 11   Using Set Operators.

Similar presentations


Presentation on theme: "20761A 11: Using Set Operators Module 11   Using Set Operators."— Presentation transcript:

1 20761A 11: Using Set Operators Module 11 Using Set Operators

2 20761A Module Overview 11: Using Set Operators Using APPLY

3 Lesson 1: Writing Queries with the UNION Operator
11: Using Set Operators Demonstration: Using UNION and UNION ALL Question 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
11: 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
11: 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
11: 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 Demonstration: Using UNION and UNION ALL
11: Using Set Operators In this demonstration, you will see how to: Use UNION and UNION ALL Emphasize to students that this functionality is identical between Azure SQL Server and a locally-installed version. Preparation Steps All demonstrations in this module use a Microsoft Azure SQL Database running a copy of the AdventureWorksLT database. Before attempting to run these demos, ensure you have a copy of AdventureWorksLT running on an Azure instance. For detailed steps on creating a copy of the AdventureWorksLT database in Azure, see: D:\Creating an AdventureWorks Database on Azure.docx Start the MSL-TMG1, 20761A-MIA-DC, and 20761A-MIA-SQL virtual machines. Demonstration Steps Ensure that the MSL-TMG1, 20761A-MIA-DC, and 20761A-MIA-SQL virtual machines are running, and then log on to 20761A-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd. Start SQL Server Management Studio and connect to your Azure instance of the AdventureWorksLT database engine instance using SQL Server authentication. If the Microsoft SQL Server Management Studio dialog box appears, click OK. Open the Demo.ssmssln solution in the D:\Demofiles\Mod11\Demo folder. If the Solution Explorer pane is not visible, on the View menu, click Solution Explorer. Expand Queries, and double-click the 11 – Demonstration A.sql script file. In the Available Databases list, click AdventureWorksLT. Select the code under the comment Step 2, and then click Execute. Select the code under the comment Step 3, and then click Execute. Keep SQL Server Management Studio open for the next demonstration.

8 Lesson 2: Using EXCEPT and INTERSECT
11: 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 You should use INTERSECT. UNION lists all results that appear in either set, which would list all countries with at least one customer or at least one employee. UNION ALL would include duplicates found in the sets. EXCEPT lists results that appear in only the first set, which would list countries with at least one customer but no employee; or a list of countries with at least one employee but no customer, depending on the order in which the tables appear in your query. INTERSECT lists results that appear in both sets—which is the list of countries you want to find.

9 Using the INTERSECT Operator
11: 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;

10 Using the EXCEPT Operator
11: 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;

11 Demonstration: Using EXCEPT and INTERSECT
11: Using Set Operators In this demonstration, you will see how to: Use INTERSECT and EXCEPT Preparation Steps Complete the previous demonstration in this module. Alternatively, start the MSL-TMG1, 20761A-MIA-DC, and 20761A-MIA-SQL virtual machines, log on to 20761A-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd, and connect to your Azure copy of the AdventureWorksLT database. Demonstration Steps Ensure that you have completed the previous demonstration in this module. Alternatively, start the MSL-TMG1, 20761A-MIA-DC, and 20761A-MIA-SQL virtual machines, log on to 20761A-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd. If SQL Server Management Studio is not already open, start it and connect to your Azure instance of the AdventureWorksLT database engine instance using SQL Server authentication, and then open the Demo.ssmssln solution in the D:\Demofiles\Mod11\Demo folder. In Solution Explorer, open the 21 – Demonstration B.sql script file. In the Available Databases list, click AdventureWorksLT. Select the code under the comment Step 2, and then click Execute. Select the code under the comment Step 3, and then click Execute. Select the code under the comment Step 4, and then click Execute. Select the code under the comment Step 5, and then click Execute. Select the code under the comment Step 6, and then click Execute. Keep SQL Server Management Studio open for the next demonstration.

12 Demonstration: Using CROSS APPLY and OUTER APPLY
Lesson 3: Using APPLY 11: 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.

13 Using the APPLY Operator
11: 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>;

14 The CROSS APPLY Operator
11: 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;

15 The OUTER APPLY Operator
11: 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;

16 CROSS APPLY and OUTER APPLY Features
11: Using Set Operators CROSS APPLY and OUTER APPLY allow query expressions which could not appear in a JOIN to return as part of a single result set For example, table-valued functions (TVF) 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;

17 Demonstration: Using CROSS APPLY and OUTER APPLY
11: Using Set Operators In this demonstration, you will see how to: Use forms of the APPLY Operator Preparation Steps Complete the previous demonstration in this module. Alternatively, start the MSL-TMG1, 20761A-MIA-DC, and 20761A-MIA-SQL virtual machines, log on to 20761A-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd, and connect to your Azure copy of the AdventureWorksLT database. Demonstration Steps Ensure that you have completed the previous demonstration in this module. Alternatively, start the, A-MIA-DC, and 20761A-MIA-SQL virtual machines, log on to 20761A-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd. If SQL Server Management Studio is not already open, start it and connect to your Azure instance of the AdventureWorksLT database engine instance using SQL Server authentication, and then open the Demo.ssmssln solution in the D:\Demofiles\Mod11\Demo folder. In Solution Explorer, open the 31 – Demonstration C.sql script file. In the Available Databases list, click AdventureWorksLT. Select the code under the comment Step 2, and then click Execute. Select the code under the comment Step 3, and then click Execute. Select the code under the comment Step 4, and then click Execute. Select the code under the comment Step 5, and then click Execute. Select the code under the comment Test with CROSS APPLY, and then click Execute. Select the code under the comment Step 6, and then click Execute. Select the code under the comment Step 7, and then click Execute. Select the code under the comment Use OUTER APPLY to include customers with no orders, and then click Execute. Close SQL Server Management Studio, without saving any changes.

18 Lab: Using Set Operators
Exercise 3: Writing Queries That Use the EXCEPT and INTERSECT Operators Important: When comparing your results with the provided sample outputs, the column ordering and total number of affected rows should always match. However, remember that the order of the rows in the output of a query without an ORDER BY clause is not guaranteed. Therefore, the order of the rows in the sample outputs may be different to yours. Also, the answer outputs include abbreviated results. Exercise 1: Writing Queries That Use UNION Set Operators and UNION ALL Multi-Set Operators The marketing department needs some additional information regarding segmentation of products and customers. It would like to have a report, based on multiple queries, which is presented as one result. You will use the UNION operator to write different SELECT statements, and then merge them together into one result. Exercise 2: Writing Queries That Use the CROSS APPLY and OUTER APPLY Operators The sales department needs a more advanced analysis of buying behavior. Staff want to find out the top three products, based on sales revenue, for each customer. Use the APPLY operator to achieve this result. Exercise 3: Writing Queries That Use the EXCEPT and INTERSECT Operators The marketing department was satisfied with the results from exercise 1, but the staff now need to see specific rows from one result set that are not present in the other result set. You will have to write different queries using the EXCEPT and INTERSECT operators. Logon Information Virtual machine: 20761A-MIA-SQL User name: ADVENTUREWORKS\Student Password: Pa$$w0rd Estimated Time: 60 minutes

19 20761A Lab Scenario 11: Using Set Operators As a business analyst for Adventure Works, you will be writing reports using corporate databases stored in SQL Server You have been provided with a set of business requirements for data and you will write T-SQL queries to retrieve the specified data from the databases. Because of the complex business requirements, you will need to prepare combined results from multiple queries using set operators.

20 Module Review and Takeaways
11: Using Set Operators Review Question(s) Review Question(s) Question Which set operator would you use to combine sets if you knew there were no duplicates and wanted the best possible performance? Answer UNION ALL Which form of the APPLY operator will not return rows from the left table if the result of the right table expression was empty? CROSS APPLY Which form of the APPLY operator can be used to rewrite LEFT OUTER JOIN queries? OUTER APPLY

21 20761A Course Evaluation 11: Using Set Operators Your evaluation of this course will help Microsoft understand the quality of your learning experience. Please work with your training provider to access the course evaluation form. Microsoft will keep your answers to this survey private and confidential and will use your responses to improve your future learning experience. Your open and honest feedback is valuable and appreciated. Remind students to complete the course evaluation.


Download ppt "20761A 11: Using Set Operators Module 11   Using Set Operators."

Similar presentations


Ads by Google