Beginning SQL

Beginning SQL Beginning SQL

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

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;

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;

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

Saved successfully!

Ooh no, something went wrong!