The AVG() function in MySQL is used to calculate the average value of a given set of values.
The function takes a single argument, which is the column or expression to be averaged, and returns the average as a decimal value.
Our table:
mysql> SELECT * FROM Stores;
+------+--------+------------+-----------------+-----------+-------+
| s_id | city | store_name | product | available | price |
+------+--------+------------+-----------------+-----------+-------+
| 1 | London | London_1 | Gold PSU | 153 | 100 |
| 2 | London | London_2 | Gold PSU | 75 | 100 |
| 3 | Berlin | Berlin_1 | Green PSU | 50 | 120 |
| 4 | Berlin | Berlin_2 | XYZ Motherboard | 5 | 75 |
| 5 | Moscow | Moscow_1 | Extension Cable | 50 | 25 |
| 6 | Moscow | Moscow_2 | LPT Cables | 500 | 10 |
| 7 | Miami | Miami_1 | COM Cables | 1450 | 5 |
| 8 | Paris | Paris_1 | NIC | 350 | 15 |
+------+--------+------------+-----------------+-----------+-------+
8 rows in set (0.00 sec)
mysql>
AVG example:
mysql> SELECT AVG(price) AS AvgPrice FROM Stores;
+----------+
| AvgPrice |
+----------+
| 56.2500 |
+----------+
1 row in set (0.05 sec)
mysql>
Very simple.
No comments:
Post a Comment