Académique Documents
Professionnel Documents
Culture Documents
Agenda
Additional SELECT Query Keywords
Listing Unique Values
Aggregate Function
COUNT
MAX and MIN
SUM
AVG
Grouping Data
Additional SELECT Query keywords
Listing Unique values
How many different vendors are currently represented in the PRODUCT table? A simple listing (SELECT)
is not very useful if the table contains several thousand rows and you have to sift through the vendor
codes manually. Fortunately, SQL’s DISTINCT clause produces a list of only those values that are
different from one another.
yields only the different vendor codes (V_CODE) in the PRODUCT table, as shown in the figure below.
Note that the second output row shows the null. The placement of nulls does not affect the list
contents. In Oracle, you could use ORDER BY V_CODE NULLS FIRST to place nulls at the top of the list.
Similarly if you want check what discounts are being offered you can just type:
Function Output
COUNT The number of rows containing “non null” values.
MIN The minimum attribute value encountered in a given column.
MAX The maximum attribute value encountered in a given column.
SUM The sum of all values for a given column.
AVG The arithmetic mean (average) of the specified column.
COUNT
The COUNT function is used to tally the number of non-null values of an attribute. COUNT can be used in
conjunction with the DISTINCT clause. For example, suppose that you want to find out how many
different vendors are in the PRODUCT table.
Output
The aggregate functions can be combined with the SQL commands discussed in previous labs. For
Example if a user wants to know the number of products that are priced below $10.
Output
MS Access does not support the use of COUNT with the DISTINCT clause. If you want to use such queries
in MS Access, you must create subqueries with DISTINCT and NOT NULL clauses.
Output
Output
The MAX and MIN aggregate functions can also be used with date columns. For example, to find out
which product has the oldest date, you would use MIN(P_INDATE). In the same manner, to find out the
most recent product, you would use MAX(P_INDATE).
Output
Output
SUM
The SUM function computes the total sum for any specified attribute, using any condition(s) you have
imposed.
For total values of all items in the PRODUCT table , Consider the following Statement:
Output
AVG
The AVG function format is similar to those of MIN and MAX and is subject to the same operating
restrictions. The first SQL command below shows how a simple average P_PRICE value can be generated
to yield the computed average price of 48.5509091.
Output
Grouping Data
In the previous examples, the aggregate functions summarized data across all rows in the given tables.
Sometimes, however, you do not want to treat the entire table as a single collection of data for
summarizing. Rows can be grouped into smaller collections quickly and easily using the GROUP BY clause
within the SELECT statement. The aggregate functions will then summarize the data within each smaller
collection.
SELECT columnlist
FROM tablelist
[WHERE conditionlist ]
[GROUP BY columnlist ]
[HAVING conditionlist ]
The GROUP BY clause is generally used when you have attribute columns combined with aggregate
functions in the SELECT statement.
Output
The GROUP BY clause is valid only when used in conjunction with one of the SQL aggregate functions,
such as COUNT, MIN, MAX, AVG, and SUM. For example, as shown in the first command set in Figure
7.26, if you try to group the output by using:
you generate a “not a GROUP BY expression” error. However, if you write the preceding SQL command
sequence in conjunction with an aggregate function, the GROUP BY clause works properly.