Contents
T-SQL Window Functions, Second Edition
For data analysis and beyond
Itzik Ben-Gan
Published with the authorization of Microsoft Corporation by:
Pearson Education, Inc.
Copyright 2020 by Itzik Ben-Gan.
All rights reserved. No commercial use of this Work, or any part thereof, is allowed without the written permission of the Author and the Publisher. The Work, or any material from the Work cannot be used to create training materials without the express written permission of the Author and Publisher. This publication is protected by copyright, and permission must be obtained from the author and publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. For information regarding permissions, request forms, and the appropriate contacts within the Pearson Education Global Rights & Permissions Department, please visit www.pearsoned.com/permissions/. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein.
ISBN-13: 978-0-13-586144-8
ISBN-10: 0-13-586144-6
Library of Congress Control Number: 2019949146
ScoutAutomatedPrintCode
Trademarks
Microsoft and the trademarks listed at on the Trademarks webpage are trademarks of the Microsoft group of companies. All other marks are property of their respective owners.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an as is basis. The author, the publisher, and Microsoft Corporation shall have neither liability nor responsibility to any person or entity with respect to any loss or damages arising from the information contained in this book.
Special Sales
For information about buying this title in bulk quantities, or for special sales opportunities (which may include electronic versions; custom cover designs; and content particular to your business, training goals, marketing focus, or branding interests), please contact our corporate sales department at or (800) 382-3419.
For government sales inquiries, please contact .
For questions about sales outside the U.S., please contact .
EDITOR-IN-CHIEF
Brett Bartow
EXECUTIVE EDITOR
Loretta Yates
DEVELOPMENT EDITOR
Rick Kughen
MANAGING EDITOR
Sandra Schroeder
SENIOR PROJECT EDITOR
Tracey Croom
COPY EDITOR
Rick Kughen
INDEXER
Erika Millen
PROOFREADER
Gill Editorial Services
TECHNICAL EDITOR
Adam Machanic
ASSISTANT SPONSORING EDITOR
Charvi Arora
COVER DESIGNER
Twist Creative, Seattle
COMPOSITOR
codeMantra
In loving memory of my parents, Mila and Gabi Ben-Gan.
About the Author
Itzik Ben-Gan is a mentor and co-founder of SolidQ and has been a data platform Microsoft MVP (Most Valuable Professional) since 1999. Itzik has delivered numerous training events around the world focused on T-SQL Querying, Query Tuning, and Programming. Itzik is the author of several books including T-SQL Fundamentals and T-SQL Querying. He has written articles for SentryOnes sqlperformance.com, ITProToday, and SolidQ. Itziks speaking activities include PASS Summit, SQLBits, and various events and user groups around the world. Itzik is the author of SolidQs Advanced T-SQL Querying, Programming, and Tuning and T-SQL Fundamentals courses, and he is a primary resource within the company for their T-SQLrelated activities.
Introduction
To me, Window functions are the most profound feature supported by both the SQL standard and Microsoft SQL Servers dialectT-SQL. They allow you to perform calculations against sets of rows in a flexible, clear, and efficient manner. The design of window functions is ingenious and overcomes a number of shortcomings of the traditional alternatives. The range of tasks that window functions help solve is so wide that it is well worth investing your time in learning those. Window functions have evolved quite substantially since their inception in SQL Server over the different versions, as well as in the SQL standard. This book covers both the SQL Serverspecific support for window functions as well as the SQL standards support, including elements that were not yet implemented in SQL Server.
Who Should Read This Book
This book is intended for SQL Server developers, database administrators (DBAs), data scientists, business intelligence (BI) specialists, and those who need to write queries and develop code using T-SQL. The book assumes that you already have at least six months to a year of experience writing and tuning T-SQL queries.
Organization of This Book
The book covers both the logical aspects of window functions as well as their optimization and practical usage aspects.
, SQL Windowing, explains the standard SQL windowing concepts. It describes the design of window functions, the types of window functions, and the elements involved in a window specification, such as partitioning, ordering, and framing.
, A Detailed Look at Window Functions, gets into the details and specifics of the different window functions. It describes window aggregate functions, window ranking functions, window offset functions, and window statistical (distribution) functions.
, Ordered Set Functions, describes the support that T-SQL and the SQL standard have for ordered set functions, including string concatenation, hypothetical set functions, inverse distribution functions, and others. For standard functions that are not yet available in T-SQL, the chapter provides working solutions.
, Row-Pattern Recognition in SQL, describes a profound standard concept for data analysis called row-pattern recognition (RPR) that you could think of as the next step in the evolution of window functions. This concept is not available yet in T-SQL, but as mentioned, this book does cover important standard analytical features even if not available yet in T-SQL.
, Optimization of Window Functions, covers in detail the optimization of window functions in SQL Server and Azure SQL Database. It provides indexing guidelines for optimal performance, explains how parallelism is handled and how to improve it, row-mode processing versus batch mode processing, and more.
, T-SQL Solutions Using Window Functions, covers practical uses of window functions to address common business tasks.