Contents
Installing SQL Server Express Edition
Using SQL Server Management Studio
Launching SQL Server Management Studio
Installing the Sample Databases
Chapter 2: Exploring Database Concepts
Chapter 3: Writing Simple SELECT Queries
Mixing Literals and Column Names
Using WHERE Clauses with Alternate Operators
Using WHERE Clauses with Two Predicates
Solutions to Exercise 3-1: Using the SELECT Statement
Solutions to Exercise 3-2: Filtering Data
Solutions to Exercise 3-3: Using WHERE Clauses with Two Predicates
Solutions to Exercise 3-4: Working with NULL
Solutions to Exercise 3-5: Sorting Data
Chapter 4: Using Built-In Functions and Expressions
Concatenating Strings and NULL
Concatenating Other Data Types to Strings
Logical Functions and Expressions
Using Functions in the WHERE and ORDER BY Clauses
Solutions to Exercise 4-1: Expressions Using Operators
Solutions to Exercise 4-2: Using Mathematical Operators
Solution to Exercise 4-3: Using Functions
Solution to Exercise 4-4: Using Date and Time Functions
Solution to Exercise 4-5: Using Mathematical Functions
Solution to Exercise 4-6: Using Logical and System Functions
Solution to Exercise 4-7: Using Functions in the WHERE and ORDER BY Clauses
Avoiding an Incorrect Join Condition
Joining on a Different Column Name
Joining on More Than One Column
Using OUTER JOIN to Find Rows with No Match
Adding a Table to the Right Side of a LEFT JOIN
Adding a Table to the Main Table of a LEFT JOIN
Solutions to Exercise 5-1: Using INNER JOIN
Solutions to Exercise 5-2: Using OUTER JOIN
Chapter 6: Building on Subqueries, Common Table Expressions, and Unions
Using a Subquery in an IN List
Using a Subquery Containing NULL with NOT IN
Using CROSS APPLY and OUTER APPLY
Using Derived Tables and Common Table Expressions
Using Common Table Expressions
Using a Common Table Expression to Solve a Complicated Join Problem
Solutions to Exercise 6-1: Using Subqueries
Solutions to Exercise 6-2: Using Derived Tables and Common Table Expressions
Chapter 7: Grouping and Summarizing Data
DISTINCT Within an Aggregate Expression
Aggregate Queries with More Than One Table
Solutions to Exercise 7-1: Aggregate Functions
Solutions to Exercise 7-2: The GROUP BY Clause
Solutions to Exercise 7-3: The HAVING Clause
Solutions to Exercise 7-4: DISTINCT Keyword
Solutions to Exercise 7-5: Aggregate Queries with More Than One Table
Chapter 8: Discovering Windowing Functions
Dividing the Window into Partitions
Summarizing Results with Window Aggregates
Defining the Window with Framing
Understanding the Difference Between ROWS and RANGE
Using Window Analytic Functions
PERCENTILE_CONT and PERCENTILE_DISC
The Trouble with Window Aggregates
Solutions to Exercise 8-1: Ranking Functions
Solutions to Exercise 8-2: Summarizing Results with Window Aggregates
Solutions to Exercise 8-3: Understanding the Difference Between ROWS and RANGE
Solutions to Exercise 8-4: Using Window Analytic Functions
Chapter 9: Advanced WHERE Clauses
Restricting the Characters in Pattern Matches
Using WHERE Clauses with Three or More Predicates
Using Multiple Terms with CONTAINS
Solutions to Exercise 9-1: Using LIKE
Solution to Exercise 9-2: Using WHERE Clauses with Three or More Predicates
Solution to Exercise 9-3: Performing a Full-Text Search
Adding One Row with Literal Values
Inserting Multiple Rows with One Statement
Inserting Rows from Another Table
Creating and Populating a Table in One Statement
Inserting Rows into Tables with Default Column Values
Inserting Rows into Tables with Automatically Populating Columns
Deleting from a Table Using EXISTS
Updating Data with Expressions and Columns
Solution to Exercise 10-1: Inserting New Rows
Solution to Exercise 10-2: Deleting Rows
Solution to Exercise 10-3: Updating Existing Rows
Chapter 11: Writing Advanced Queries
Referencing a CTE Multiple Times
Isolating Aggregate Query Logic
Correlated Subqueries in the SELECT list
Using CROSS APPLY and OUTER APPLY
Chapter 12: Understanding T-SQL Programming Logic
Declaring and Initializing a Variable
Using Expressions and Functions with Variables
Using Variables in WHERE and HAVING Clauses
Temporary Tables and Table Variables
Using a Temp Table or Table Variable
Using a Temp Table or Table Variable Like an Array
Solutions to Exercise 12-1: Variables
Solutions to Exercise 12-2: The IF ... ELSE Construct
Solutions to Exercise 12-3: WHILE
Solutions to Exercise 12-4: Temporary Tables and Table Variables
Chapter 13: Managing Transactions
Writing an Explicit Transaction
Using TRY ... CATCH with Transactions
Using THROW Instead of RAISERROR
Solutions to Exercise 13-1: Writing an Explicit Transaction
Solutions to Exercise 13-2: Error Handling
Chapter 14: Implementing Logic in the Database
Adding Check Constraints to a Table
Adding a Primary Key to a Table
Creating Foreign Keys with Delete and Update Rules
Defining Automatically Populated Columns
Avoiding Common Problems with Views
Creating User-Defined Scalar Functions
Using Table-Valued User-Defined Functions
Using Default Values with Parameters
Saving the Results of a Stored Procedure in a Table
Using a Logic in Stored Procedures
Solutions to Exercise 14-1: Tables
Solution to Exercise 14-2: Views
Solution to Exercise 14-3: User-Defined Functions
Solution to Exercise 14-4: Stored Procedures
Retrieving Data as XML Using the FOR XML Clause
Chapter 16: Expanding on Data Type Concepts
Large-Value String Data Types (MAX)
Using DATE , TIME , and DATETIME 2
Using Stored Procedures to Manage Hierarchical Data
Viewing the Spatial Results Tab
Chapter 17: Running SQL Server in the Cloud
Procuring a Microsoft Azure Account
Windows Azure Virtual Machines