0% found this document useful (0 votes)
8 views

Structured Query Language - Part III

Uploaded by

shazakhalid14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Structured Query Language - Part III

Uploaded by

shazakhalid14
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Computer Science Notes

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.

S.No. Function Purpose


1 MAX() Returns the MAXIMUM of the values under the specified
column/expression.
2 MIN() Returns the MINIMUM of the values under the specified
column/expression.
3 AVG() Returns the AVERAGE of the values under the specified
column/expression.
4 SUM() Returns the SUM of the values under the specified column/expression.
5 COUNT() Returns the COUNT of the number of values under the specified
column/expression.

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;

To find the highest cost of any shoe of type 'School'.


SELECT MAX(cost) FROM shoes WHERE type = 'School';

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.

SELECT MIN(cost) FROM shoes;

To find the lowest cost of any shoe of type 'School'.


SELECT MIN(cost) FROM shoes WHERE type = 'School';

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 total quantity present in the stock.


SELECT SUM(Qty) FROM Shoes;

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;

To count the total number of records in the table Shoes.


SELECT COUNT(*) FROM shoes;

Aggregate functions and NULL values:


None of the aggregate functions takes NULL into consideration. NULL is simply ignored by all the aggregate
functions.
For example, the statement:

SELECT COUNT(margin) FROM shoes;


produces the following output:

Whereas the query:


SELECT COUNT(*) FROM shoes;
produces the following output:
These outputs indicate that there are 13 records in the Shoes table; but there are only 10 values in the margin
column of Shoes table. This means there are 3 (13-10) NULLs in the margin column.
This feature of aggregate functions ensures that NULLs don't play any role in actual calculations. For example,
the following statement:
SELECT AVG(margin) FROM shoes;
produces the output:

Q. There is a column C1 in a table T1. The following two statements:


SELECT COUNT(*) FROM T1;
and

SELECT COUNT(C1) from T1;


are giving different outputs. What may be the possible reason?
The C1 column has NULL values. Aggregate functions generally ignore NULL values. But COUNT(*) function
counts rows irrespective of NULL values.
GROUP BY clause:
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.
E.g. the management of the shoe factory may want to know what is the total quantity of shoes of various
types. i.e., what is the total quantity of shoes of type School, Office, and Sports each.
So the statement to do this is:
SELECT type, SUM(qty) FROM shoes GROUP BY type;
and the corresponding output is:
Q. What is the difference between ORDER by and GROUP BY clauses?

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;

Q. What is the difference between WHERE clause and HAVING clause?

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.

You might also like