20761B 12: Using Set Operators Module 12 Using Set Operators
Using DELETE, DROP and TRUNCATE 20761B Module Overview 12: Using Set Operators Using BETWEEN, NOT Using DELETE, DROP and TRUNCATE
Clause BETWEEN returns values that fall within a given range. Using BETWEEN 12: Using Set Operators Clause BETWEEN returns values that fall within a given range. SELECT * FROM HR.EMPLOYEES WHERE birthdate BETWEEN '1960.01.01' and '1970.01.01‘
SELECT * FROM HR.EMPLOYEES WHERE NOT TitleOfCourtesy ='Dr.' 20761B Using NOT 12: Using Set Operators In order to generate the report that retrieves data from the table with the values that we don’t want to have in output we use the operator NOT. SELECT * FROM HR.EMPLOYEES WHERE NOT TitleOfCourtesy ='Dr.'
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;
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;
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;
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; http://www.essentialsql.com/wp-content/uploads/2014/10/UnionInsersectExcept.jpg
Using UPDATE to Modify Data 20761B Using UPDATE to Modify Data 7: Using DML to Modify Data UPDATE changes all rows in a table or view Unless rows are filtered with a WHERE clause or constrained with a JOIN clause Column values are changed with the SET clause UPDATE Production.Products SET unitprice = (unitprice * 1.04) WHERE categoryid = 1 AND discontinued = 0 ; UPDATE Production.Products SET unitprice *= 1.04 -- Using compound -- assignment operators WHERE categoryid = 1 AND discontinued = 0;
Using DELETE, DROP and TRUNCATE 20761B Using DELETE, DROP and TRUNCATE 7: Using DML to Modify Data DELETE is used to delete data in the table DELETE from table Where column_name=value DROP is used to delete objects DROP table table_name TRUNCATE is used to remove all records from the object Truncate table table_name
Demonstration: Using UNION and UNION ALL 20761B Demonstration: Using UNION and UNION ALL 12: 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 Start the MSL-TMG1, 20761B-MIA-DC, and 20761B-MIA-SQL virtual machines. Demonstration Steps Ensure that the MSL-TMG1, 20761B-MIA-DC, and 20761B-MIA-SQL virtual machines are running, and then log on to 20761B-MIA-SQL as ADVENTUREWORKS\Student with the password Pa$$w0rd. Run D:\Demofiles\Mod12\Setup.cmd as an administrator. In the User Account Control dialog box, click Yes. At the command prompt, type y, and then press Enter. Wait for the script to finish, and then press any key. Start SQL Server Management Studio and connect to the MIA-SQL database engine instance using Windows authentication. Open the Demo.ssmssln solution in the D:\Demofiles\Mod12\Demo folder. In Solution Explorer, expand Queries, and double-click the 11 - Demonstration A.sql script file. Select the code under the comment Step 1, and then click Execute. 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.
Demonstration: Using EXCEPT and INTERSECT 20761B Demonstration: Using EXCEPT and INTERSECT 12: Using Set Operators In this demonstration, you will see how to: Use UPDATE, DELETE, DROP, TRUNCATE Preparation Steps Complete the previous demonstration in this module. Demonstration Steps In Solution Explorer, open the 21 - Demonstration B.sql script file. Select the code under the comment Step 1, and then click Execute. 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.