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.

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

60<br />

Let’s look at the differences. First, as with all columns that are returned as a result of a function call,<br />

there is no default column name. If you want there to be a column name, then you need to supply an<br />

alias. Next, you’ll notice that we haven’t really returned much of anything. So what does this record set<br />

represent? It is the number of rows that matched the WHERE condition in the query for the table(s) in the<br />

FROM clause.<br />

Keep this query in mind. This is a basic query that you can use to verify that the exact<br />

number of rows that you expect to be in a table and match your WHERE condition are<br />

indeed in there.<br />

Just for fun, try running the query without the WHERE clause:<br />

SELECT COUNT(*)<br />

FROM HumanResources.Employee;<br />

If you haven’t done any deletions or insertions into the Employee table, then you should get a record set<br />

that looks something like this:<br />

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

290<br />

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

What is that number? It’s the total number of rows in the Employee table. This is another one to keep in<br />

mind for future use.<br />

Now, we’re just getting started! If you look back at the header for this section (the COUNT section), you’ll<br />

see that there are two different ways of using COUNT. We’ve already discussed using COUNT with the *<br />

option. Now it’s time to look at it with an expression — usually a column name.<br />

First, try running the COUNT the old way, but against a new table:<br />

SELECT COUNT(*)<br />

FROM Person.Person;<br />

This is a slightly larger table, so you get a higher COUNT:<br />

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

19972<br />

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

Now alter your query to select the count for a specific column:<br />

SELECT COUNT(AdditionalContactInfo)<br />

FROM Person.Person;

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

Saved successfully!

Ooh no, something went wrong!