You are on page 1of 231

O F F I C I A L M I C R O S O F T L E A R N I N G P R O D U C T

10774A
Querying Microsoft® SQL Server® 2012
Companion Content
2 Querying Microsoft® SQL Server® 2012

Information in this document, including URL and other Internet Web site references, is subject to change
without notice. Unless otherwise noted, the example companies, organizations, products, domain names,
e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with
any real company, organization, product, domain name, e-mail address, logo, person, place or event is
intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the
user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in
or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical,
photocopying, recording, or otherwise), or for any purpose, without the express written permission of
Microsoft Corporation.

Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property
rights covering subject matter in this document. Except as expressly provided in any written license
agreement from Microsoft, the furnishing of this document does not give you any license to these
patents, trademarks, copyrights, or other intellectual property.

The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory, regarding
these manufacturers or the use of the products with any Microsoft technologies. The inclusion of a
manufacturer or product does not imply endorsement of Microsoft of the manufacturer or product. Links
may be provided to third party sites. Such sites are not under the control of Microsoft and Microsoft is not
responsible for the contents of any linked site or any link contained in a linked site, or any changes or
updates to such sites. Microsoft is not responsible for webcasting or any other form of transmission
received from any linked site. Microsoft is providing these links to you only as a convenience, and the
inclusion of any link does not imply endorsement of Microsoft of the site or the products contained
therein.
© 2012 Microsoft Corporation. All rights reserved.

Microsoft, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the
United States and/or other countries.
All other trademarks are property of their respective owners.

Product Number: 10774A

Released: 05/2012
Querying Microsoft® SQL Server® 2012 3
4 Querying Microsoft® SQL Server® 2012
Querying Microsoft® SQL Server® 2012 5
6 Querying Microsoft® SQL Server® 2012
Querying Microsoft® SQL Server® 2012 7

Module 1
Introduction to Microsoft SQL Server 2012
Contents:
Lesson 1: Introducing Microsoft SQL Server 2012 8

Lesson 2: Getting Started with SQL Server Management Studio 10

Module Reviews and Takeaways 13


8 Querying Microsoft® SQL Server® 2012

Lesson 1
Introducing Microsoft SQL Server 2012
Contents:
Additional Reading 9
Querying Microsoft® SQL Server® 2012 9

Additional Reading
SQL Server Editions
• SQL Server 2012 Editions guide
10 Querying Microsoft® SQL Server® 2012

Lesson 2
Getting Started with SQL Server Management Studio
Contents:
Detailed Demonstration Steps 11

Additional Reading 12
Querying Microsoft® SQL Server® 2012 11

Detailed Demonstration Steps


Demonstration: Introducing Microsoft SQL Server 2012
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click SQL
Server Management Studio. In the Connect to Server window, type Proseware in the Server
Name text box and click Connect.

2. If the Object Explorer pane is not visible, click View and click Object Explorer.

3. Expand the Databases folder to see the list of databases.

4. Expand the AdventureWorks2008R2 database.

5. Expand the Tables folder.

6. Expand the Sales.Customer table.


7. Expand the Columns folder.

8. Show the list of columns, and point out the data type information for the ModifiedDate column.

9. If the Solution Explorer pane is not visible, click View and click Solution Explorer. It will be empty,
initially.

10. Click the File menu, click New, click Project


11. In the New Project box, under Installed Templates, click SQL Server Management Studio
Projects.

12. In the middle pane, click SQL Server Scripts.


13. In the Name box, type Module 1 Demonstration.
14. In the Location box, type or browse to F:\10774A_Labs\10774A_01_PRJ.
15. Point out the solution name, then click OK.
16. In the Solution Explorer pane, right-click Queries, then click New Query.
17. Type the following T-SQL code:

USE AdventureWorks2008R2;
GO
SELECT CustomerID, AccountNumber
FROM Sales.Customer;

18. Select the code and click Execute on the toolbar.


19. Point out the results pane.
20. Click File, and then click Save All.
21. Click File, and then click Close Solution.
22. Click File, click Recent Projects and Solutions, and then click Module 01 Demonstration.ssmssln.
23. Point out the Solution Explorer pane.
12 Querying Microsoft® SQL Server® 2012

Additional Reading
Starting SSMS
• Books Online
Querying Microsoft® SQL Server® 2012 13

Module Reviews and Takeaways


Review questions
Question: Can a SQL Server database be stored across multiple instances?

Answer: No, a database is completely contained within a single instance.

Question: If no T-SQL code is selected in a script window, which lines will be run when you
click the Execute button?

Answer: All of the statements in the script will be executed.

Question: What does a SQL Server Management Studio solution contain?

Answer: Projects.
14 Querying Microsoft® SQL Server® 2012

Module 2
Getting Started with SQL Azure
Contents:
Lesson 1: Overview of SQL Azure 15

Module Reviews and Takeaways 18


Querying Microsoft® SQL Server® 2012 15

Lesson 1
Overview of SQL Azure
Contents:
Detailed Demonstration Steps 16

Additional Reading 17
16 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Connecting to SQL Azure with SQL Server Management
Studio
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type <4 part name of Azure
server> in the Server Name text box, set Authentication to SQL Server Authentication, enter valid
administrator credentials, and click Connect.

2. From the File menu, click Open, click Project/Solution, navigate to


F:\10774A_Labs\10774A_02_PRJ\10774A_2_PRJ.ssmssln and click Open.
3. From the View menu, click Solution Explorer.

4. To create and populate the sample database on the SQL Azure server, open the script file 00 –
Setup.sql from within Solution Explorer and follow the instruction that are embedded as inline
comments.
Querying Microsoft® SQL Server® 2012 17

Additional Reading
What is SQL Azure?
• SQL Azure Landing Page

Key Distinctions Between SQL Server Azure and On-Premises SQL Server
• Transact-SQL Support (SQL Azure Database)

• SQL Azure security administration


18 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: What authentication mechanism does SQL Azure use?

Answer: SQL Authentication only, using a user name and password

Question: How do you switch from one user database to another on a SQL Azure server?

Answer: If you are connected to the master database, you can switch to a user database the
database menu in SSMS, but not with the USE statement. If you are connected to a user
database, you must disconnect and reconnect to the other user database.

Question: What choices are there for creating a database on a SQL Azure server
Answer: Use the Azure Management Portal or connect to the master database in SSMS and
execute a script which creates the database.
Querying Microsoft® SQL Server® 2012 19

Module 3
Introduction to T-SQL Querying
Contents:
Lesson 1: Introducing T-SQL 20

Lesson 2: Understanding Sets 23

Lesson 3: Understanding Predicate Logic 25

Lesson 4: Understanding the Logical Order of Operations in SELECT statements 27

