20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

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 6<br />

194<br />

This query gives the results in the following table:<br />

State Count(State)<br />

NULL 1<br />

Golden State 3<br />

Mega State 4<br />

New State 5<br />

A GROUP BY clause essentially splits off results into groups, each being a subset of the full results. The<br />

COUNT() function in this case counts the results from each subset of the main results. Things might be a<br />

bit clearer if you look at the following table, which contains a list of States and LastNames returned by<br />

the FROM MemberDetails clauses:<br />

State LastName Notes<br />

NULL Hawthorn Null group<br />

Golden State Botts Golden State group<br />

Golden State Dales<br />

Golden State Night<br />

Mega State Doors Mega State group<br />

Mega State Johnson<br />

Mega State Simons<br />

Mega State Smith<br />

New State Gee<br />

New State Hills New State group<br />

New State Jackson<br />

New State Jones<br />

New State Jones<br />

The different groups in the preceding table are highlighted with different shading. If you count each<br />

group, you’ll find that they match the results from the COUNT(State) results table.<br />

Just as the SELECT statement has a DISTINCT option, so do aggregate functions. The DISTINCT option<br />

counts only the number of unique records; any duplicates are counted just once. Also, NULLs are not<br />

counted at all. For example, the following <strong>SQL</strong> counts how many distinct values the CategoryId column<br />

of the FavCategory table contains. Note that MS Access doesn’t support the DISTINCT keyword:<br />

SELECT COUNT(DISTINCT CategoryId)<br />

FROM FavCategory;

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

Saved successfully!

Ooh no, something went wrong!