Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

MemberId Street City State 11 Newish Lane Orange Town New State 12 Long Lane Windy Village Golden State 13 Winding Road Big City Mega State 14 White Cliff Street Dover Golden State 15 NULL NULL NULL You can also see that the MemberId column contains no NULLs, so if you change the column counted to MemberId (by including MemberId inside parentheses), you get the value 13, because 13 records in the MemberId column contain values that are not NULL. You can include more than one aggregate function in the SELECT statement. Consider the following statement: SELECT COUNT(City), COUNT(LastName) FROM MemberDetails; Executing the preceding SQL returns the following results: COUNT(City) COUNT(MemberId) 12 13 Remember, the City column does contain one record with a NULL value; hence the value above is 12. The MemberId column contains no NULLs in any of the records, so its value is 13, or all the records in the results set. Trying to combine an aggregate function and a nonaggregate column is not allowed by the rules of SQL. For example, the following SQL results in an error: SELECT City, COUNT(MemberId) FROM MemberDetails; The reason for this rule is that City might return more than one row, whereas COUNT() only ever returns one row. In this situation, the GROUP BY clause comes in quite handy. For example, if you want to know how many members live in each state based on information from the MemberDetails table, run the following query: SELECT State, COUNT(LastName) FROM MemberDetails GROUP BY State; Grouping and Aggregating Data 193

MemberId Street City State<br />

11 Newish Lane Orange Town New State<br />

12 Long Lane Windy Village Golden State<br />

13 Winding Road Big City Mega State<br />

14 White Cliff Street Dover Golden State<br />

15 NULL NULL NULL<br />

You can also see that the MemberId column contains no NULLs, so if you change the column counted to<br />

MemberId (by including MemberId inside parentheses), you get the value 13, because 13 records in the<br />

MemberId column contain values that are not NULL.<br />

You can include more than one aggregate function in the SELECT statement. Consider the following<br />

statement:<br />

SELECT COUNT(City), COUNT(LastName)<br />

FROM MemberDetails;<br />

Executing the preceding <strong>SQL</strong> returns the following results:<br />

COUNT(City) COUNT(MemberId)<br />

12 13<br />

Remember, the City column does contain one record with a NULL value; hence the value above is 12. The<br />

MemberId column contains no NULLs in any of the records, so its value is 13, or all the records in the<br />

results set.<br />

Trying to combine an aggregate function and a nonaggregate column is not allowed by the rules of <strong>SQL</strong>.<br />

For example, the following <strong>SQL</strong> results in an error:<br />

SELECT City, COUNT(MemberId)<br />

FROM MemberDetails;<br />

The reason for this rule is that City might return more than one row, whereas COUNT() only ever returns<br />

one row.<br />

In this situation, the GROUP BY clause comes in quite handy. For example, if you want to know how<br />

many members live in each state based on information from the MemberDetails table, run the following<br />

query:<br />

SELECT State, COUNT(LastName)<br />

FROM MemberDetails<br />

GROUP BY State;<br />

Grouping and Aggregating Data<br />

193

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

Saved successfully!

Ooh no, something went wrong!