Lubor Kollar, Dejan Sarka, Steve Kass
Kalen Delaney–Series Editor
Itzik Ben-Gan
Inside Microsoft
®
SQL Server
®
2008
:
T-SQL Querying
Foreword by César Galindo-Legaria, PhD
Manager, Query Optimization Team, Microsoft SQL Server
PUBLISHED BY
Microsoft Press
A Division of Microsoft Corporation
One Microsoft Way
Redmond, Washington 98052-6399
Copyright © 2009 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: 2009920791
Printed and bound in the United States of America.
1 2 3 4 5 6 7 8 9 QWT 4 3 2 1 0 9
Distributed in Canada by H.B. Fenn and Company Ltd.
A CIP catalogue record for this book is available from the British Library.
Microsoft Press books are available through booksellers and distributors worldwide. For further infor mation about
international editions, contact your local Microsoft Corporation offi ce or contact Microsoft Press International directly at
fax (425) 936-7329. Visit our Web site at www.microsoft.com/mspress. Send comments to mspinput@microsoft.com.
Microsoft, Microsoft Press, Excel, MS, MSDN, PivotTable, SQL Server, Visual Basic, Visual C#, Visual Studio and Windows
are either registered trademarks or trademarks of the Microsoft group of companies. Other product and company names
mentioned herein may be the trademarks of their respective owners.
The example companies, organizations, products, domain names, e-mail addresses, logos, people, places, and events
depicted herein are fi ctitious. No association with any real company, organization, product, domain name, e-mail address,
logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided without any
express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will
be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
Acquisitions Editor: Ken Jones
Developmental Editor: Sally Stickney
Project Editor: Denise Bankaitis
Editorial Production: S4Carlisle Publishing Services
Technical Reviewers: Steve Kass and Umachandar Jayachandran; Technical Review services provided by Content
Master, a member of CM Group, Ltd.
Cover: Tom Draper Design
Body Part No. X15-45856
A02L626034.indd ii 2/28/2009 1:31:28 AM
To my parents, Mila & Gabi
—Itzik Ben-Gan
A03D626034.indd iii 2/17/2009 2:00:56 AM
A03D626034.indd iv 2/17/2009 2:00:56 AM
v
Table of Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xix
1 Logical Query Processing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1
Logical Query Processing Phases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Logical Query Processing Phases in Brief . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Sample Query Based on Customers/Orders Scenario . . . . . . . . . . . . . . . . . . . . . . 5
Logical Query Processing Phase Details. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Step 1: The FROM Phase. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Step 2: The WHERE Phase. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Step 3: The GROUP BY Phase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Step 4: The HAVING Phase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Step 5: The SELECT Phase . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Step 6: The Presentation ORDER BY Phase . . . . . . . . . . . . . . . . . . . . . . . . . 16
Further Aspects of Logical Query Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Table Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
OVER Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Set Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Conclusion. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2 Set Theory and Predicate Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
An Example of English-to-Mathematics Translation . . . . . . . . . . . . . . . . . . . . . . 35
Well-Defi nedness. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Equality, Identity, and Sameness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Mathematical Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Functions, Parameters, and Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Instructions and Algorithms. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
Microsoft is interested in hearing your feedback so we can continually improve our books and learning
resources for you. To participate in a brief online survey, please visit:
www.microsoft.com/learning/booksurvey/
What do you think of this book? We want to hear from you!
A04T626034.indd v 2/20/2009 6:16:01 PM
- 1
- 2
- 3
- 4
- 5
前往页