17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Now let’s try taking a little bit more specific information. Let’s say all we want is a list of all our customers<br />

by last name:<br />

SELECT LastName FROM Person.Person;<br />

Your results should look something like:<br />

Achong<br />

Abel<br />

Abercrombie<br />

...<br />

He<br />

Zheng<br />

Hu<br />

Note that I’ve snipped rows out of the middle for brevity. You should have 19,972 rows. Since the last<br />

name of each customer is all that we want, that’s all that we’ve selected.<br />

Many <strong>SQL</strong> writers have the habit of cutting their queries short and always selecting every column by<br />

using a * in their selection criteria. This is another one of those habits to resist. While typing in a * saves<br />

you a few moments of typing out the column names that you want, it also means that more data has to<br />

be retrieved than is really necessary. In addition, <strong>SQL</strong> <strong>Server</strong> must figure out just how many columns<br />

“*” amounts to and what specifically they are. You would be surprised at just how much this can drag<br />

down your application’s performance and that of your network. In short, a good rule to live by is to<br />

select what you need — that is, exactly what you need. No more, no less.<br />

Let’s try another simple query. How about:<br />

SELECT Name FROM Production.Product;<br />

Again, assuming that you haven’t modified the data that came with the sample database, <strong>SQL</strong> <strong>Server</strong> should<br />

respond by returning a list of 504 different products that are available in the AdventureWorks database:<br />

Name<br />

----------------------------------------<br />

Adjustable Race<br />

Bearing Ball<br />

BB Ball Bearing<br />

...<br />

...<br />

Road-750 Black, 44<br />

Road-750 Black, 48<br />

Road-750 Black, 52<br />

Chapter 3: The Foundation Statements of T-<strong>SQL</strong><br />

The columns that you have chosen right after your SELECT clause are known as the SELECT list. In short,<br />

the SELECT list is made up of the columns that you have requested be output from your query.<br />

The columns that you have chosen right after your SELECT clause are known as the<br />

SELECT list.<br />

47

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!