Le funzioni COUNT(), AVG() e SUM() restituiscono rispettivamente il conteggio delle righe, la media e il totale di una colonna numerica.
La funzione COUNT() restituisce il numero di righe che corrispondono ai criteri specificati.
SELECT COUNT(column_name)
FROM table_name
WHERE condition
La funzione AVG() restituisce il valore medio di una colonna numerica.
SELECT AVG(column_name)
FROM table_name
WHERE condition
La funzione SUM() restituisce la somma totale di una colonna numerica.
SELECT SUM(column_name)
FROM table_name
WHERE condition
Quello che segue è un esempio della tabella "Products" ("Prodotti") del database "Northwind":
| ProductID | ProductName | SupplierID | CategoryID | Unit | Price |
|---|---|---|---|---|---|
| 1 | Chais | 1 | 1 | 10 boxes x 20 bags | 18.00 |
| 2 | Chang | 1 | 1 | 24 - 12 oz bottles | 19.00 |
| 3 | Aniseed Syrup | 1 | 2 | 12 - 550 ml bottles | 10.00 |
| 4 | Chef Anton's Cajun Seasoning | 2 | 2 | 48 - 6 oz jars | 22.00 |
| 5 | Chef Anton's Gumbo Mix | 2 | 2 | 36 boxes | 21.35 |
La seguente istruzione SQL trova il numero di prodotti:
Run SQLSELECT COUNT(ProductID)
FROM Products
La seguente istruzione SQL trova il prezzo medio di tutti i prodotti:
Run SQLSELECT AVG(Price)
FROM Products
Quello che segue è un esempio della tabella "OrderDetails" ("Dettagli ordine") del database "Northwind":
| OrderDetailID | OrderID | ProductID | Quantity |
|---|---|---|---|
| 1 | 10248 | 11 | 12 |
| 2 | 10248 | 42 | 10 |
| 3 | 10248 | 72 | 5 |
| 4 | 10249 | 14 | 9 |
| 5 | 10249 | 51 | 40 |
La seguente istruzione SQL trova la somma dei campi "Quantity" ("Quantità") nella tabella "OrderDetails" ("Dettagli ordine"):
Run SQLSELECT SUM(Quantity)
FROM OrderDetails