sql--aggregate function (Aggregate functions): Avg,count,first,last,max,min,sum

Source: Internet
Author: User

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:

Orderaverage
950
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:

Customernilsen
2
SQL COUNT (*) instance

If we omit the WHERE clause, such as this:

SELECT COUNT (*) as numberoforders from Orders

The result set looks like this:

Numberoforders
6

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:

Numberofcustomers
3

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:

Firstorderprice
1000

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:

Lastorderprice
100

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:

Largestorderprice
2000

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:

Smallestorderprice
100

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:

OrderTotal
5700

sql--aggregate function (Aggregate functions): Avg,count,first,last,max,min,sum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.