w3resource

SQL Exercises: Calculate the average price of all the products

SQL Aggregate Functions: Exercise-21 with Solution

From the following table, write a SQL query to calculate the average product price. Return average product price.

Sample table: item_mast

 PRO_ID PRO_NAME                       PRO_PRICE    PRO_COM
------- ------------------------- -------------- ----------
    101 Mother Board                    3200.00         15
    102 Key Board                        450.00         16
    103 ZIP drive                        250.00         14
    104 Speaker                          550.00         16
    105 Monitor                         5000.00         11
    106 DVD drive                        900.00         12
    107 CD drive                         800.00         12
    108 Printer                         2600.00         13
    109 Refill cartridge                 350.00         13
    110 Mouse                            250.00         12

Sample Solution:

-- This query calculates the average 'pro_price' from the 'item_mast' table and assigns it the alias "Average Price".
SELECT AVG(pro_price) AS "Average Price"
-- Specifies the table from which to retrieve the data (in this case, 'item_mast').
FROM item_mast;

Output of the Query:

Average Price
1435.0000000000000000

Code Explanation:

The said SQL query calculates the average price of all items in the 'item_mast' table. The "AVG(pro_price)" function calculates the average value of the "pro_price" column, and the "AS 'Average Price'" clause gives the result column a label of "Average Price".

Practice Online


Query Visualization:

Duration:

Query visualization of Calculate the average price of all the products - Duration

Rows:

Query visualization of Calculate the average price of all the products - Rows

Cost:

Query visualization of Calculate the average price of all the products - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: Count how many salesmen each day register orders.
Next SQL Exercise: Find the number of products over or equal to Rs 350.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.