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 Tree:
Practice Online
Query Visualization:
Duration:
Rows:
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.
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
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics