Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Chapter 3: The Foundation Statements of T-SQL 52 This will produce the following results: Name ProductNumber ReorderPoint ------------------------------ ---------------- ------------ Adjustable Race AR-5381 750 Bearing Ball BA-8327 750 ... ... Road-750 Black, 48 BK-R19B-48 75 Road-750 Black, 52 BK-R19B-52 75 (504 row(s) affected) As it happened, our query result set was sorted in ProductID order. Why? Because SQL Server decided that the best way to look at this data was by using an index that sorts the data by ProductID. That just happened to be what created the lowest-cost (in terms of CPU and I/O) query. Were we to run this exact query when the table has grown to a much larger size, SQL Server might have choose an entirely different execution plan, and therefore might sort the data differently. We could force this sort order by changing our query to this: SELECT Name, ProductNumber, ReorderPoint FROM Production.Product ORDER BY Name; Note that the WHERE clause isn’t required. It can either be there or not depending on what you’re trying to accomplish. Just remember that if you do have a WHERE clause, it goes before the ORDER BY clause. Unfortunately, that previous query doesn’t really give us anything different, so we don’t see what’s actually happening. Let’s change the query to sort the data differently — by the ProductNumber: SELECT Name, ProductNumber, ReorderPoint FROM Production.Product ORDER BY ProductNumber; Now our results are quite different. It’s the same data, but it’s been substantially rearranged: Name ProductNumber ReorderPoint -------------------------------------- ------------------------- ------------ Adjustable Race AR-5381 750 Bearing Ball BA-8327 750 LL Bottom Bracket BB-7421 375 ML Bottom Bracket BB-8107 375 ... ... Classic Vest, L VE-C304-L 3 Classic Vest, M VE-C304-M 3 Classic Vest, S VE-C304-S 3 Water Bottle - 30 oz. WB-H098 3 (504 row(s) affected)
Chapter 3: The Foundation Statements of T-SQL SQL Server still chose the least-cost method of giving us our desired results, but the particular set of tasks it actually needed to perform changed somewhat because the nature of the query changed. We can also do our sorting using numeric fields (note that we’re querying a new table): SELECT Name, SalesPersonID FROM Sales.Store WHERE Name BETWEEN ‘g’ AND ‘j’ AND SalesPersonID > 283 ORDER BY SalesPersonID, Name DESC; This one results in: Name SalesPersonID -------------------------------------------------- ------------- Inexpensive Parts Shop 286 Ideal Components 286 Helpful Sales and Repair Service 286 Helmets and Cycles 286 Global Sports Outlet 286 Gears and Parts Company 286 Irregulars Outlet 288 Hometown Riding Supplies 288 Good Bicycle Store 288 Global Bike Retailers 288 Instruments and Parts Company 289 Instant Cycle Store 290 Impervious Paint Company 290 Hiatus Bike Tours 290 Getaway Inn 290 (15 row(s) affected) Notice several things in this query: We’ve made use of many of the things that we’ve talked about up to this point. We’ve combined multiple WHERE clause conditions and also have an ORDER BY clause in place. In addition, we’ve added some new twists in our ORDER BY clause. First, we now have an ORDER BY clause that sorts based on more than one column. To do this, we simply comma delimited the columns we wanted to sort by. In this case, we’ve sorted first by SalesPersonID and then added a sub-sort based on Name. Second, the DESC keyword tells SQL Server that our ORDER BY should work in descending order for the Name sub-sort, rather than the default of ascending. (If you want to explicitly state that you want it to be ascending, use ASC.) While we usually sort the results based on one of the columns that we are returning, it’s worth noting that the ORDER BY clause can be based on any column in any table used in the query, regardless of whether it is included in the SELECT list. 53
- Page 40 and 41: Chapter 1: RDBMS Basics: What Makes
- Page 42 and 43: Chapter 1: RDBMS Basics: What Makes
- Page 44 and 45: Chapter 1: RDBMS Basics: What Makes
- Page 46 and 47: Chapter 1: RDBMS Basics: What Makes
- Page 48 and 49: Chapter 1: RDBMS Basics: What Makes
- Page 50 and 51: Chapter 1: RDBMS Basics: What Makes
- Page 52 and 53: Chapter 1: RDBMS Basics: What Makes
- Page 54 and 55: Chapter 1: RDBMS Basics: What Makes
- Page 56 and 57: Chapter 1: RDBMS Basics: What Makes
- Page 58 and 59: Chapter 1: RDBMS Basics: What Makes
- Page 60 and 61: Chapter 2: Tools of the Trade 22 My
- Page 62 and 63: Chapter 2: Tools of the Trade ❑ S
- Page 64 and 65: Chapter 2: Tools of the Trade 26 Ke
- Page 66 and 67: Chapter 2: Tools of the Trade 28 Th
- Page 68 and 69: Chapter 2: Tools of the Trade 30 lo
- Page 70 and 71: Chapter 2: Tools of the Trade 32 3.
- Page 72 and 73: Chapter 2: Tools of the Trade 34 Fi
- Page 74 and 75: Chapter 2: Tools of the Trade Figur
- Page 76 and 77: Chapter 2: Tools of the Trade Note
- Page 78 and 79: Chapter 2: Tools of the Trade bcp i
- Page 81 and 82: 3 The Foundation Statements of T-SQ
- Page 83 and 84: asic SELECT statement. Fire up the
- Page 85 and 86: Now let’s try taking a little bit
- Page 87 and 88: very few of your tables will have s
- Page 89: Operator Example Usage Effect ALL,
- Page 93 and 94: WHERE SalesOrderID IN (43660, 43670
- Page 95 and 96: AVG This one is for computing avera
- Page 97 and 98: efore, but remove the two AS keywor
- Page 99 and 100: You’ll get a result that is a bit
- Page 101 and 102: What if we want to place conditions
- Page 103 and 104: Let’s say, for example, that we w
- Page 105 and 106: Chapter 3: The Foundation Statement
- Page 107 and 108: City varchar(20) NOT NULL, State ch
- Page 109 and 110: for it in the INSERT statement. For
- Page 111 and 112: And sure enough, we get back the on
- Page 113 and 114: Chapter 3: The Foundation Statement
- Page 115 and 116: Chapter 3: The Foundation Statement
- Page 117: Chapter 3: The Foundation Statement
- Page 120 and 121: Chapter 4: JOINs 82 returned is in
- Page 122 and 123: Chapter 4: JOINs 84 Using an INNER
- Page 124 and 125: Chapter 4: JOINs 86 Uh, oh — this
- Page 126 and 127: Chapter 4: JOINs How It Works Unlik
- Page 128 and 129: Chapter 4: JOINs If we were to look
- Page 130 and 131: Chapter 4: JOINs 92 What I’m tryi
- Page 132 and 133: Chapter 4: JOINs 16 Mountain-500 Si
- Page 134 and 135: Chapter 4: JOINs 96 Let’s use thi
- Page 136 and 137: Chapter 4: JOINs 98 vendor. We’re
- Page 138 and 139: Chapter 4: JOINs 100 Somehow, we lo
Chapter 3: The Foundation Statements of T-<strong>SQL</strong><br />
52<br />
This will produce the following results:<br />
Name ProductNumber ReorderPoint<br />
------------------------------ ---------------- ------------<br />
Adjustable Race AR-5381 750<br />
Bearing Ball BA-8327 750<br />
...<br />
...<br />
Road-750 Black, 48 BK-R19B-48 75<br />
Road-750 Black, 52 BK-R19B-52 75<br />
(504 row(s) affected)<br />
As it happened, our query result set was sorted in ProductID order. Why? Because <strong>SQL</strong> <strong>Server</strong> decided<br />
that the best way to look at this data was by using an index that sorts the data by ProductID. That just<br />
happened to be what created the lowest-cost (in terms of CPU and I/O) query. Were we to run this exact<br />
query when the table has grown to a much larger size, <strong>SQL</strong> <strong>Server</strong> might have choose an entirely different<br />
execution plan, and therefore might sort the data differently. We could force this sort order by changing<br />
our query to this:<br />
SELECT Name, ProductNumber, ReorderPoint<br />
FROM Production.Product<br />
ORDER BY Name;<br />
Note that the WHERE clause isn’t required. It can either be there or not depending on what you’re trying<br />
to accomplish. Just remember that if you do have a WHERE clause, it goes before the ORDER BY clause.<br />
Unfortunately, that previous query doesn’t really give us anything different, so we don’t see what’s<br />
actually happening. Let’s change the query to sort the data differently — by the ProductNumber:<br />
SELECT Name, ProductNumber, ReorderPoint<br />
FROM Production.Product<br />
ORDER BY ProductNumber;<br />
Now our results are quite different. It’s the same data, but it’s been substantially rearranged:<br />
Name ProductNumber ReorderPoint<br />
-------------------------------------- ------------------------- ------------<br />
Adjustable Race AR-5381 750<br />
Bearing Ball BA-8327 750<br />
LL Bottom Bracket BB-7421 375<br />
ML Bottom Bracket BB-8107 375<br />
...<br />
...<br />
Classic Vest, L VE-C304-L 3<br />
Classic Vest, M VE-C304-M 3<br />
Classic Vest, S VE-C304-S 3<br />
Water Bottle - 30 oz. WB-H098 3<br />
(504 row(s) affected)