Module Reviews and Takeaways 29


20 Querying Microsoft® SQL Server® 2012

Lesson 1
Introducing T-SQL
Contents:
Detailed Demonstration Steps 21

Additional Reading 22
Querying Microsoft® SQL Server® 2012 21

Detailed Demonstration Steps


Demonstration: T-SQL Language Elements
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate to
F:\10774A_Labs\10774A_03_PRJ\10774A_03_PRJ.ssmssln, and click Open.

2. On the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from within
Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
22 Querying Microsoft® SQL Server® 2012

Additional Reading
Categories of T-SQL Statements
• DML, DDL, and DCL commands can be found in Books Online

T-SQL Language Elements: Predicates and Operators


• Information on other categories of operators, including bitwise, unary, and scope assignment, can be
found in Books Online

T-SQL Language Elements: Functions


• information on these functions, with sample code, can be found in Books Online
Querying Microsoft® SQL Server® 2012 23

Lesson 2
Understanding Sets
Contents:
Additional Reading 24
24 Querying Microsoft® SQL Server® 2012

Additional Reading
Set Theory and SQL Server
• More information on the set theory and its application to SQL Server queries can be found in Chapter
1 of Itzik Ben-Gan’s Inside Microsoft® SQL Server® 2008: T-SQL Querying (Microsoft Press, 2009) and
Chapter 2 of Itzik Ben-Gan’s Microsoft SQL Server 2008: T-SQL Fundamentals (Microsoft Press, 2008).
For more information on the use of "Set of all…" see Joe Celko's Thinking in Sets (Morgan Kaufman,
2008).
Querying Microsoft® SQL Server® 2012 25

Lesson 3
Understanding Predicate Logic
Contents:
Additional Reading 26
26 Querying Microsoft® SQL Server® 2012

Additional Reading
Set Theory and SQL Server
• More information on the set theory and its application to SQL Server queries can be found in Chapter
1 of Itzik Ben-Gan’s Inside Microsoft® SQL Server® 2008: T-SQL Querying (Microsoft Press, 2009) and
Chapter 2 of Itzik Ben-Gan’s Microsoft SQL Server 2008: T-SQL Fundamentals (Microsoft Press, 2008).
For more information on the use of "Set of all…" see Joe Celko's Thinking in Sets (Morgan Kaufman,
2008).
Querying Microsoft® SQL Server® 2012 27

Lesson 4
Understanding the Logical Order of Operations in SELECT
Statements
Contents:
Detailed Demonstration Steps 28
28 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Logical Query Processing
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to F:\10774A_Labs\10774A_03_PRJ\10774A_03_PRJ.ssmssln, and click Open.

2. From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from
within Solution Explorer.
3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 29

Module Reviews and Takeaways


Review questions
Question: Which category of T-SQL statements concerns querying and modifying data?

Answer: DML

Question: What are some examples of aggregate functions supported by T-SQL?

Answer: SUM, MIN, COUNT, MAX, AVG

Question: Which SELECT statement element will be processed before a WHERE clause?

Answer: FROM
30 Querying Microsoft® SQL Server® 2012

Module 4
Writing SELECT Queries
Contents:
Lesson 1: Writing Simple SELECT Statements 31

Lesson 2: Eliminating Duplicates with DISTINCT 34

Lesson 3: Using Column and Table Aliases 36

Lesson 4: Writing Simple CASE Expressions 38

Module Reviews and Takeaways 40


Querying Microsoft® SQL Server® 2012 31

Lesson 1
Writing Simple SELECT Statements
Contents:
Detailed Demonstration Steps 32

Additional Reading 33
32 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Writing Simple SELECT Statements
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to F:\10774A_Labs\10774A_04_PRJ\10774A_04_PRJ.ssmssln, and click Open.

2. On the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from
within Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 33

Additional Reading
Elements of the SELECT Statement
• SELECT elements
34 Querying Microsoft® SQL Server® 2012

Lesson 2
Eliminating Duplicates with DISTINCT
Contents:
Detailed Demonstration Steps 35
Querying Microsoft® SQL Server® 2012 35

Detailed Demonstration Steps


Demonstration: Eliminating Duplicates with DISTINCT
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to F:\10774A_Labs\10774A_04_PRJ\10774A_04_PRJ.ssmssln and click Open.

2. From the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
36 Querying Microsoft® SQL Server® 2012

Lesson 3
Using Column and Table Aliases
Contents:
Detailed Demonstration Steps 37
Querying Microsoft® SQL Server® 2012 37

Detailed Demonstration Steps


Demonstration: Using Column and Table Aliases
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to F:\10774A_Labs\10774A_04_PRJ\10774A_04_PRJ.ssmssln, and click Open.

2. From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from
within Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
38 Querying Microsoft® SQL Server® 2012

Lesson 4
Writing Simple CASE Expressions
Contents:
Detailed Demonstration Steps 39
Querying Microsoft® SQL Server® 2012 39

Detailed Demonstration Steps


Demonstration: Simple CASE Expressions
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to F:\10774A_Labs\10774A_04_PRJ\10774A_04_PRJ.ssmssln, and click Open.

2. From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from
within Solution Explorer.

3. Open the 41 – Demonstration D.sql script file.

4. Follow the instructions contained within the comments of the script file.
40 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: Why is the use of SELECT * not a recommended practice?

Answer: Looking for 2 answers: 1) * asks for all columns, which is typically too much; 2)
Query exposed to changes in underlying table structure.

Question: What will happen if you omit a comma between column names in a SELECT
clause?

Answer: An accidental alias is created.

Question: What kind of result does a simple CASE statement return?

Answer: Scalar (single-value).

Best Practices
1. Terminate all SQL statements with a semicolon. This will make your code more readable, avoid
certain parsing errors, and will protect your code against changes in future versions of SQL
Server.
2. Consider standardizing your code on the AS keyword for labeling column and table aliases. This
will make your code easier to read and avoids accidental aliases.
Querying Microsoft® SQL Server® 2012 41

Module 5
Querying Multiple Tables
Contents:
Lesson 1: Understanding Joins 42

Lesson 2: Querying with Inner Joins 44

Lesson 3: Querying with Outer Joins 46

Lesson 4: Querying with Cross Joins and Self-Joins 48

Module Reviews and Takeaways 50


42 Querying Microsoft® SQL Server® 2012

Lesson 1
Understanding Joins
Contents:
Detailed Demonstration Steps 43
Querying Microsoft® SQL Server® 2012 43

Detailed Demonstration Steps


Demonstration: Understanding Joins
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution. Navigate
to F:\10774A_Labs\10774A_05_PRJ\10774A_05_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
44 Querying Microsoft® SQL Server® 2012

