Acknowledgments |
|
xxi | |
About the Author |
|
xxii | |
Introduction |
|
xxiv | |
|
Chapter 1 Background to T-SQL querying and programming |
|
|
1 | (26) |
|
|
1 | (12) |
|
|
2 | (1) |
|
|
3 | (1) |
|
|
4 | (1) |
|
|
5 | (6) |
|
Types of database workloads |
|
|
11 | (2) |
|
|
13 | (7) |
|
On-premises and cloud RDBMS flavors |
|
|
13 | (2) |
|
|
15 | (1) |
|
|
16 | (3) |
|
|
19 | (1) |
|
Creating tables and defining data integrity |
|
|
20 | (6) |
|
|
21 | (1) |
|
|
22 | (4) |
|
|
26 | (1) |
|
Chapter 2 Single-table queries |
|
|
27 | (90) |
|
Elements of the SELECT statement |
|
|
27 | (23) |
|
|
29 | (2) |
|
|
31 | (1) |
|
|
32 | (4) |
|
|
36 | (1) |
|
|
37 | (5) |
|
|
42 | (2) |
|
The TOP and OFFSET-FETCH filters |
|
|
44 | (5) |
|
A quick look at window functions |
|
|
49 | (1) |
|
|
50 | (3) |
|
|
53 | (3) |
|
|
56 | (6) |
|
The GREATEST and LEAST functions |
|
|
62 | (1) |
|
|
63 | (1) |
|
Working with character data |
|
|
64 | (19) |
|
|
64 | (2) |
|
|
66 | (2) |
|
|
68 | (13) |
|
|
81 | (2) |
|
Working with date and time data |
|
|
83 | (20) |
|
|
84 | (1) |
|
|
84 | (4) |
|
Working with date and time separately |
|
|
88 | (2) |
|
|
90 | (1) |
|
|
90 | (13) |
|
|
103 | (3) |
|
|
104 | (1) |
|
|
105 | (1) |
|
System stored procedures and functions |
|
|
105 | (1) |
|
|
106 | (1) |
|
|
107 | (4) |
|
|
107 | (1) |
|
|
107 | (1) |
|
|
108 | (1) |
|
|
108 | (1) |
|
|
109 | (1) |
|
|
109 | (1) |
|
|
109 | (1) |
|
|
110 | (1) |
|
|
110 | (1) |
|
|
111 | (1) |
|
|
111 | (6) |
|
|
111 | (1) |
|
|
112 | (1) |
|
|
112 | (1) |
|
|
112 | (1) |
|
|
113 | (1) |
|
|
114 | (1) |
|
|
115 | (1) |
|
|
115 | (1) |
|
|
116 | (1) |
|
|
116 | (1) |
|
|
117 | (32) |
|
|
117 | (4) |
|
|
118 | (1) |
|
|
118 | (1) |
|
|
119 | (1) |
|
Producing tables of numbers |
|
|
120 | (1) |
|
|
121 | (3) |
|
|
121 | (1) |
|
|
122 | (1) |
|
|
123 | (1) |
|
|
124 | (4) |
|
|
124 | (1) |
|
|
125 | (2) |
|
|
127 | (1) |
|
|
128 | (9) |
|
|
128 | (2) |
|
|
130 | (2) |
|
Filtering attributes from the nonpreserved side of an outer join |
|
|
132 | (1) |
|
Using outer joins in a multi-join query |
|
|
133 | (3) |
|
Using the COUNT aggregate with outer joins |
|
|
136 | (1) |
|
|
137 | (1) |
|
|
137 | (5) |
|
|
137 | (1) |
|
|
138 | (1) |
|
|
139 | (1) |
|
|
140 | (1) |
|
|
140 | (1) |
|
|
141 | (1) |
|
|
141 | (1) |
|
|
141 | (1) |
|
|
142 | (1) |
|
|
142 | (7) |
|
|
143 | (1) |
|
|
143 | (1) |
|
|
143 | (1) |
|
|
144 | (1) |
|
|
144 | (1) |
|
|
145 | (1) |
|
|
145 | (1) |
|
|
145 | (1) |
|
|
146 | (1) |
|
|
146 | (1) |
|
|
147 | (2) |
|
|
149 | (28) |
|
Self-contained subqueries |
|
|
149 | (6) |
|
Self-contained scalar subquery examples |
|
|
149 | (2) |
|
Self-contained multivalued subquery examples |
|
|
151 | (4) |
|
|
155 | (4) |
|
|
158 | (1) |
|
Returning previous or next values |
|
|
159 | (1) |
|
|
160 | (1) |
|
Dealing with misbehaving subqueries |
|
|
161 | (5) |
|
|
161 | (2) |
|
Substitution errors in subquery column names |
|
|
163 | (3) |
|
|
166 | (1) |
|
|
166 | (4) |
|
|
166 | (1) |
|
|
166 | (1) |
|
|
167 | (1) |
|
|
168 | (1) |
|
|
168 | (1) |
|
|
168 | (1) |
|
|
169 | (1) |
|
|
169 | (1) |
|
|
170 | (1) |
|
|
170 | (1) |
|
|
170 | (7) |
|
|
170 | (1) |
|
|
170 | (1) |
|
|
171 | (1) |
|
|
171 | (1) |
|
|
172 | (1) |
|
|
172 | (1) |
|
|
172 | (1) |
|
|
173 | (1) |
|
|
173 | (1) |
|
|
174 | (3) |
|
Chapter 5 Table expressions |
|
|
177 | (34) |
|
|
177 | (6) |
|
|
179 | (2) |
|
|
181 | (1) |
|
|
181 | (1) |
|
|
182 | (1) |
|
|
183 | (5) |
|
Assigning column aliases in CTEs |
|
|
184 | (1) |
|
|
185 | (1) |
|
|
185 | (1) |
|
Multiple references in CTEs |
|
|
186 | (1) |
|
|
186 | (2) |
|
|
188 | (2) |
|
Views and the ORDER BY clause |
|
|
190 | (2) |
|
|
192 | (4) |
|
Inline table-valued functions |
|
|
196 | (1) |
|
|
197 | (3) |
|
|
200 | (1) |
|
|
201 | (5) |
|
|
201 | (1) |
|
|
201 | (1) |
|
|
202 | (1) |
|
|
202 | (1) |
|
|
203 | (1) |
|
|
203 | (1) |
|
|
203 | (1) |
|
|
204 | (1) |
|
|
205 | (1) |
|
|
205 | (1) |
|
|
206 | (5) |
|
|
206 | (1) |
|
|
206 | (1) |
|
|
206 | (1) |
|
|
207 | (1) |
|
|
207 | (1) |
|
|
207 | (1) |
|
|
208 | (1) |
|
|
208 | (1) |
|
|
209 | (1) |
|
|
209 | (2) |
|
|
211 | (20) |
|
|
212 | (2) |
|
|
213 | (1) |
|
The Union (Distinct) operator |
|
|
213 | (1) |
|
|
214 | (3) |
|
The Intersect (Distinct) operator |
|
|
215 | (1) |
|
The Intersect ALL operator |
|
|
215 | (2) |
|
|
217 | (3) |
|
The Except (Distinct) operator |
|
|
218 | (1) |
|
|
219 | (1) |
|
|
220 | (1) |
|
Circumventing unsupported logical phases |
|
|
221 | (2) |
|
|
223 | (1) |
|
|
223 | (4) |
|
|
223 | (1) |
|
|
223 | (1) |
|
|
224 | (1) |
|
|
225 | (1) |
|
|
225 | (1) |
|
|
226 | (1) |
|
|
227 | (4) |
|
|
227 | (1) |
|
|
227 | (1) |
|
|
228 | (1) |
|
|
228 | (1) |
|
|
229 | (1) |
|
|
230 | (1) |
|
Chapter 7 T-SQL for data analysis |
|
|
231 | (62) |
|
|
231 | (15) |
|
|
234 | (3) |
|
|
237 | (5) |
|
Aggregate window functions |
|
|
242 | (2) |
|
|
244 | (2) |
|
|
246 | (5) |
|
Pivoting with a grouped query |
|
|
248 | (1) |
|
Pivoting with the P/VOf operator |
|
|
249 | (2) |
|
|
251 | (5) |
|
Unpivoting with the APPLY operator |
|
|
253 | (2) |
|
Unpivoting with the UNPIVO Toperator |
|
|
255 | (1) |
|
|
256 | (6) |
|
The GROUPING SETS subclause |
|
|
258 | (1) |
|
|
258 | (1) |
|
|
258 | (2) |
|
The GROUPING and GROUPINGJD functions |
|
|
260 | (2) |
|
|
262 | (18) |
|
|
263 | (3) |
|
|
266 | (2) |
|
Custom computation of start of containing bucket |
|
|
268 | (2) |
|
Applying bucket logic to sample data |
|
|
270 | (5) |
|
|
275 | (5) |
|
|
280 | (1) |
|
|
280 | (5) |
|
|
280 | (1) |
|
|
281 | (1) |
|
|
282 | (1) |
|
|
282 | (1) |
|
|
282 | (1) |
|
|
283 | (1) |
|
|
284 | (1) |
|
|
285 | (1) |
|
|
285 | (8) |
|
|
285 | (1) |
|
|
286 | (1) |
|
|
286 | (1) |
|
|
286 | (1) |
|
|
287 | (1) |
|
|
288 | (1) |
|
|
289 | (1) |
|
|
290 | (3) |
|
Chapter 8 Data modification |
|
|
293 | (50) |
|
|
293 | (14) |
|
The INSERT VALUES statement |
|
|
293 | (2) |
|
The INSERT SELECT statement |
|
|
295 | (1) |
|
The INSERT EXEC statement |
|
|
296 | (1) |
|
The SELECT INTO statement |
|
|
297 | (1) |
|
The BULK INSERT statement |
|
|
298 | (1) |
|
The identity property and the sequence object |
|
|
298 | (9) |
|
|
307 | (4) |
|
|
308 | (1) |
|
|
309 | (1) |
|
|
310 | (1) |
|
|
311 | (6) |
|
|
313 | (1) |
|
|
314 | (2) |
|
|
316 | (1) |
|
|
317 | (4) |
|
Modifying data through table expressions |
|
|
321 | (3) |
|
Modifications with TOP and OFFSET-FETCH |
|
|
324 | (2) |
|
|
326 | (7) |
|
|
326 | (2) |
|
|
328 | (1) |
|
|
329 | (1) |
|
|
330 | (1) |
|
|
331 | (2) |
|
|
333 | (1) |
|
|
333 | (4) |
|
|
333 | (1) |
|
|
334 | (1) |
|
|
334 | (1) |
|
|
334 | (2) |
|
|
336 | (1) |
|
|
336 | (1) |
|
|
337 | (6) |
|
|
337 | (1) |
|
|
338 | (1) |
|
|
339 | (1) |
|
|
340 | (1) |
|
|
340 | (1) |
|
|
341 | (2) |
|
Chapter 9 Temporal tables |
|
|
343 | (24) |
|
|
344 | (4) |
|
|
348 | (5) |
|
|
353 | (7) |
|
|
360 | (1) |
|
|
360 | (2) |
|
|
360 | (1) |
|
|
360 | (1) |
|
|
361 | (1) |
|
|
362 | (1) |
|
|
362 | (5) |
|
|
362 | (2) |
|
|
364 | (1) |
|
|
365 | (1) |
|
|
366 | (1) |
|
Chapter 10 Transactions and concurrency |
|
|
367 | (42) |
|
|
367 | (3) |
|
|
370 | (10) |
|
|
371 | (2) |
|
|
373 | (7) |
|
|
380 | (14) |
|
The READ UNCOMMITTED isolation level |
|
|
381 | (1) |
|
The READ COMMITTED isolation level |
|
|
382 | (2) |
|
The REPEATABLE READ isolation level |
|
|
384 | (2) |
|
The SERIALIZABLE isolation level |
|
|
386 | (1) |
|
Isolation levels based on row versioning |
|
|
387 | (7) |
|
Summary of isolation levels |
|
|
394 | (1) |
|
|
394 | (3) |
|
|
397 | (1) |
|
|
397 | (12) |
|
|
397 | (3) |
|
|
400 | (7) |
|
|
407 | (2) |
|
|
409 | (82) |
|
|
410 | (28) |
|
|
411 | (6) |
|
|
417 | (21) |
|
|
438 | (36) |
|
|
438 | (12) |
|
|
450 | (4) |
|
Using the SHORTESTPATH option |
|
|
454 | (17) |
|
SQL Graph querying features that are still missing |
|
|
471 | (3) |
|
Data modification considerations |
|
|
474 | (6) |
|
Deleting and updating data |
|
|
474 | (3) |
|
|
477 | (3) |
|
|
480 | (1) |
|
|
481 | (3) |
|
|
481 | (1) |
|
|
482 | (1) |
|
|
483 | (1) |
|
|
483 | (1) |
|
|
484 | (6) |
|
|
484 | (1) |
|
|
485 | (2) |
|
|
487 | (1) |
|
|
488 | (2) |
|
|
490 | (1) |
|
Chapter 12 Programmable objects |
|
|
491 | (36) |
|
|
491 | (3) |
|
|
494 | (3) |
|
A batch as a unit of parsing |
|
|
494 | (1) |
|
|
495 | (1) |
|
Statements that cannot be combined in the same batch |
|
|
495 | (1) |
|
A batch as a unit of resolution |
|
|
496 | (1) |
|
|
496 | (1) |
|
|
497 | (3) |
|
The IF... ELSE flow element |
|
|
497 | (1) |
|
|
498 | (2) |
|
|
500 | (5) |
|
|
505 | (5) |
|
|
505 | (2) |
|
|
507 | (1) |
|
|
508 | (1) |
|
|
509 | (1) |
|
|
510 | (3) |
|
|
511 | (1) |
|
The sp_executesql stored procedure |
|
|
511 | (1) |
|
Using PIVOT with Dynamic SQL |
|
|
512 | (1) |
|
|
513 | (8) |
|
|
514 | (1) |
|
|
515 | (2) |
|
|
517 | (4) |
|
|
521 | (4) |
|
|
525 | (2) |
Appendix: Getting started |
|
527 | (20) |
Index |
|
547 | |