Here’s an example of the DISTINCT keyword, used with the following SongTitles table: 4 The Rolling Stones Let It Bleed Monkey Man Let’s say you want to see a list of artists in the table
Trang 1Here’s an example of the DISTINCT keyword, used with the following SongTitles table:
4 The Rolling Stones Let It Bleed Monkey Man
Let’s say you want to see a list of artists in the table This can be accom-plished with:
SELECT
DISTINCT
Artist
FROM SongTitles
ORDER BY Artist
The results are:
Artist
Paul McCartney
The Beatles
The Rolling Stones
The DISTINCT keyword is always placed immediately after the SELECT key-word The DISTINCT specifies that only unique values of the columnlist that
follow are to be brought back In this case, there are only three unique artists, so only three rows are returned
If you want to see unique combinations of both artists and albums, you’d issue: SELECT
DISTINCT
Artist,
Chapter 10 ■ Summarizing Data
96
Trang 2FROM SongTitles
ORDER BY Artist, Album
and the results would be:
The Rolling Stones Flowers
The Rolling Stones Let It Bleed
Notice that Abbey Road is only listed once, even though there are two songs from
that album in the table This is because the DISTINCT keyword causes only
unique values from the listed columns to be shown
Aggregate Functions
The functions we discussed in Chapter 4 were all scalar functions These
functions were all performed on a single number or value In contrast, aggregate
functions are meant to be used with groups of data The mostly widely used
aggregate functions areCOUNT,SUM,AVG,MIN, andMAX These provide counts,
sums, averages, and minimum and maximum values of groups of data
All of our aggregate function examples will be taken from the following two
tables with data about students, fees, and grades The Fees table contains:
Trang 3Here’s the Grades table:
Starting with theSUMfunction, let’s say that you want to see the total amount of gym fees paid by all students This can be accomplished with:
SELECT
SUM (Fee) AS 'Total Gym Fees'
FROM Fees
WHERE FeeType ¼ 'Gym'
The resulting data is:
Total Gym Fees
60
As can be seen, theSUM function sums up the total values for the Fee column, subject to selection in the WHERE clause Since the only expression in the
columnlist is an aggregate function, the query returns only one row of data,
giving the aggregate amount
D A T A B A S E D I F F E R E N C E S : M y S Q L
As noted in Chapter 4, MySQL sometimes requires that there be no space between a function name and the left parenthesis This is also true of most aggregate functions For example, the previous statement needs to be written as the following in MySQL:
SELECT
SUM(Fee) AS 'Total Gym Fees'
Chapter 10 ■ Summarizing Data
98
Trang 4FROM Fees
WHERE FeeType ¼ 'Gym'
TheAVG, MIN, and MAX functions are quite similar Here’s an example of the
AVGfunction In this case, we’re seeking to obtain the average grade of all quizzes
in the Grades table:
SELECT
AVG (Grade) AS 'Average Quiz Score'
FROM Grades
WHERE GradeType ¼ 'Quiz'
The result is:
Average Quiz Score
77
More than one aggregate function can be used in a single SELECT statement
Here’s aSELECTthat illustratesAVG,MIN, andMAX in a single statement:
SELECT
AVG (Grade) AS 'Average Quiz Score',
MIN (Grade) AS 'Minimum Quiz Score',
MAX (Grade) AS 'Maximum Quiz Score'
FROM Grades
WHERE GradeType ¼ 'Quiz'
The result is:
Average Quiz Score Minimum Quiz Score Maximum Quiz Score
The numbers you see have been computed separately The output shows the
average, minimum, and maximum of all quizzes in the Grades table
The COUNT Function
The COUNT function is slightly more complex, in that it can be used in three
different ways
Trang 5First, the COUNT function can be used to return a count of all selected rows, regardless of the values in any particular column As an example, the following returns a count of all rows with homework grades:
SELECT
COUNT (*) AS 'Count of Homework Rows'
FROM Grades
WHERE GradeType ¼ 'Homework'
The result is:
Count of Homework Rows
3
The asterisk in the parentheses means ‘‘all columns.’’ SQL retrieves all columns
in those rows that are selected, and then it returns a count of the number
of rows
In the second format of theCOUNTfunction, a specific column is specified rather than the asterisk Here’s an example:
SELECT
COUNT (Grade) AS 'Count of Homework Scores'
FROM Grades
WHERE GradeType ¼ 'Homework'
The result is:
Count of Homework Scores
2
Notice the subtle difference between the previous twoSELECTstatements In the first, you’re merely counting rows where the GradeType equals ‘‘Homework.’’ There are three of these rows In the second, you’re counting occurrences of the Grade column where the GradeType column has a value of ‘‘Homework.’’ In this case, one of the three rows has a value of NULL in the Grade column, so it isn’t counted As you remember, NULL means that the data doesn’t exist
Chapter 10 ■ Summarizing Data
100