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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

efore, but remove the two AS keywords from the query — you’ll see that you wind up with exactly the<br />

same results. It’s also worth noting that you can alias any column (and even, as we’ll see in the next<br />

chapter, table names), not just aggregates.<br />

Let’s re-run this last query, but this time we’ll not use the AS keyword in some places, and we’ll alias<br />

every column:<br />

SELECT SalesOrderID AS “Order Number”, MIN(OrderQty) MinOrderQty, MAX(OrderQty)<br />

MaxOrderQty<br />

FROM Sales.SalesOrderDetail<br />

WHERE SalesOrderID IN (43660, 43670, 43672)<br />

GROUP BY SalesOrderID;<br />

Despite the AS keyword being missing in some places, we’ve still changed the name output for every<br />

column:<br />

Order Number MinOrderQty MaxOrderQty<br />

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

43660 1 1<br />

43670 1 2<br />

43672 1 6<br />

(3 row(s) affected)<br />

I must admit that I usually don’t include the AS keyword in my aliasing, but I would also admit that<br />

it’s a bad habit on my part. I’ve been working with <strong>SQL</strong> <strong>Server</strong> since before the AS keyword was available<br />

and have, unfortunately, become set in my ways about it (I simply forget to use it). I would, however,<br />

strongly encourage you to go ahead and make use of this extra word. Why? Well, first because it<br />

reads somewhat more clearly, and second, because it’s the ANSI/ISO standard way of doing things.<br />

So then, why did I even tell you about it? Well, I got you started doing it the right way — with the AS<br />

keyword — but I want you to be aware of alternate ways of doing things, so that you aren’t confused<br />

when you see something that looks a little different.<br />

COUNT(Expression|*)<br />

The COUNT(*) function is about counting the rows in a query. To begin with, let’s go with one of the<br />

most common varieties of queries:<br />

SELECT COUNT(*)<br />

FROM HumanResources.Employee<br />

WHERE HumanResources.Employee.BusinessEntityID = 5;<br />

The record set you get back looks a little different from what you’re used to from earlier queries:<br />

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

1<br />

(1 row(s) affected)<br />

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

59

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

Saved successfully!

Ooh no, something went wrong!