Beginning SQL
Beginning SQL Beginning SQL
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
- Page 374: ased on the digits after the decima
- Page 378: The SUBSTRING() function works at t
- Page 382: LastName UPPER(LastName) LOWER(Last
- Page 386: The LENGTH() function takes just on
- Page 390: LastName LOWER(SUBSTRING(LastName,1
- Page 394: FirstName DIFFERENCE(FirstName, ‘
- Page 398: Converting Different Data Types Thi
- Page 402: CREATE Table MyTable(FirstColumn in
- Page 406: The COALESCE() Function The COALESC
- Page 410: Ideally, this first argument is the
- Page 414: LocationId Street City State 1 Main
- Page 418: 6 Grouping and Aggregating Data So
- Page 422: Running the query, complete with a
- Page 428: Chapter 6 194 This query gives the
- Page 432: Chapter 6 You’re almost there. Yo
- Page 436: Chapter 6 the database even though
- Page 440: Chapter 6 SELECT Category, AVG(DVDP
- Page 444: Chapter 6 Try It Out Selecting Data
- Page 448: Chapter 6 SELECT Category, COUNT(Fa
- Page 454: 7 Selecting Data from Different Tab
- Page 458: Selecting Data from Different Table
- Page 462: Note that MS Access insists that yo
- Page 466: City State Orange Town New State Wi
- Page 470: SELECT FM1.FilmName FROM Films AS F
- Page 474: Selecting Data from Different Table
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