Lesson 2
Querying with Inner Joins
Contents:
Detailed Demonstration Steps 45
Querying Microsoft® SQL Server® 2012 45

Detailed Demonstration Steps


Demonstration: Querying with Inner Joins
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution. Navigate
to F:\10774A_Labs\10774A_05_PRJ\10774A_05_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
46 Querying Microsoft® SQL Server® 2012

Lesson 3
Querying with Outer Joins
Contents:
Detailed Demonstration Steps 47
Querying Microsoft® SQL Server® 2012 47

Detailed Demonstration Steps


Demonstration: Querying with Outer Joins
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution. Navigate
to F:\10774A_Labs\10774A_05_PRJ\10774A_05_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
48 Querying Microsoft® SQL Server® 2012

Lesson 4
Querying with Cross Joins and Self-Joins
Contents:
Detailed Demonstration Steps 49
Querying Microsoft® SQL Server® 2012 49

Detailed Demonstration Steps


Demonstration: Querying with Cross Joins and Self-Joins
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution. Navigate
to F:\10774A_Labs\10774A_05_PRJ\10774A_05_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 41 – Demonstration D.sql script file.

4. Follow the instructions contained within the comments of the script file.
50 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: How does an inner join differ from an outer join?

Answer: An inner join filters out rows which do not satisfy the predicate in the ON clause. An
outer join includes all rows from both tables and includes NULLs for attributes where no
match is found.

Question: Which join types include a logical Cartesian product?

Answer: CROSS, INNER and OUTER.

Question: Can a table be joined to itself?


Answer: Yes, as a self join. An alias to at least one table is required in the FROM clause.

Best Practices
1. Table aliases should always be defined when joining tables.

2. Joins should be expressed using SQL-92 syntax, with JOIN and ON keywords.
Querying Microsoft® SQL Server® 2012 51

Module 6
Sorting and Filtering Data
Contents:
Lesson 1: Sorting Data 52

Lesson 2: Filtering Data with Predicates 56

Lesson 3: Filtering Data with TOP and OFFSET-FETCH 58

Lesson 4: Working with Unknown Values 61

Module Reviews and Takeaways 63


52 Querying Microsoft® SQL Server® 2012

Lesson 1
Sorting Data
Contents:
Question and Answers 53

Detailed Demonstration Steps 54

Additional Reading 55
Querying Microsoft® SQL Server® 2012 53

Question and Answers


Demonstration: Sorting Data
Question: Does the physical order of rows in a SQL Server table guarantee any sort order in
queries using the table?

Answer: No.
54 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Sorting Data
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to F:\10774A_Labs\10774A_06_PRJ\10774A_06_PRJ.ssmssln, and click Open.

2. Open the 11 – Demonstration A.sql script file.

3. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 55

Additional Reading
Using the ORDER BY Clause
• ORDER BY Clause (Transact-SQL)
56 Querying Microsoft® SQL Server® 2012

Lesson 2
Filtering Data with Predicates
Contents:
Detailed Demonstration Steps 57
Querying Microsoft® SQL Server® 2012 57

Detailed Demonstration Steps


Demonstration: Filtering Data with Predicates
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to F:\10774A_Labs\10774A_06_PRJ\10774A_06_PRJ.ssmssln, and click Open.

2. Open the 11 – Demonstration A.sql script file.

3. Follow the instructions contained within the comments of the script file.
58 Querying Microsoft® SQL Server® 2012

Lesson 3
Filtering Data with TOP and OFFSET-FETCH
Contents:
Detailed Demonstration Steps 59

Additional Reading 60
Querying Microsoft® SQL Server® 2012 59

Detailed Demonstration Steps


Demonstration: Filtering Data with TOP and OFFSET-FETCH
Detailed demonstration steps
1. In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to F:\10774A_Labs\10774A_06_PRJ\10774A_06_PRJ.ssmssln and click Open.

2. Open the 31 – Demonstration C.sql script file.

3. Follow the instructions contained within the comments of the script file.
60 Querying Microsoft® SQL Server® 2012

Additional Reading
Filtering in the SELECT Clause Using the TOP Option
• TOP (Transact-SQL)

Filtering in the ORDER BY Clause Using OFFSET-FETCH


• Using OFFSET and FETCH to limit the rows returned
Querying Microsoft® SQL Server® 2012 61

Lesson 4
Working with Unknown Values
Contents:
Detailed Demonstration Steps 62
62 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Working with NULL
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to F:\10774A_Labs\10774A_06_PRJ\10774A_06_PRJ.ssmssln, and click Open.

2. Open the 41 – Demonstration D.sql script file.

3. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 63

Module Reviews and Takeaways


Review questions
Question: Is the use of ordinal column positions in an ORDER BY clause recommended? Why
or why not?

Answer: No. The underlying table structure may change.

Question: Can column aliases defined in a SELECT clause be used in an ORDER BY clause?
Why or why not?

Answer: Yes. ORDER BY is processed after SELECT

Question: What is the relationship between the ORDER BY clause and an OFFSET-FETCH
clause?

Answer: OFFSET-FETCH is dependent on ORDER BY clause.


64 Querying Microsoft® SQL Server® 2012

Module 7
Working with SQL Server 2012 Data Types
Contents:
Lesson 1: Introducing SQL Server 2012 Data Types 65

Lesson 2: Working with Character Data 68

Lesson 3: Working with Date and Time Data 71

Module Reviews and Takeaways 73


Querying Microsoft® SQL Server® 2012 65

Lesson 1
Introducing SQL Server 2012 Data Types
Contents:
Detailed Demonstration Steps 66

Additional Reading 67
66 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: SQL Server Data Types
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_07_PRJ\10774A_07_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 67

Additional Reading
Numeric Data Types
• decimal and numeric (Transact SQL)

• Precision, Scale, and Length (Transact-SQL)

• Data Types (Transact-SQL)

• float and real (Transact-SQL)

Binary String Data Types


• binary and varbinary (Transact-SQL)

Other Data Types


• See course 10776A: Developing Microsoft® SQL Server® 2012 Databases for additional information
on the XML data type.

• See course 10776: Developing Microsoft® SQL Server® 2012 Databases for additional information
about the hierarchyid data type.

• Data Types (Transact-SQL)

Data Type Precedence


• Data Type Precedence (Transact-SQL)

When Are Data Types Converted?


• Data Type Conversion (Database Engine)
68 Querying Microsoft® SQL Server® 2012

Lesson 2
Working with Character Data
Contents:
Detailed Demonstration Steps 69

Additional Reading 70
Querying Microsoft® SQL Server® 2012 69

Detailed Demonstration Steps


