The syntax of the SQL Advanced (9) function function syntax for built-in SQL functions is:
SELECT function (column) from table
The type of function in SQL, the basic function type and kind have several kinds. The basic types of functions are:
- Aggregate function
- Scalar function
The operation of the aggregate function (Aggregate functions) Aggregate function targets a series of values and returns a single value.
Note: If a SELECT statement is used in many other expressions in the list of items in the SELECT statement, the select must use the GROUP by statement!
"Persons" table (used in most cases)
Name |
| Age
Adams, John. |
38 |
Bush, George. |
33 |
Carter, Thomas. |
28 |
Aggregate functions in SQL Server
function |
Description |
AVG (column) |
Returns the number of rows in a column |
Binary_checksum |
|
CHECKSUM |
|
Checksum_agg |
|
COUNT (column) |
Returns the number of rows in a column (not including null values) |
COUNT (*) |
Returns the number of rows selected |
COUNT (DISTINCT column) |
Returns the number of distinct results |
First (column) |
Returns the value of the first record in a specified field (SQLServer2000 not supported) |
Last (column) |
Returns the value of the last record in the specified field (SQLServer2000 not supported) |
MAX (column) |
Returns the highest value of a column |
MIN (column) |
Returns the lowest value of a column |
STDEV (column) |
|
STDEVP (column) |
|
SUM (column) |
Returns the sum of a column |
VAR (column) |
|
VARP (column) |
|
Scalar function
The operation of the Scalar function targets a single value and returns a single value based on the input value.
The SQL Avg function defines and uses the AVG function to return 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 () example 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 average of the "Orderprice" field.
We use the following SQL statement:
SELECT AVG (Orderprice) as Orderaverage from Orders
The result set looks like this:
Now, we want to find customers with Orderprice values above the Orderprice average.
Select Customer from Orderswhere orderprice> (select AVG (orderprice) from Orders)
The result set looks like this:
Customer |
Bush |
Carter |
Adams |
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".
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:
SQL COUNT (DISTINCT column_name) instance
Now, we want to calculate the number of different customers in the Orders table.
SELECT COUNT (DISTINCT Customer) as Numberofcustomers from Orders
The result set looks like this:
The first () function returns the value of record one 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.
SELECT First (Orderprice) as Firstorderprice from Orders
The result set looks like this:
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.
SELECT last (Orderprice) as Lastorderprice from Orders
The result set looks like this:
The max () function of 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.
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.
SELECT MIN (Orderprice) as Smallestorderprice from Orders
The result set looks like this:
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.
SELECT SUM (Orderprice) as OrderTotal from Orders
The result set looks like this:
SQL Advanced (9) function