Beginning SQL
Beginning SQL Beginning SQL
Chapter 6 190 Begin by looking at how GROUP BY can answer the question, “Which states do members of the film club live in?” The answer doesn’t require a list of every member and the state they live in; you simply want a list of the specific different states. Use the GROUP BY clause to answer this question, even though strictly speaking SELECT DISTINCT would work just as well: SELECT State FROM MemberDetails GROUP BY State; The GROUP BY clause must go after any FROM or WHERE clauses in the SELECT statement. All the columns you want to be grouped must be listed in the GROUP BY column list. For example, the preceding code groups by the State column. If you want to include more than one column in the GROUP BY clause, then separate the columns with commas, in the same way that you would separate columns in a SELECT statement’s column list. The preceding SQL produces the results shown in the following table. One of the values in the table is NULL, so you end up with one group that is NULL: State NULL Golden State Mega State New State When dealing with a GROUP BY clause, the database system first creates a temporary results set based on the FROM and WHERE clauses. So, in the preceding example, the results set is created from the following SELECT statement: SELECT State FROM MemberDetails; The DBMS then uses these results and looks for groups of identical records based on the column or columns specified in the GROUP BY clause. In this case, State is the only column, so it’s just a matter of grouping all the identical states into one row. If your query includes a WHERE clause, like the one shown below, the grouping is based on the results returned by the WHERE clause: SELECT State FROM MemberDetails WHERE State IN (‘Mega State’, ‘Golden State’,’New State’) GROUP BY State;
- Page 370: The CEILING() Function The CEILING(
- 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 424: Chapter 6 This section covers queri
- 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
Chapter 6<br />
190<br />
Begin by looking at how GROUP BY can answer the question, “Which states do members of the film club<br />
live in?”<br />
The answer doesn’t require a list of every member and the state they live in; you simply want a list of<br />
the specific different states. Use the GROUP BY clause to answer this question, even though strictly<br />
speaking SELECT DISTINCT would work just as well:<br />
SELECT State<br />
FROM MemberDetails<br />
GROUP BY State;<br />
The GROUP BY clause must go after any FROM or WHERE clauses in the SELECT statement. All the columns<br />
you want to be grouped must be listed in the GROUP BY column list. For example, the preceding code<br />
groups by the State column. If you want to include more than one column in the GROUP BY clause, then<br />
separate the columns with commas, in the same way that you would separate columns in a SELECT<br />
statement’s column list.<br />
The preceding <strong>SQL</strong> produces the results shown in the following table. One of the values in the table is<br />
NULL, so you end up with one group that is NULL:<br />
State<br />
NULL<br />
Golden State<br />
Mega State<br />
New State<br />
When dealing with a GROUP BY clause, the database system first creates a temporary results set based on<br />
the FROM and WHERE clauses. So, in the preceding example, the results set is created from the following<br />
SELECT statement:<br />
SELECT State<br />
FROM MemberDetails;<br />
The DBMS then uses these results and looks for groups of identical records based on the column or<br />
columns specified in the GROUP BY clause. In this case, State is the only column, so it’s just a matter of<br />
grouping all the identical states into one row. If your query includes a WHERE clause, like the one shown<br />
below, the grouping is based on the results returned by the WHERE clause:<br />
SELECT State<br />
FROM MemberDetails<br />
WHERE State IN (‘Mega State’, ‘Golden State’,’New State’)<br />
GROUP BY State;