Demonstration: Working with Character Data
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_07_PRJ\10774A_07_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
70 Querying Microsoft® SQL Server® 2012

Additional Reading
String Concatenation
• CONCAT (Transact-SQL)

Character String Functions


• String Functions (Transact-SQL)
Querying Microsoft® SQL Server® 2012 71

Lesson 3
Working with Date and Time Data
Contents:
Detailed Demonstration Steps 72
72 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Working with Date and Time Data
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_07_PRJ\10774A_07_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 73

Module Reviews and Takeaways


Review questions
Question: Is SQL Server able to implicitly convert an int data type to a varchar?

Answer: No, int has higher type precedence.

Question: What data type is suitable for storing flag information, such as TRUE or FALSE?

Answer: bit

Question: What logical operators are useful for retrieving ranges of date and time values?

Answer: >=, <


74 Querying Microsoft® SQL Server® 2012

Module 8
Using Built-In Functions
Contents:
Lesson 1: Writing Queries with Built-In Functions 75

Lesson 2: Using Conversion Functions 78

Lesson 3: Using Logical Functions 81

Lesson 4: Using Functions to Work with NULL 85


Querying Microsoft® SQL Server® 2012 75

Lesson 1
Writing Queries with Built-In Functions
Contents:
Detailed Demonstration Steps 76

Additional Reading 77
76 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Writing Queries Using Built-In Functions
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
Name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_08_PRJ\10774A_08_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11- Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 77

Additional Reading
Scalar Functions
• Built-in Functions (Transact-SQL)
78 Querying Microsoft® SQL Server® 2012

Lesson 2
Using Conversion Functions
Contents:
Detailed Demonstration Steps 79

Additional Reading 80
Querying Microsoft® SQL Server® 2012 79

Detailed Demonstration Steps


Demonstration: Using Conversion Functions
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_08_PRJ\10774A_08_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
80 Querying Microsoft® SQL Server® 2012

Additional Reading
Implicit and Explicit Data Type Conversions
• CAST and CONVERT (Transact-SQL)

• Data Type Conversion (Database Engine)

Converting with CAST


• CAST and CONVERT (Transact-SQL)

Converting with CONVERT


• CAST and CONVERT (Transact-SQL)

Converting Strings with PARSE


• PARSE (Transact-SQL)
Querying Microsoft® SQL Server® 2012 81

Lesson 3
Using Logical Functions
Contents:
Question and Answers 82

Detailed Demonstration Steps 83

Additional Reading 84
82 Querying Microsoft® SQL Server® 2012

Question and Answers


Writing Logical Tests with Functions
Question: How might you use ISNUMERIC when testing data quality?

Answer: Answers will vary, but may include logic to replace invalid data with a substitute.
Querying Microsoft® SQL Server® 2012 83

Detailed Demonstration Steps


Demonstration: Using Logical Functions
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_08_PRJ\10774A_08_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
84 Querying Microsoft® SQL Server® 2012

Additional Reading
Performing Conditional Tests with IIF
• IIF (Transact-SQL)

Selecting Items from a List with CHOOSE


• CHOOSE (Transact-SQL)
Querying Microsoft® SQL Server® 2012 85

Lesson 4
Using Functions to Work with NULL
Contents:
Detailed Demonstration Steps 86

Additional Reading 87
86 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Using Functions to Work with NULL
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_08_PRJ\10774A_08_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 41 – Demonstration D.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 87

Additional Reading
Converting NULL with ISNULL
• ISNULL (Transact-SQL)

Using COALESCE to Return Non-NULL Values


• COALESCE (Transact-SQL)

Using NULLIF to Return NULL If Values Match


• NULLIF (Transact-SQL)
88 Querying Microsoft® SQL Server® 2012

Module 9
Grouping and Aggregating Data
Contents:
Lesson 1: Using Aggregate Functions 89

Lesson 2: Using the GROUP BY Clause 92

Lesson 3: Filtering Groups with HAVING 95

Module Reviews and Takeaways 98


Querying Microsoft® SQL Server® 2012 89

Lesson 1
Using Aggregate Functions
Contents:
Detailed Demonstration Steps 90

Additional Reading 91
90 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Using Aggregate Functions
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_09_PRJ\10774A_09_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 91

Additional Reading
Working with Aggregate Functions
• Aggregate Functions (Transact-SQL)

Built-In Aggregate Functions


• Aggregate Functions (Transact-SQL)
92 Querying Microsoft® SQL Server® 2012

Lesson 2
Using the GROUP BY Clause
Contents:
Detailed Demonstration Steps 93

Additional Reading 94
Querying Microsoft® SQL Server® 2012 93

Detailed Demonstration Steps


Demonstration: Using GROUP BY
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_09_PRJ\10774A_09_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer..

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
94 Querying Microsoft® SQL Server® 2012

Additional Reading
Using the GROUP BY Clause
• GROUP BY (Transact SQL)

GROUP BY and the Logical Order of Operations


• Troubleshooting GROUP BY Errors
Querying Microsoft® SQL Server® 2012 95

Lesson 3
Filtering Groups with HAVING
Contents:
Detailed Demonstration Steps 96

Additional Reading 97
96 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Filtering Groups with HAVING
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_09_PRJ\10774A_09_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 97

Additional Reading
Filtering Grouped Data Using the HAVING clause
• HAVING (Transact-SQL)
98 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: What is the difference between the COUNT function and the COUNT_BIG
function?

Answer: COUNT returns an int; COUNT_BIG returns a big_int.

Question: Can a GROUP BY clause include more than one column?

Answer: Yes, separated by commas.

Question: Can a WHERE clause and a HAVING clause in a query filter on the same column?

Answer: Yes.
Querying Microsoft® SQL Server® 2012 99

Module 10
Using Subqueries
Contents:
Lesson 1: Writing Self-Contained Subqueries 100

Lesson 2: Writing Correlated Subqueries 103

Lesson 3: Using the EXISTS Predicate with Subqueries 107

Module Reviews and Takeaways 110


100 Querying Microsoft® SQL Server® 2012

Lesson 1
Writing Self-Contained Subqueries
Contents:
Detailed Demonstration Steps 101

Additional Reading 102


Querying Microsoft® SQL Server® 2012 101

Detailed Demonstration Steps


Demonstration: Writing Self-Contained Subqueries
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_10_PRJ\10774A_10_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
102 Querying Microsoft® SQL Server® 2012

Additional Reading
Working with Subqueries
• Subquery Fundamentals

Writing Scalar Subqueries


• PRIMARY KEY Constraints
Querying Microsoft® SQL Server® 2012 103

Lesson 2
Writing Correlated Subqueries
Contents:
Question and Answers 104

