Get the average price : AVG « Aggregate Functions « SQL / MySQL






Get the average price

      
mysql>
mysql> CREATE TABLE IF NOT EXISTS multimeters
    -> (
    ->   id             INT             AUTO_INCREMENT PRIMARY KEY,
    ->   model          CHAR(10)        NOT NULL,
    ->   price          DECIMAL(3,2)    NOT NULL
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO multimeters (model, price)   VALUES ("Standard", 11.75);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> INSERT INTO multimeters (model, price)   VALUES ("Super", 19.50);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> INSERT INTO multimeters (model, price)   VALUES ("DeLuxe", 24.99);
Query OK, 1 row affected, 1 warning (0.00 sec)

mysql>
mysql> SELECT * FROM multimeters;
+----+----------+-------+
| id | model    | price |
+----+----------+-------+
|  1 | Standard |  9.99 |
|  2 | Super    |  9.99 |
|  3 | DeLuxe   |  9.99 |
+----+----------+-------+
3 rows in set (0.00 sec)

mysql>
mysql> SELECT AVG(price) AS avg_price
    -> FROM multimeters;
+-----------+
| avg_price |
+-----------+
|  9.990000 |
+-----------+
1 row in set (0.00 sec)

mysql>
mysql>
mysql> # delete this sample table
mysql> DROP TABLE IF EXISTS multimeters;
Query OK, 0 rows affected (0.00 sec)

mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.To display an average value of zero in that case, modify the query to test the value of AVG( ) with IFNULL( )
2.AVG() Function averages the values returned by a specified expression.
3.Returning the Average, Minimum, and Total Values with AVG( ), MIN( ), and SUM( )
4.Average length
5.How much you paid for each author's books, in total and on average
6.Find the average sales amount per sales quarter.
7.How many miles did the drivers in the mytable table travel? What was the average miles traveled per day?