Dear readers of our blog, we'd like to recommend you to visit the main page of our website, where you can learn about our product SQLS*Plus and its advantages.
 
SQLS*Plus - best SQL Server command line reporting and automation tool! SQLS*Plus is several orders of magnitude better than SQL Server sqlcmd and osql command line tools.
 

REQUEST COMPLIMENTARY SQLS*PLUS LICENCE

Enteros UpBeat offers a patented database performance management SaaS platform. It proactively identifies root causes of complex revenue-impacting database performance issues across a growing number of RDBMS, NoSQL, and deep/machine learning database platforms. We support Oracle, SQL Server, IBM DB2, MongoDB, Casandra, MySQL, Amazon Aurora, and other database systems.

Average – SQL Server AVG function

23 June 2020

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_1name_idage_idsalary_id
1Peter23100
2John24200
3Jim25300

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

 
Tags: , , , ,

MORE NEWS

 

Preamble​​NoSql is not a replacement for SQL databases but is a valid alternative for many situations where standard SQL is not the best approach for...

Preamble​​MongoDB 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...

Preamble​​MS 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...

Preamble​​Atom 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...

Preamble​​MongoDB 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...

Preamble​​SQLShell 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...

Preamble​​Writing 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...

Preamble​​Oracle Coherence is a distributed cache that is functionally comparable with Memcached. In addition to the basic function of the API cache, it...

Preamble​​IBM 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....

Preamble​​If 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...

Preamble​​Starting with Microsoft SQL Server 2008, by default, the group of local administrators is no longer added to SQL Server administrators during the...