Detailed Demonstration Steps 105

Additional Reading 106


104 Querying Microsoft® SQL Server® 2012

Question and Answers


Working with Correlated Subqueries
Question: Why can't a correlated subquery be executed separately from the outer query?

Answer: The subquery depends on input from the outer query for its values.
Querying Microsoft® SQL Server® 2012 105

Detailed Demonstration Steps


Demonstration: Writing Correlated Subqueries
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_10_PRJ\10774A_10_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
106 Querying Microsoft® SQL Server® 2012

Additional Reading
Writing Correlated Subqueries
• Correlated Subqueries
Querying Microsoft® SQL Server® 2012 107

Lesson 3
Using the EXISTS Predicate with Subqueries
Contents:
Detailed Demonstration Steps 108

Additional Reading 109


108 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Writing Subqueries Using EXISTS
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_10_PRJ\10774A_10_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 109

Additional Reading
Writing Queries Using EXISTS with Subqueries
• Subqueries with EXISTS
110 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: Can a correlated subquery return a multi-valued set?

Answer: Yes.

Question: What type of subquery may be rewritten as a JOIN?

Answer: Correlated subqueries.

Question: What columns should appear in the SELECT list of a subquery following the EXISTS
predicate?

Answer: Only a * needs to be specified. No actual columns will be retrieved.


Querying Microsoft® SQL Server® 2012 111

Module 11
Using Table Expressions
Contents:
Lesson 1: Using Views 112

Lesson 2: Using Inline Table-Valued Functions 115

Lesson 3: Using Derived Tables 118

Lesson 4: Using Common Table Expressions 120

Module Reviews and Takeaways 123


112 Querying Microsoft® SQL Server® 2012

Lesson 1
Using Views
Contents:
Detailed Demonstration Steps 113

Additional Reading 114


Querying Microsoft® SQL Server® 2012 113

Detailed Demonstration Steps


Demonstration: Using Views
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution. Navigate
to F:\10774A_Labs\10774A_11_PRJ\10774A_11_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
114 Querying Microsoft® SQL Server® 2012

Additional Reading
Writing Queries That Return Results From Views
• For more information on database security, see Microsoft course 10775: Administering Microsoft®
SQL Server® 2012 Databases.
Querying Microsoft® SQL Server® 2012 115

Lesson 2
Using Inline Table-Valued Functions
Contents:
Detailed Demonstration Steps 116

Additional Reading 117


116 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Inline Table-Valued Functions
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_11_PRJ\10774A_11_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 117

Additional Reading
Writing Queries That Use Inline Table-Valued Functions
• CREATE FUNCTION (Transact-SQL)
118 Querying Microsoft® SQL Server® 2012

Lesson 3
Using Derived Tables
Contents:
Detailed Demonstration Steps 119
Querying Microsoft® SQL Server® 2012 119

Detailed Demonstration Steps


Demonstration: Using Derived Tables
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_11_PRJ\10774A_11_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
120 Querying Microsoft® SQL Server® 2012

Lesson 4
Using Common Table Expressions
Contents:
Detailed Demonstration Steps 121

Additional Reading 122


Querying Microsoft® SQL Server® 2012 121

Detailed Demonstration Steps


Demonstration: Using CTEs
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_11_PRJ\10774A_11_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 41 – Demonstration D.sql script file.

4. Follow the instructions contained within the comments of the script file.
122 Querying Microsoft® SQL Server® 2012

Additional Reading
Writing Queries with Common Table Expressions
• Recursive Queries Using Common Table Expression
Querying Microsoft® SQL Server® 2012 123

Module Reviews and Takeaways


Review questions
Question: When would you use a common table expression rather than a derived table for a query?

Answer: CTEs may be written once, referenced multiple times in a query

Question: Which table expressions allow variables to be passed in as parameters to the expression?

Answer: Table-valued functions


124 Querying Microsoft® SQL Server® 2012

Module 12
Using Set Operators
Contents:
Lesson 1: Writing Queries with the UNION Operator 125

Lesson 2: Using EXCEPT and INTERSECT 128

Lesson 3: Using APPLY 131

Module Reviews and Takeaways 134


Querying Microsoft® SQL Server® 2012 125

Lesson 1
Writing Queries with the UNION Operator
Contents:
Detailed Demonstration Steps 126

Additional Reading 127


126 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Using UNION and UNION ALL
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_12_PRJ\10774A_12_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 127

Additional Reading
Using the UNION Operator
• UNION (Transact-SQL)
128 Querying Microsoft® SQL Server® 2012

Lesson 2
Using EXCEPT and INTERSECT
Contents:
Detailed Demonstration Steps 129

Additional Reading 130


Querying Microsoft® SQL Server® 2012 129

Detailed Demonstration Steps


Demonstration: Using EXCEPT and INTERSECT
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_12_PRJ\10774A_12_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
130 Querying Microsoft® SQL Server® 2012

Additional Reading
Using the INTERSECT Operator
• EXCEPT and INTERSECT (Transact-SQL)
Querying Microsoft® SQL Server® 2012 131

Lesson 3
Using APPLY
Contents:
Detailed Demonstration Steps 132

Additional Reading 133


132 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Using CROSS APPLY and OUTER APPLY
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_12_PRJ\10774A_12_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 133

Additional Reading
Using the APPLY Operator
• FROM (Transact-SQL)

• Using APPLY
134 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: Which set operator would you use to combine sets if you knew there were no duplicates
and wanted better performance?

Answer: UNION ALL

Question: Which APPLY form will not return rows from the left table if the result of the right table
expression was empty?

Answer: CROSS APPLY

Question: What is the difference between APPLY and JOIN?

Answer: APPLY can take the output of a table-valued function.


Querying Microsoft® SQL Server® 2012 135

Module 13
Using Window Ranking, Offset, and Aggregate Functions
Contents:
Lesson 1: Creating Windows with OVER 136

Lesson 2: Exploring Window Functions 139

Module Reviews and Takeaways 142


136 Querying Microsoft® SQL Server® 2012

Lesson 1
Creating Windows with OVER
Contents:
Detailed Demonstration Steps 137

Additional Reading 138


Querying Microsoft® SQL Server® 2012 137

Detailed Demonstration Steps


Demonstration: Using OVER and Partitioning
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_13_PRJ\10774A_13_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
138 Querying Microsoft® SQL Server® 2012

Additional Reading
Using OVER
• OVER Clause (Transact-SQL)
Querying Microsoft® SQL Server® 2012 139

Lesson 2
Exploring Window Functions
Contents:
Detailed Demonstration Steps 140

Additional Reading 141


140 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Exploring Windows Functions
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_13_PRJ\10774A_13_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 141

