Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Ordering
Limiting the output
Eliminating duplicate results
Aggregation
16. Find the average value
Grouping
HAVING: filtering and ordering groups
Let's practice

Instruction

OK, now that you know what the best salary is, let's discuss another function:

SELECT
  AVG(TotalSum) AS AvgTotalSum
FROM Order
WHERE CustomerId = 100;

The AVG() function finds the average value of the specified column. In our example, we'll get the average order value for the customer with ID 100.

It is important to note that in SQL Server, AVG() used on an integer will use integer division and return an integer. For example, the average of the four numbers 1, 2, 2, and 2 will be 1 (even though it's 1.75 with floating-point division). Keep that in mind when you use AVG() on integer columns in SQL Server.

Exercise

Find the average salary in the Employee table for the year 2013. Name the column AvgSalary.

Stuck? Here's a hint!

Type:

SELECT
  AVG(Salary) AS AvgSalary
FROM Employee
WHERE Year = 2013;