T-SQL Fundamentals, Third Edition
Itzik Ben-Gan
PUBLISHED BY
Microsoft Press
A division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright 2016 by Itzik Ben-Gan
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher.
Library of Congress Control Number: 2015955815
ISBN: 978-1-5093-0200-0
Printed and bound in the United States of America.
First Printing
Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Support at .
This book is provided as-is and expresses the authors views and opinions. The views, opinions and information expressed in this book, including URL and other Internet website references, may change without notice.
Some examples depicted herein are provided for illustration only and are fictitious. No real association or connection is intended or should be inferred.
Microsoft and the trademarks listed at http://www.microsoft.com on the Trademarks webpage are trademarks of the Microsoft group of companies. All other marks are property of their respective owners.
Acquisitions Editor: Devon Musgrave
Developmental Editor: Carol Dillingham
Project Editor: Carol Dillingham
Editorial Production: Christian Holdener; S4Carlisle Publishing Services
Technical Reviewer: Bob Beauchemin; Technical Review services provided by Content Master, a member of CM Group, Ltd.
Copyeditor: Roger Leblanc
Indexer: Maureen Johnson, MoJos Indexing Services
Cover: Twist Creative Seattle and Joel Panchot
To Dato,
To live in hearts we leave behind,
Is not to die.
THOMAS CAMPBELL
Contents at a glance
Contents
What do you think of this book? We want to hear from you!
Microsoft is interested in hearing your feedback so we can improve our books and learning resources for you. To participate in a brief survey, please visit:
http://aka.ms/tellpress
Introduction
This book walks you through your first steps in T-SQL (also known as Transact-SQL), which is the Microsoft SQL Server dialect of the ISO and ANSI standards for SQL. Youll learn the theory behind T-SQL querying and programming and how to develop T-SQL code to query and modify data, and youll get an overview of programmable objects.
Although this book is intended for beginners, its not merely a set of procedures for readers to follow. It goes beyond the syntactical elements of T-SQL and explains the logic behind the language and its elements.
Occasionally, the book covers subjects that might be considered advanced for readers who are new to T-SQL; therefore, you should consider those sections to be optional reading. The text will indicate when a section is considered more advanced and is provided as optional reading. If you feel comfortable with the material discussed in the book up to that point, you might want to tackle these more advanced subjects; otherwise, feel free to skip those sections and return to them after you gain more experience.
Many aspects of SQL are unique to the language and very different from other programming languages. This book helps you adopt the right state of mind and gain a true understanding of the language elements. You learn how to think in relational terms and follow good SQL programming practices.
The book is not version specific; it does, however, cover language elements that were introduced in recent versions of SQL Server, including SQL Server 2016. When I discuss language elements that were introduced recently, I specify the version in which they were added.
Besides being available as a box product, SQL Server is also available as a cloud-based service called Microsoft Azure SQL Database, or in short, just SQL Database. The code samples in this book were tested against both a box SQL Server product and Azure SQL Database. The books companion content (available at http://aka.ms/T-SQLFund3e/downloads) provides information about compatibility issues between the flavors.
To complement the learning experience, the book provides exercises you can use to practice what you learn. The book occasionally provides optional exercises that are more advanced. Those exercises are intended for readers who feel comfortable with the material and want to challenge themselves with more difficult problems. The optional exercises for advanced readers are labeled as such.
Who should read this book
This book is intended for T-SQL developers, database administrators (DBAs), business intelligence (BI) practitioners, data scientists, report writers, analysts, architects, and SQL Server power users who just started working with SQL Server and who need to write queries and develop code using Transact-SQL.
Assumptions
To get the most out of this book, you should have working experience with Microsoft Windows and with applications based on Windows. You should also be familiar with basic concepts of relational database management systems.
This book might not be for you if...
Not every book is aimed at every possible audience. This book covers fundamentals. Its mainly aimed at T-SQL practitioners with little or no experience. This book might not be for you if youre an advanced T-SQL practitioner with many years of experience. With that said, several readers of the previous editions of this book have mentioned thateven though they already had years of experiencethey still found the book useful for filling gaps in their knowledge.
Organization of this book
This book starts with a theoretical background to T-SQL querying and programming in .
Heres a list of the chapters along with a short description of the content in each chapter:
, provides the theoretical background for SQL, set theory, and predicate logic. It examines the relational model, describes SQL Servers architecture, and explains how to create tables and define data integrity.
, covers various aspects of querying a single table by using the SELECT statement.
, covers querying multiple tables by using joins, including cross joins, inner joins, and outer joins.
, covers queries within queries, otherwise known as subqueries.
, covers derived tables, Common Table Expressions (CTEs), views, inline table-valued functions, and the APPLY operator.
, covers the set operators UNION, INTERSECT, and EXCEPT.
, covers window functions, pivoting, unpivoting, and working with grouping sets.
, covers inserting, updating, deleting, and merging data.