Additional Reading
Window Ranking Functions
• Ranking Functions (Transact-SQL)
142 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: What results will be returned by a ROW_NUMBER function if there is no ORDER BY clause
in the query?

Answer: An unordered set.

Question: Which ranking function would you use to return the values 1,1,3? Which would return
1,1,2?

Answer: RANK, DENSE_RANK

Question: Can a window frame extend beyond the boundaries of the window partition defined in the
same OVER() clause?

Answer: No.
Querying Microsoft® SQL Server® 2012 143

Module 14
Pivoting and Grouping Sets
Contents:
Lesson 1: Writing Queries with PIVOT and UNPIVOT 144

Lesson 2: Working with Grouping Sets 147

Module Reviews and Takeaways 150


144 Querying Microsoft® SQL Server® 2012

Lesson 1
Writing Queries with PIVOT and UNPIVOT
Contents:
Detailed Demonstration Steps 145

Additional Reading 146


Querying Microsoft® SQL Server® 2012 145

Detailed Demonstration Steps


Demonstration: Writing Queries with PIVOT and UNPIVOT
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_14_PRJ\10774A_14_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
146 Querying Microsoft® SQL Server® 2012

Additional Reading
What is Pivoting?
• Using PIVOT and UNPIVOT
Querying Microsoft® SQL Server® 2012 147

Lesson 2
Working with Grouping Sets
Contents:
Detailed Demonstration Steps 148

Additional Reading 149


148 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Using Grouping Sets
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_14_PRJ\10774A_14_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 149

Additional Reading
Writing Queries with Grouping Sets
• Using GROUP BY with ROLLUP, CUBE, and GROUPING SETS
150 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: Once a dataset has been pivoted with aggregation, can the original detail rows be restored
with an unpivot operation?

Answer: No, the original detail is lost during aggregation.

Question: What are the possible sources of NULLs returned by a query using grouping sets to create
aggregations?

Answer: NULLs might be present in the underlying source data, or may be placeholders for rows that
do not participate in the group member.

Question: Which subclause infers a hierarchy of columns to create meaningful grouping sets?

Answer: ROLLUP.
Querying Microsoft® SQL Server® 2012 151

Module 15
Querying SQL Server Metadata
Contents:
Lesson 1: Querying System Catalog Views and Functions 152

Lesson 2: Executing System Stored Procedures 155

Lesson 3: Querying Dynamic Management Objects 158

Module Reviews and Takeaways 161


152 Querying Microsoft® SQL Server® 2012

Lesson 1
Querying System Catalog Views and Functions
Contents:
Detailed Demonstration Steps 153

Additional Reading 154


Querying Microsoft® SQL Server® 2012 153

Detailed Demonstration Steps


Demonstration: Querying System Catalog Views and Functions
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_15_PRJ\10774A_15_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
154 Querying Microsoft® SQL Server® 2012

Additional Reading
System Catalog Views
• Querying the SQL Server System Catalog FAQ

Information Schema Views


• Information Schema Views (Transact-SQL)

System Metadata Functions


• SERVERPROPERTY (Transact-SQL)
Querying Microsoft® SQL Server® 2012 155

Lesson 2
Executing System Stored Procedures
Contents:
Detailed Demonstration Steps 156

Additional Reading 157


156 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Executing System Stored Procedures
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_15_PRJ\10774A_15_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 157

Additional Reading
Executing Stored Procedures
• Execute a Stored Procedure

Common System Stored Procedures


• Catalog Stored Procedures (Transact-SQL)
158 Querying Microsoft® SQL Server® 2012

Lesson 3
Querying Dynamic Management Objects
Contents:
Detailed Demonstration Steps 159

Additional Reading 160


Querying Microsoft® SQL Server® 2012 159

Detailed Demonstration Steps


Demonstration: Querying Dynamic Management Objects
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_15_PRJ\10774A_15_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
160 Querying Microsoft® SQL Server® 2012

Additional Reading
About Dynamic Management Objects
• Dynamic Management Views and Functions (Transact-SQL)
Querying Microsoft® SQL Server® 2012 161

Module Reviews and Takeaways


Review questions
Question: Why might you choose to query a system view rather than a system stored procedure that
returned the same metadata?

Answer: Views may be filtered, joined and further processed, unlike procedures.

Question: What issues might you face later if your application used SELECT * to query system catalog
views?

Answer: System views may be changed in future releases of SQL Server and columns may be added.
162 Querying Microsoft® SQL Server® 2012

Module 16
Using Table Expressions
Contents:
Lesson 1: Querying Data with Stored Procedures 163

Lesson 2: Passing Parameters to Stored Procedures 166

Lesson 3: Creating Simple Stored Procedures 169

Lesson 4: Working with Dynamic SQL 171

Module Reviews and Takeaways 174


Querying Microsoft® SQL Server® 2012 163

Lesson 1
Querying Data with Stored Procedures
Contents:
Detailed Demonstration Steps 164

Additional Reading 165


164 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Querying Data with Stored Procedures
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_16_PRJ\10774A_16_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 165

Additional Reading
Examining Stored Procedures
• Stored Procedures (Database Engine)

Executing Stored Procedures


• Execute a Stored Procedure
166 Querying Microsoft® SQL Server® 2012

Lesson 2
Passing Parameters to Stored Procedures
Contents:
Detailed Demonstration Steps 167

Additional Reading 168


Querying Microsoft® SQL Server® 2012 167

Detailed Demonstration Steps


Demonstration: Passing Parameters to Stored Procedures
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_16_PRJ\10774A_16_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
168 Querying Microsoft® SQL Server® 2012

Additional Reading
Passing Input Parameters to Stored Procedures
• Specify Parameters
Querying Microsoft® SQL Server® 2012 169

Lesson 3
Creating Simple Stored Procedures
Contents:
Detailed Demonstration Steps 170
170 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Creating Simple Stored Procedures
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_16_PRJ\10774A_16_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 171

Lesson 4
Working with Dynamic SQL
Contents:
Detailed Demonstration Steps 172

Additional Reading 173


172 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Working with Dynamic SQL
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_16_PRJ\10774A_16_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 41 – Demonstration D.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 173

Additional Reading
Constructing Dynamic SQL
• EXECUTE (Transact-SQL)

• SQL Injection

Writing Queries with Dynamic SQL


• Using sp_executesql
174 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: What benefits do stored procedures provide for data retrieval that views do not?

Answer: Answers may vary, but ability to accept parameters is what we're looking for.

Question: What form should parameter and value pairs take when passed to a stored procedure in
the EXECUTE statement?

Answer: @NAME = VALUE

Question: Which method for constructing dynamic SQL allows parameters to be passed at runtime?

