w3resource

SQL Exercise: Average price of all products of the given manufacturer

SQL Basic Select Statement: Exercise-26 with Solution.

From the following table, write a SQL query to calculate the average price for a manufacturer code of 16. Return avg.

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 value of the 'pro_price' column from the 'item_mast' table.
SELECT AVG(pro_price)
-- Specifies the table from which to retrieve the data (in this case, 'item_mast').
FROM item_mast
-- Filters the rows to only include those where the 'pro_com' column has the value 16.
WHERE pro_com = 16;

Output of the Query:

avg
500.0000000000000000

Code Explanation:

The said SQL query selects the average price of all items from the 'item_mast' table where the value of the "pro_com" column is equal to 16.

Relational Algebra Expression:

Relational Algebra Expression: Calculate the average price of all products of the manufacturer which code is 16.

Relational Algebra Tree:

Relational Algebra Tree: Calculate the average price of all products of the manufacturer which code is 16.

Practice Online


Query Visualization:

Duration:

Query visualization of Calculate the average price of all products of the manufacturer which code is 16 - Duration

Rows:

Query visualization of Calculate the average price of all products of the manufacturer which code is 16 - Rows

Cost:

Query visualization of Calculate the average price of all products of the manufacturer which code is 16 - Cost

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

Previous SQL Exercise: Find all products between Rs.200 and Rs.600.
Next SQL Exercise: Display the item name and price

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/sql-exercises/sql-retrieve-exercise-26.php