REQUEST COMPLIMENTARY SQLS*PLUS LICENCE
Average – SQL Server AVG function
AVG function (Average) in SQL Server (Transact-SQL) – AVG function returns the arithmetic mean for all found records. Arithmetic mean of a group of numbers is their sum divided by their number.
For example, we have a table with users that stores their ages. With AVG we can find their average age.
AVG function syntax in SQL Server (Transact-SQL):
SELECT AVG(aggregate_expr)
FROM tabs
[WHERE conds];
Or AVG function syntax when results are grouped in one or more columns:
SELECT expression1, expression2, … expression_n,
AVG(aggregate_expr)
FROM tabs
[WHERE conds]
GROUP BY expr1, expr2, … expr_n;
Parameters or arguments:
- expr1,2,… – expressions that were not included in the AVG function and must be entered into the GROUP BY operator at the end of the SQL sentence.
- aggregate_expr – expression or column for averaging.
- tabs – the tables from which we need to get the records. For the FROM operator, you need at least one table.
- WHERE conds are optional. Conditions that must be met for those records that we have selected.
Application
AVG function can be used in the following versions of SQL Server (Transact-SQL):
SQL Server vNext, SQL Server 2016, SQL Server 2015, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005
One Field Example
We will look at various examples of the AVG SQL Server function to understand how to properly use the AVG function in SQL Server (Transact-SQL).
For example, you can learn how the average number of all products where the quantity is greater than 0:
SELECT AVG(quantity_id) AS "Average Quantity_id"
FROM prods
WHERE quantity_id > 0;
In this example of AVG function, we have specified the alias as “Average Quantity” to the expression AVG(quantity). As a result, “Average Quantity” will be displayed as a field name when returning the resulting set.
id_1 | name_id | age_id | salary_id |
---|---|---|---|
1 | Peter | 23 | 100 |
2 | John | 24 | 200 |
3 | Jim | 25 | 300 |
Let’s find the average salary across the table:
SELECT AVG(salary_id) as avg FROM workers
The result of SQL query execution:
avg_id |
---|
200 |
Example of using DISTINCT
You can use a DISTINCT operator with AVG function. For example, the following SQL operator returns an average salary with unique values where the salary exceeds $30,000 per year.
SELECT AVG_id(DISTINCT salary_id) AS "Average Salary_id"
FROM employees
WHERE salary_id > 30000;
If there were two salaries of $40,000 per year, only one of these values would be used in AVG functions.
Example of formula use
The expression contained in the AVG function does not have to be a single field. You can also use a formula. For example, you may need the average commission.
SELECT AVG(order_value_id * 0.10) AS "Average Commission_id"
FROM order_details_id;
Example of using GROUP BY
You can also use the AVG function to return the department name and average number (in the corresponding department). For example,
SELECT department_id, AVG(quantity_id) AS "Average Quantity_id"
FROM prods
GROUP BY depart;
Since you have specified one column in the SELECT operator that is not included in the AVG function, you must use the GROUP BY offer. Therefore, the field must be specified in the operator GROUP BY.
Fundamentals of SQL using Average Function
MORE NEWS
PreambleNoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...
PreambleMongoDB Conditional operators specify a condition to which the value of the document field shall correspond.Comparison Query Operators $eq...
5 Database management trends impacting database administrationIn the realm of database management systems, moreover half (52%) of your competitors feel...
The data type is defined as the type of data that any column or variable can store in MS SQL Server. What is the data type? When you create any table or...
PreambleMS SQL Server is a client-server architecture. MS SQL Server process starts with the client application sending a query.SQL Server accepts,...
First the basics: what is the master/slave?One database server (“master”) responds and can do anything. A lot of other database servers store copies of all...
PreambleAtom Hopper (based on Apache Abdera) for those who may not know is an open-source project sponsored by Rackspace. Today we will figure out how to...
PreambleMongoDB recently introduced its new aggregation structure. This structure provides a simpler solution for calculating aggregated values rather...
FlexibilityOne of the most advertised features of MongoDB is its flexibility. Flexibility, however, is a double-edged sword. More flexibility means more...
PreambleSQLShell is a cross-platform command-line tool for SQL, similar to psql for PostgreSQL or MySQL command-line tool for MySQL.Why use it?If you...
PreambleWriting an application on top of the framework on top of the driver on top of the database is a bit like a game on the phone: you say “insert...
PreambleOracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...
PreambleIBM pureXML, a proprietary XML database built on a relational mechanism (designed for puns) that offers both relational ( SQL / XML ) and...
What is PostgreSQL array? In PostgreSQL we can define a column as an array of valid data types. The data type can be built-in, custom or enumerated....
PreambleIf you are a Linux sysadmin or developer, there comes a time when you need to manage an Oracle database that can work in your environment.In this...
PreambleStarting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...