Answer: Using sp_executesql


Querying Microsoft® SQL Server® 2012 175

Module 17
Programming with T-SQL
Contents:
Lesson 1: T-SQL Programming Elements 176

Lesson 2: Controlling Program Flow 179

Module Reviews and Takeaways 182


176 Querying Microsoft® SQL Server® 2012

Lesson 1
T-SQL Programming Elements
Contents:
Detailed Demonstration Steps 177

Additional Reading 178


Querying Microsoft® SQL Server® 2012 177

Detailed Demonstration Steps


Demonstration: T-SQL Programming Elements
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_17_PRJ\10774A_17_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
178 Querying Microsoft® SQL Server® 2012

Additional Reading
Introducing T-SQL Batches
• Batches

Introducing T-SQL Variables


• Variables (Transact-SQL)

Working with Synonyms


• Using Synonyms (Database Engine)
Querying Microsoft® SQL Server® 2012 179

Lesson 2
Controlling Program Flow
Contents:
Detailed Demonstration Steps 180

Additional Reading 181


180 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Controlling Program Flow
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_17_PRJ\10774A_17_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 181

Additional Reading
Understanding T-SQL Control-of-Flow Language
• Control-of-Flow Language (Transact-SQL)

Working with IF…ELSE


• IF...ELSE (Transact-SQL)
182 Querying Microsoft® SQL Server® 2012

Module Reviews and Takeaways


Review questions
Question: Can a variable be declared in one batch and referenced in multiple batches?

Answer: No, variables are local to the batch in which they are declared.

Question: Can a synonym be created that references an object that doesn’t exist?

Answer: Yes, resolution doesn't occur until the synonym is used.

Question: Will a WHILE loop exit when the predicate evaluates to NULL?

Answer: Yes.
Querying Microsoft® SQL Server® 2012 183

Module 18
Implementing Error Handling
Contents:
Lesson 1: Using TRY / CATCH Blocks 184

Lesson 2: Working with Error Information 187

Module Reviews and Takeaways 189


184 Querying Microsoft® SQL Server® 2012

Lesson 1
Using TRY / CATCH Blocks
Contents:
Detailed Demonstration Steps 185

Additional Reading 186


Querying Microsoft® SQL Server® 2012 185

Detailed Demonstration Steps


Demonstration: Using TRY / CATCH Blocks
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_18_PRJ\10774A_18_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
186 Querying Microsoft® SQL Server® 2012

Additional Reading
Structured Exception Handling
• Using TRY...CATCH in Transact-SQL
Querying Microsoft® SQL Server® 2012 187

Lesson 2
Working with Error Information
Contents:
Detailed Demonstration Steps 188
188 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Working with Error Information
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_18_PRJ\10774A_18_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 189

Module Reviews and Takeaways


Review questions
Question: What type of errors cannot by caught by structured exception handling?

Answer: Compile/syntax errors, as well as some delayed name resolution errors.

Question: Can TRY / CATCH blocks be nested?

Answer: Yes.

Question: How can THROW be used outside of a CATCH block?

Answer: With arguments that raise a user-defined error.


190 Querying Microsoft® SQL Server® 2012

Module 19
Implementing Transactions
Contents:
Lesson 1: Transactions and the Database Engine 191

Lesson 2: Controlling Transactions 194

Module Reviews and Takeaways 197


Querying Microsoft® SQL Server® 2012 191

Lesson 1
Transactions and the Database Engine
Contents:
Detailed Demonstration Steps 192

Additional Reading 193


192 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Transactions and the Database Engine
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_19_PRJ\10774A_19_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 193

Additional Reading
Defining Transactions
• Transaction Statements (Transact-SQL)

• For more information on locking, see Microsoft course 10776: Developing Microsoft® SQL Server®
2012 Databases.
194 Querying Microsoft® SQL Server® 2012

Lesson 2
Controlling Transactions
Contents:
Detailed Demonstration Steps 195

Additional Reading 196


Querying Microsoft® SQL Server® 2012 195

Detailed Demonstration Steps


Demonstration: Controlling Transactions
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_19_PRJ\10774A_19_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
196 Querying Microsoft® SQL Server® 2012

Additional Reading
BEGIN TRANSACTION
• BEGIN TRANSACTION (Transact-SQL)

• Nesting Transactions

ROLLBACK TRANSACTION
• Transaction Statements (Transact-SQL)

Using XACT_ABORT
• SET XACT_ABORT (Transact-SQL)
Querying Microsoft® SQL Server® 2012 197

Module Reviews and Takeaways


Review questions
Question: What happens to a nested transaction when the outer transaction is rolled back?

Answer: The inner transaction is also rolled back. Therefore nested transactions are not typically
useful in user code.

Question: When a runtime error occurs in a transaction and SET XACT_ABORT is ON, is the
transaction always automatically rolled back?

Answer: No, not if the error occurs within a TRY block.


198 Querying Microsoft® SQL Server® 2012

Module 20
Improving Query Performance
Contents:
Lesson 1: Factors in Query Performance 199

Lesson 2: Displaying Query Performance Data 202

Module Reviews and Takeaways 205


Querying Microsoft® SQL Server® 2012 199

Lesson 1
Factors in Query Performance
Contents:
Detailed Demonstration Steps 200

Additional Reading 201


200 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Factors in Query Performance
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_20_PRJ\10774A_20_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 201

Additional Reading
Writing Well-Performing Queries
• Query Performance

SQL Server Index Basics: Clustered Indexes


• Create Clustered Indexes

SQL Server Index Basics: Nonclustered Indexes


• Create Nonclustered Indexes

SQL Server Indexes: Performance Considerations


• Clustered and Nonclustered Indexes Described

Distribution Statistics
• Statistics Used by the Query Optimizer in Microsoft SQL Server 2008

Defining Cursors
• Cursors
202 Querying Microsoft® SQL Server® 2012

Lesson 2
Displaying Query Performance Data
Contents:
Detailed Demonstration Steps 203

Additional Reading 204


Querying Microsoft® SQL Server® 2012 203

Detailed Demonstration Steps


Demonstration: Displaying Query Performance Data
Detailed Demonstration Steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_20_PRJ\10774A_20_PRJ.ssmssln and click Open.

2. From the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
204 Querying Microsoft® SQL Server® 2012

Additional Reading
What Is an Execution Plan?
• Display and Save Execution Plans

Interpreting the Execution Plan


• Showplan Logical and Physical Operators Reference

Displaying Query Statistics


• Understanding Pages and Extents

• SET STATISTICS TIME (Transact-SQL)

• SET STATISTICS IO (Transact-SQL)


Querying Microsoft® SQL Server® 2012 205

