Author: Scott Shaw and Kathi Kellenberger
Audience: Beginner to intermediate T-SQL developers
Reviewed by: Kay Ewbank
Updated version of a well-regarded earlier book on T-SQL 2008 that now takes account of SQL Server 2012.
The original book was written by Kathi Kellenberger, and this new edition has been updated by Scott Shaw. The authors make good use of examples, and have exercises that you can do to check you’ve understood the concepts. One small niggle is that the exercises say the answers are contained in the appendix, but this has been omitted from the new edition. According to the APress website the appendix is contained in the zipped file containing the source code online, but when I tried downloading it the ZIP file was reported as corrupted. Hopefully APress will sort this out.
The book starts with the absolute basics (installing SQL Express, using Management Studio, and the basic concepts of databases). The following chapters take you through T-SQL one step at a time, with chapters on Simple Select queries, using functions and expressions, querying multiple tables, grouping and summarizing, and manipulating data. By chapter 7 the authors have got as far as T-SQL programming logic - variables, If..Else, While, error handling, and temporary tables. There’s a new chapter in this edition on XML that gives a brief but reasonable introduction to working with XML in T-SQL.
Chapter 9 looks at moving logic to the database covering concepts such as table constraints, views, user defined functions, stored procedures and triggers. Given the amount of time many database developers spend working with these features, I think more space could usefully have been taken - a chapter for each wouldn’t have been over the top. However, what is there is well written. Data types get a chapter that essentially covers large-value data types, the more complex data and time types, hierarchyID, spatial data types (Geometry and Geography) and sparse columns. Most of these have been added to SQL Server 2008 and 2012, and the chapter gives ideas on how they are useful as well as how to use them.
The final ‘teaching’ chapter covers advanced queries. It starts with a look at the more advanced uses of common table expressions (CTEs) and how to use them to isolate parts of the query logic. The authors show how this gives you the means to do things such as write recursive queries (if you really want to tie your server in knots). The examples show how you can have multiple CTEs contained in a single statement, and how you can call a CTE multiple times in one statement. This is where T-SQL can get very powerful, and where you can indeed get into a terrible mess. Other examples in the chapter show how you can join a CTE to another CTE, and how to write a recursive query to call itself. There are good descriptions of the Output clause to retrieve result sets, Merge/Upsert to synchronize two tables, and Grouping sets for those occasions when you need to combine different grouping levels in one statement. Pivoted queries are also looked at in reasonable detail so you can create your own pivot tables using T-SQL.
The book closes with a chapter on other resources you can use having read the book - user groups, books, classes, conferences and so on.
This is a very good book. It’s not perfect, but it would be useful all the way from being a complete beginner who’s never written a SQL Select statement all the way through to pivot tables and recursive queries.