SQL Exercises: Compute the average price for unique companies
23. Average Price for Unique Companies
From the following table, write a SQL query to compute the average price for unique companies. Return average price and company id.
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' for each 'pro_com' (Company ID) in the 'item_mast' table.
SELECT AVG(pro_price) AS "Average Price",
       pro_com AS "Company ID"
-- Specifies the table from which to retrieve the data (in this case, 'item_mast').
FROM item_mast
-- Groups the result set by the 'pro_com' column.
GROUP BY pro_com;
Output of the Query:
Average Price Company ID 250.0000000000000000 14 650.0000000000000000 12 3200.0000000000000000 15 5000.0000000000000000 11 1475.0000000000000000 13 500.0000000000000000 16
Code Explanation:
The said SQL query that returns the average price of items grouped by company. The query performs the following operations:
Selects the average price of items from the 'item_mast' table using the AVG function and assigns the result to the alias "Average Price".
Selects the company ID of each item from the 'item_mast' table and assigns it to the alias "Company ID".
Groups the results by the "pro_com" column, which represents the company ID of each item.
The final result is a table with two columns, "Average Price" and "Company ID", showing the average price of items for each company.
Go to:
PREV : Count Products with Price >= 350.
NEXT :
 Sum of Allotment Amount for All Departments.
Practice Online
For more Practice: Solve these Related Problems:
- Write a SQL query to compute the average price for unique companies, but only for companies with an average price greater than 1000.
- Write a SQL query to compute the average price for unique companies, but only for companies with an average price less than 500.
- Write a SQL query to compute the average price for unique companies, but only for companies with an average price between 500 and 2000.
- Write a SQL query to compute the average price for unique companies, but only for companies with an average price greater than 2000.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