Module Reviews and Takeaways


Review questions
Question: Why should you avoid the use of * in a SELECT clause?

Answer: Answers will vary, but should include performance and risk of changes to table design
breaking a client application

Question: How many clustered indexes are permitted per table?

Answer: One.

Question: Which type of execution plan can be displayed without running a query?

Answer: Estimated.
206 Querying Microsoft® SQL Server® 2012

Appendix A
Using DML to Modify Data
Contents:
Lesson 1: Adding Data to Tables 207

Lesson 2: Modifying and Removing Data 209

Lesson 3: Generating Numbers 211

Module Reviews and Takeaways 213


Querying Microsoft® SQL Server® 2012 207

Lesson 1
Adding Data to Tables
Contents:
Additional Reading 208
208 Querying Microsoft® SQL Server® 2012

Additional Reading
Using INSERT to Add Data
• INSERT (Transact-SQL)

• Table Value Constructor (Transact-SQL)


Querying Microsoft® SQL Server® 2012 209

Lesson 2
Modifying and Removing Data
Contents:
Additional Reading 210
210 Querying Microsoft® SQL Server® 2012

Additional Reading
Using UPDATE to Modify Data
• Books Online

Using MERGE to Modify Data


• MERGE (Transact-SQL)
Querying Microsoft® SQL Server® 2012 211

Lesson 3
Generating Numbers
Contents:
Additional Reading 212
212 Querying Microsoft® SQL Server® 2012

Additional Reading
Using IDENTITY
• SET IDENTITY_INSERT (Transact-SQL)

Using Sequences
• CREATE SEQUENCE (Transact-SQL)
Querying Microsoft® SQL Server® 2012 213

Module Reviews and Takeaways


Review questions
Question: What attributes of the source columns are transferred to a table created with a SELECT
INTO query?

Answer: Name, data type and nullability.

Question: The presence of what constraint prevents TRUNCATE TABLE from executing?

Answer: A foreign key reference to the table.


214 Querying Microsoft® SQL Server® 2012

Appendix B
Using DDL to Create and Modify Objects
Contents:
Lesson 1: Creating, Altering, and Dropping Tables 215

Lesson 2: Creating, Altering, and Dropping Views 217

Module Reviews and Takeaways 219


Querying Microsoft® SQL Server® 2012 215

Lesson 1
Creating, Altering, and Dropping Tables
Contents:
Additional Reading 216
216 Querying Microsoft® SQL Server® 2012

Additional Reading
Defining Tables
• CREATE TABLE (Transact-SQL)

Altering the Definition of a Table


• ALTER TABLE (Transact SQL)
Querying Microsoft® SQL Server® 2012 217

Lesson 2
Creating, Altering and Dropping Views
Contents:
Additional Reading 218
218 Querying Microsoft® SQL Server® 2012

Additional Reading
Defining Views
• CREATE VIEW (Transact-SQL)

Altering the Definition of a View


• ALTER VIEW (Transact-SQL)
Querying Microsoft® SQL Server® 2012 219

Module Reviews and Takeaways


Review questions
Question: What elements of a CREATE TABLE statement must be specified?

Answer: Table name, column names and data types.

Question: Can a table be dropped if a view depends on the table?

Answer: Yes, unless the view is defined with the SCHEMABINDING option.
220 Querying Microsoft® SQL Server® 2012

Appendix C
Using Table Expressions
Contents:
Lesson 2: Querying Full-Text Data 221

Lesson 3: Working with XML Data 223

Lesson 4: Converting Query Results to XML with FOR XML 225

Lesson 5: Querying XML Data 228


Querying Microsoft® SQL Server® 2012 221

Lesson 2
Querying Full-Text Data
Contents:
Detailed Demonstration Steps 222
222 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Querying Full-Text Data
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_AC_PRJ\10774A_AC_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 11 – Demonstration A.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 223

Lesson 3
Working with XML Data
Contents:
Additional Reading 224
224 Querying Microsoft® SQL Server® 2012

Additional Reading
Components of XML
• The complete XML specification
Querying Microsoft® SQL Server® 2012 225

Lesson 4
Converting Query Results to XML with FOR XML
Contents:
Detailed Demonstration Steps 226

Additional Reading 227


226 Querying Microsoft® SQL Server® 2012

Detailed Demonstration Steps


Demonstration: Converting Query Results to XML with FOR XML
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_AC_PRJ\10774A_AC_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 21 – Demonstration B.sql script file.

4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 227

Additional Reading
Using FOR XML in a SELECT Statement
• Use FOR XML and OPENXML to Publish and Process XML Data

• FOR XML (SQL Server)

Using FOR XML RAW


• RAW Mode with FOR XML

Using FOR XML AUTO


• Use AUTO Mode with FOR XML

Mixing Elements and Attributes with XPath


• Use PATH Mode with FOR XML

Handling NULL in XML Output


• Generate Elements for NULL Values with the XSINIL Parameter
228 Querying Microsoft® SQL Server® 2012

Lesson 5
Querying XML Data
Contents:
Detailed Demonstration Steps 229

Additional Reading 230


Querying Microsoft® SQL Server® 2012 229

Detailed Demonstration Steps


Demonstration: Querying XML Data
Detailed demonstration steps
1. On the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. On the File menu, click Open and click Project/Solution.
Navigate to F:\10774A_Labs\10774A_AC_PRJ\10774A_AC_PRJ.ssmssln and click Open.

2. On the View menu, click Solution Explorer.

3. Open the 31 – Demonstration C.sql script file.

4. Follow the instructions contained within the comments of the script file.
230 Querying Microsoft® SQL Server® 2012

Additional Reading
Working with the XML Data Type
• xml data type methods
Querying Microsoft® SQL Server® 2012 231

Send Us Your Feedback


You can search the Microsoft Knowledge Base for known issues at Microsoft Help and Support before
submitting feedback. Search using either the course number and revision, or the course title.

Note Not all training products will have a Knowledge Base article – if that is the case,
please ask your instructor whether or not there are existing error log entries.

Courseware Feedback
Send all courseware feedback to support@mscourseware.com. We truly appreciate your time and effort.
We review every e-mail received and forward the information on to the appropriate team. Unfortunately,
because of volume, we are unable to provide a response but we may use your feedback to improve your
future experience with Microsoft Learning products.

Reporting Errors
When providing feedback, include the training product name and number in the subject line of your e-
mail. When you provide comments or report bugs, please include the following:

7. Document or CD part number


8. Page number or location

9. Complete description of the error or suggested change

Please provide any details that are necessary to help us verify the issue.

Important All errors and suggestions are evaluated, but only those that are validated are
added to the product Knowledge Base article.

You might also like