Structured Query Language - Part III
Structured Query Language - Part III
Aggregate Functions
Aggregate functions are also called Multiple Row functions. These functions work on a set of records as a
whole and return a single value for each column of the records on which the function is applied.
MIN(), MAX(), and COUNT() work on any type of values - Numeric, Date, or String. AVG(), and SUM() work on
only Numeric values (INT and DECIMAL).
MAX() :
MAX() function is used to find the highest value of any column or any expression based on a column. MAX()
takes one argument which can be any column name or a valid expression involving a column name
To find the highest cost of any type of shoe in the factory.
SELECT MAX(cost) FROM shoes;
MIN() :
MIN() function is used to find the lowest value of any column or an expression based on a column.
MIN() takes one argument which can be any column name or a valid expression involving a column name.
To find the lowest cost of any type of shoe in the factory.
AVG() :
AVG() function is used to find the average value of any column or an expression based on a column. AVG()
takes one argument which can be any column name or a valid expression involving a column name.
Here we have a limitation: the argument of AVG() function can be of numeric (int/float) type only. Averages of
String and Date type data are not defined.
To find the average margin from shoes table.
SELECT AVG(margin) FROM shoes;
To find the average quantity in stock for the shoes of type Sports.
SELECT AVG(qty) FROM shoes WHERE type = 'Sports';
SUM() :
SUM() function is used to find the total value of any column or an expression based on a column.
SUM() also takes one argument which can be any column name or a valid expression involving a column name.
Like AVG(), the argument of SUM() function can be of numeric (int/decimal) type only. Sums of String and Date
type data are not defined.
To find the the total value (Quanitity x Cost) of Shoes of type 'Office' present in the inventory
SELECT SUM(cost*qty) FROM shoes WHERE type = 'Office';
COUNT() :
COUNT() function is used to count the number of values in a column.
COUNT() takes one argument which can be any column name, an expression based on a column, or an asterisk
(*). When the argument is a column name or an expression based on a column, COUNT() returns the number
of non-NULL values in that column. If the argument is a *, then COUNT() counts the total number of rows
satisfying the condition, if any, in the table.
To count the records for which the margin is greater than 2.00
SELECT COUNT(margin) FROM shoes WHERE margin > 2;
ORDER BY clause is used to display the selected rows in ascending or in descending order of the specified
column/expression.
GROUP BY clause is used in a SELECT statement in conjunction with aggregate functions to group the result
based on distinct values in a column.
HAVING clause :
HAVING: HAVING clause is used in conjunction with GROUP BY clause in a SELECT statement to put condition
on groups.
Suppose, in the previous result, we are interested in viewing only those groups' output for which the total
quantity is more than 1500 (SUM(Qty) > 1500). As this condition is applicable to groups and not to individual
rows, we use HAVING clause as shown below:
SELECT type, SUM(qty) FROM shoes GROUP BY type HAVING SUM(qty) > 1500;
Another example:
SELECT type, SUM(qty) FROM shoes GROUP BY type HAVING AVG(margin) >2;
WHERE is used to put a condition on individual row of a table whereas HAVING is used to put condition on
individual group formed by GROUP BY clause in a SELECT statement.