Beginning SQL

Beginning SQL Beginning SQL

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

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

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

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

Saved successfully!

Ooh no, something went wrong!