Beginning SQL

Beginning SQL Beginning SQL

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

6 Grouping and Aggregating Data So far, results returned by SELECT queries have been a list of records, that is, specific data rather than summaries or overviews. This chapter examines two main things: sorting data into groups and returning the data for that group as a whole, and aggregation functions available in SQL. Aggregation is another way of saying a summary of data. For example, aggregating data might involve finding the average age of film club members or counting how many members live in a particular state. What you’ve learned so far allows you to answer questions pertaining to which film categories each member likes or what John Jones’s favorite film category is. However, by the end of this chapter, using a combination of groups and aggregation will enable you to answer questions such as how many members like thrillers. The difference between grouping and aggregation is that grouping finds out information about a particular record, whereas aggregation summarizes more than one record. Specifically, this chapter covers the GROUP BY clause, which groups results according to the parameters set forth in the clause. Additionally, this chapter examines the COUNT() function, which counts records; the SUM() function, which adds the value of records together; the AVG() function, which finds averages; and finally, the MAX() and MIN() functions, which find the lowest and highest values in a set of records, respectively. The chapter begins by looking at grouping results with the GROUP BY clause. Grouping Results This section examines the GROUP BY clause, which is used in conjunction with the SELECT statement. It allows you to group identical data into one subset rather than listing each record. The GROUP BY clause is at its most powerful when used with SQL’s summarizing and aggregating functions, which are covered in the next section. The GROUP BY clause is also very useful with subqueries, a concept examined in Chapter 7. The aim of this section is to get a handle on how GROUP BY works; the next section shows you how to use it more effectively.

6<br />

Grouping and Aggregating<br />

Data<br />

So far, results returned by SELECT queries have been a list of records, that is, specific data rather<br />

than summaries or overviews. This chapter examines two main things: sorting data into groups<br />

and returning the data for that group as a whole, and aggregation functions available in <strong>SQL</strong>.<br />

Aggregation is another way of saying a summary of data. For example, aggregating data might<br />

involve finding the average age of film club members or counting how many members live in a<br />

particular state. What you’ve learned so far allows you to answer questions pertaining to which<br />

film categories each member likes or what John Jones’s favorite film category is. However, by the<br />

end of this chapter, using a combination of groups and aggregation will enable you to answer<br />

questions such as how many members like thrillers. The difference between grouping and aggregation<br />

is that grouping finds out information about a particular record, whereas aggregation summarizes<br />

more than one record.<br />

Specifically, this chapter covers the GROUP BY clause, which groups results according to the<br />

parameters set forth in the clause. Additionally, this chapter examines the COUNT() function,<br />

which counts records; the SUM() function, which adds the value of records together; the AVG()<br />

function, which finds averages; and finally, the MAX() and MIN() functions, which find the lowest<br />

and highest values in a set of records, respectively. The chapter begins by looking at grouping<br />

results with the GROUP BY clause.<br />

Grouping Results<br />

This section examines the GROUP BY clause, which is used in conjunction with the SELECT statement.<br />

It allows you to group identical data into one subset rather than listing each record. The<br />

GROUP BY clause is at its most powerful when used with <strong>SQL</strong>’s summarizing and aggregating<br />

functions, which are covered in the next section. The GROUP BY clause is also very useful with<br />

subqueries, a concept examined in Chapter 7. The aim of this section is to get a handle on how<br />

GROUP BY works; the next section shows you how to use it more effectively.

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

Saved successfully!

Ooh no, something went wrong!