Beginning SQL
Beginning SQL Beginning SQL
Chapter 6 Try It Out Selecting Data with MAX() and MIN() 1. The film club chairperson wants to know the dates of birth of the youngest and oldest members in each state. Use the following SQL: SELECT State, MAX(DateOfBirth), MIN(DateOfBirth) FROM MemberDetails WHERE State IS NOT NULL GROUP BY State; How It Works This query is a little easier than the other examples because all the data comes from the same table; there is no need for joins. The SELECT statement lists the columns and expressions that form the final results: SELECT State, MAX(DateOfBirth), MIN(DateOfBirth) The FROM part is easy: everything comes from the MemberDetails table. However, some values in the State and DateOfBirth columns are NULL, so you use an IS NOT NULL operator in the WHERE clause to exclude them: FROM MemberDetails WHERE State IS NOT NULL Finally, you want results grouped based on the State column, so you add a GROUP BY clause: GROUP BY State Although it’s true that all columns listed in the SELECT statement must also appear in the GROUP BY clause, it’s not true of aggregate functions like MAX() and MIN(), so they don’t appear in the GROUP BY clause. Executing the entire statement produces the following results set: State MAX(DateOfBirth) MIN(DateOfBirth) Golden State 1997-05-28 1956-08-07 Mega State 1994-05-28 1937-01-20 New State 1992-07-17 1952-10-05 So far, you’ve learned how to group and summarize results with aggregation functions. The next section explains how to use the HAVING clause to limit which groups are displayed and how to use the HAVING clause with the aggregation functions. Using the HAVING Clause with GROUP BY Statements 202 The HAVING clause enables you to specify conditions that filter which group results appear in the final results. In some ways, the HAVING clause resembles a WHERE clause for groups, in that you place it
- 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 426: MemberId Street City State 11 Newis
- Page 430: Executing the SQL returns seven dis
- Page 434: For example, the following code add
- Page 438: Running the query gives the answer
- Page 442: SELECT MAX(LastName), MIN(LastName)
- 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
- Page 478: effects of any WHERE clause. There
- Page 482: Now re-execute the query. You get a
- Page 486: the category list and a list of whi
- Page 490: Category FirstName LastName Horror
Chapter 6<br />
Try It Out Selecting Data with MAX() and MIN()<br />
1. The film club chairperson wants to know the dates of birth of the youngest and oldest members<br />
in each state. Use the following <strong>SQL</strong>:<br />
SELECT State, MAX(DateOfBirth), MIN(DateOfBirth)<br />
FROM MemberDetails<br />
WHERE State IS NOT NULL<br />
GROUP BY State;<br />
How It Works<br />
This query is a little easier than the other examples because all the data comes from the same table; there<br />
is no need for joins. The SELECT statement lists the columns and expressions that form the final results:<br />
SELECT State, MAX(DateOfBirth), MIN(DateOfBirth)<br />
The FROM part is easy: everything comes from the MemberDetails table. However, some values in the<br />
State and DateOfBirth columns are NULL, so you use an IS NOT NULL operator in the WHERE clause to<br />
exclude them:<br />
FROM MemberDetails<br />
WHERE State IS NOT NULL<br />
Finally, you want results grouped based on the State column, so you add a GROUP BY clause:<br />
GROUP BY State<br />
Although it’s true that all columns listed in the SELECT statement must also appear in the GROUP BY<br />
clause, it’s not true of aggregate functions like MAX() and MIN(), so they don’t appear in the GROUP BY<br />
clause. Executing the entire statement produces the following results set:<br />
State MAX(DateOfBirth) MIN(DateOfBirth)<br />
Golden State 1997-05-28 1956-08-07<br />
Mega State 1994-05-28 1937-01-20<br />
New State 1992-07-17 1952-10-05<br />
So far, you’ve learned how to group and summarize results with aggregation functions. The next section<br />
explains how to use the HAVING clause to limit which groups are displayed and how to use the HAVING<br />
clause with the aggregation functions.<br />
Using the HAVING Clause with<br />
GROUP BY Statements<br />
202<br />
The HAVING clause enables you to specify conditions that filter which group results appear in the final<br />
results. In some ways, the HAVING clause resembles a WHERE clause for groups, in that you place it