SUM function returns the sum of all values in an expression.
Let’s practice with OrderDetails table by following examples:
To get the total money for each selling product we just use the SUM function and group by product. Here is the query:
SELECT productCode,sum(priceEach * quantityOrdered) total
FROM orderdetails
GROUP by productCode

Display:

AVG FUNCTION

AVG is used to calculate average value of an expression. It ignores NULL values. We can use AVG function to calculate the average price of all products buy executing the following query.
SELECT AVG(buyPrice) average_buy_price
FROM Products;
Display:

MAX AND MIN FUNCTION

MAX function returns the maximum and MIN function returns the minimum value of the set of values in expression. As an example, we can use MIN and MAX function to retrieve the highest and lowest price product as follows:
SELECT MAX(buyPrice) highest_price,
       MIN(buyPrice) lowest_price
FROM Products

Display:

COUNT FUNCTION

COUNT function returns the count of the items in expression. We can use COUNT function to count how many products we have as follows:
SELECT COUNT(*) AS Total
FROM products

Display:




0 comments:

Post a Comment

 
Top
Blogger Template