Beginning SQL
Beginning SQL Beginning SQL
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.
- Page 366: To demonstrate the sort of results
- 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 420: Chapter 6 190 Begin by looking at h
- 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
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.