Select Count, AVG, SUM
The COUNT() function returns the number of rows.
The AVG() function returns the average value of a column.
The SUM() function returns the total sum of a column.
SQL Select COUNT() Syntax:
SELECT COUNT(Column1)
FROM Table_Name
WHERE condition;
SQL Select AVG() Syntax:
SELECT AVG(Column1)
FROM Table_Name
WHERE condition;
SQL Select SUM() Syntax:
SELECT SUM(Column1)
FROM Table_Name
WHERE condition;
Example:
Lets use the following table to count the number of Products from the SupplierName column with the name of ‘TechOne’.
ProductID | ProductName | Description | Price | SupplierName | SupplierID |
---|---|---|---|---|---|
1 | Super Printer | 9000 series printer | 300 | TechOne | 1 |
2 | Laptop 5 | 4000 Laptop 5th generation | 700 | TechTwo | 2 |
3 | 3D Printer | 3D Print Pro | 900 | TechOne | 1 |
4 | Labtop 7 | 100 Labtop 7 processor | 1200 | TechTwo | 2 |
5 | Camera 400 | Camera Ultra 400 | 400 | TechThree | 3 |
SELECT Count (SupplierName) as Result
FROM Products
WHERE SupplierName = ‘TechOne’
Results:
Result |
---|
2 |
Now lets find the average price of Products from the SupplierName column with the name of ‘TechOne’.
SELECT AVG(Price) as Result
FROM Products
WHERE SupplierName = ‘TechOne’
SELECT AVG(Price) as Result
FROM Products
WHERE SupplierName = ‘TechOne’
Results:
Result |
---|
600 |
Finally lets find the sum price of Products from the SupplierName column with the name of ‘TechOne’.
SELECT SUM(Price) as Result
FROM Products
WHERE SupplierName = ‘TechOne’
Results:
Result |
---|
900 |
SQL Tutorial