sql--aggregate function (Aggregate functions): Avg,count,first,last,max,min,sum
avg () function
Definition and Usage
The AVG function returns the average of a numeric column. NULL values are not included in the calculation.
SQL AVG () syntax
SELECT AVG (column_name) from table_name
SQL AVG () instance
We have the following "Orders" table:
o_id |
OrderDate |
Orderprice |
Customer |
1 |
2008/12/29 |
1000 |
Bush |
2 |
2008/11/23 |
1600 |
Carter |
3 |
2008/10/05 |
700 |
Bush |
4 |
2008/09/28 |
300 |
Bush |
5 |
2008/08/06 |
2000 |
Adams |
6 |
2008/07/21 |
100 |
Carter |
Example 1
Now, we want to calculate the average of the "Orderprice" field.
We use the following SQL statement:
SELECT AVG (Orderprice) as Orderaverage from Orders
The result set looks like this:
Example 2
Now, we want to find customers with Orderprice values above the Orderprice average.
We use the following SQL statement:
Select Customer from Orderswhere orderprice> (select AVG (orderprice) from Orders)
The result set looks like this:
Customer |
Bush |
Carter |
Adams |
count () function
The count () function returns the number of rows that match the specified criteria.
SQL count () syntax SQL COUNT (column_name) syntax
The count (column_name) function returns the number of values for the specified column (NULL does not count in):
SELECT COUNT (column_name) from table_name
SQL COUNT (*) syntax
The count (*) function returns the number of records in the table:
SELECT COUNT (*) from table_name
SQL COUNT (DISTINCT column_name) syntax
The COUNT (DISTINCT column_name) function returns the number of different values for the specified column:
SELECT COUNT (DISTINCT column_name) from table_name
Note: COUNT (DISTINCT) applies to ORACLE and Microsoft SQL Server, but not to Microsoft Access.
SQL COUNT (column_name) instance
We have the following "Orders" table:
o_id |
OrderDate |
Orderprice |
Customer |
1 |
2008/12/29 |
1000 |
Bush |
2 |
2008/11/23 |
1600 |
Carter |
3 |
2008/10/05 |
700 |
Bush |
4 |
2008/09/28 |
300 |
Bush |
5 |
2008/08/06 |
2000 |
Adams |
6 |
2008/07/21 |
100 |
Carter |
Now, we want to calculate the number of orders for the customer "Carter".
We use the following SQL statement:
SELECT COUNT (Customer) as Customernilsen from Orderswhere customer= ' Carter '
The result of the above SQL statement is 2 because there are 2 orders for customer Carter:
SQL COUNT (*) instance
If we omit the WHERE clause, such as this:
SELECT COUNT (*) as numberoforders from Orders
The result set looks like this:
This is the total number of rows in the table.
SQL COUNT (DISTINCT column_name) instance
Now, we want to calculate the number of different customers in the Orders table.
We use the following SQL statement:
SELECT COUNT (DISTINCT Customer) as Numberofcustomers from Orders
The result set looks like this:
This is the number of different customers (Bush, Carter, and Adams) in the Orders table.
First () function
The first () function returns the value of the number one record in the specified field.
Tip: You can use the order BY statement to sort records.
SQL First () syntax
SELECT First (column_name) from table_name
SQL First () instance
We have the following "Orders" table:
o_id |
OrderDate |
Orderprice |
Customer |
1 |
2008/12/29 |
1000 |
Bush |
2 |
2008/11/23 |
1600 |
Carter |
3 |
2008/10/05 |
700 |
Bush |
4 |
2008/09/28 |
300 |
Bush |
5 |
2008/08/06 |
2000 |
Adams |
6 |
2008/07/21 |
100 |
Carter |
Now, we want to find the first value of the "Orderprice" column.
We use the following SQL statement:
SELECT First (Orderprice) as Firstorderprice from Orders
The result set looks like this:
Last () function
The last () function returns the value of the final record in the specified field.
Tip: You can use the order BY statement to sort records.
SQL last () syntax
SELECT last (column_name) from table_name
SQL last () instance
We have the following "Orders" table:
o_id |
OrderDate |
Orderprice |
Customer |
1 |
2008/12/29 |
1000 |
Bush |
2 |
2008/11/23 |
1600 |
Carter |
3 |
2008/10/05 |
700 |
Bush |
4 |
2008/09/28 |
300 |
Bush |
5 |
2008/08/06 |
2000 |
Adams |
6 |
2008/07/21 |
100 |
Carter |
Now, we want to find the last value of the "Orderprice" column.
We use the following SQL statement:
SELECT last (Orderprice) as Lastorderprice from Orders
The result set looks like this:
max () function
The Max function returns the maximum value in a column. NULL values are not included in the calculation.
SQL MAX () syntax
SELECT MAX (column_name) from table_name
Note: MIN and MAX can also be used for text columns to get the highest or lowest values in alphabetical order.
SQL MAX () instance
We have the following "Orders" table:
o_id |
OrderDate |
Orderprice |
Customer |
1 |
2008/12/29 |
1000 |
Bush |
2 |
2008/11/23 |
1600 |
Carter |
3 |
2008/10/05 |
700 |
Bush |
4 |
2008/09/28 |
300 |
Bush |
5 |
2008/08/06 |
2000 |
Adams |
6 |
2008/07/21 |
100 |
Carter |
Now, we want to find the maximum value for the "Orderprice" column.
We use the following SQL statement:
SELECT MAX (Orderprice) as Largestorderprice from Orders
The result set looks like this:
min () function
The Min function returns the minimum value in a column. NULL values are not included in the calculation.
SQL MIN () syntax
SELECT MIN (column_name) from table_name
Note: MIN and MAX can also be used for text columns to get the highest or lowest values in alphabetical order.
SQL MIN () instance
We have the following "Orders" table:
o_id |
OrderDate |
Orderprice |
Customer |
1 |
2008/12/29 |
1000 |
Bush |
2 |
2008/11/23 |
1600 |
Carter |
3 |
2008/10/05 |
700 |
Bush |
4 |
2008/09/28 |
300 |
Bush |
5 |
2008/08/06 |
2000 |
Adams |
6 |
2008/07/21 |
100 |
Carter |
Now, we want to find the minimum value for the "Orderprice" column.
We use the following SQL statement:
SELECT MIN (Orderprice) as Smallestorderprice from Orders
The result set looks like this:
sum () function
The SUM function returns the total number of numeric columns (total).
SQL SUM () syntax
SELECT SUM (column_name) from table_name
SQL SUM () instance
We have the following "Orders" table:
o_id |
OrderDate |
Orderprice |
Customer |
1 |
2008/12/29 |
1000 |
Bush |
2 |
2008/11/23 |
1600 |
Carter |
3 |
2008/10/05 |
700 |
Bush |
4 |
2008/09/28 |
300 |
Bush |
5 |
2008/08/06 |
2000 |
Adams |
6 |
2008/07/21 |
100 |
Carter |
Now, we want to find the total number of "Orderprice" fields.
We use the following SQL statement:
SELECT SUM (Orderprice) as OrderTotal from Orders
The result set looks like this:
sql--aggregate function (Aggregate functions): Avg,count,first,last,max,min,sum