compare AVG(integer) with AVG(integer) of a category - mysql

i have relation with three columns: ProductName, CategoryID and Price. i need to SELECT only those products which Price is higher than average product Price in given category.
(eg when apple(ProductName) is a fruit(CategoryID) it should be selected because its price is higher than average fruit price). How do i do that?
I'm thinking of something like this, but its obviously wrong since I'm comparing Price with Price and CategoryID:
SELECT ProductName, AVG(Price) FROM `products`
WHERE (SELECT AVG(Price)) > (SELECT CategoryID, AVG(Price))
GROUP BY ProductName;

Something like this
SELECT ProductName, Price, CategoryID FROM `products` p
WHERE Price > (SELECT AVG(Price) from `products` where CategoryID = p.CategoryID)

Related

How to SELECT based on grouped SUM that is compared to the AVG of that grouped SUM

I have a table consists of films, categories and prices.
Now I want to select only categories with total price (sum of all the price per film under that category) that is higher than the average of those categories' total prices.
I have been able to find the average of the total prices (thanks to this web) but can't combine it together.
Here are the queries:
-- Return the average sums of the price per category
SELECT AVG(sum_price)
FROM
(
SELECT category, sum(price) AS sum_price
FROM film_list
GROUP BY category
) AS inner_query;
-- Return category and its total price
SELECT category, SUM(price)
FROM film_list
GROUP BY category;
--[Error] Return only the category with sums of price larger than the average of sum of prices
SELECT category, SUM(price)
FROM film_list
WHERE SUM(price) >
(
SELECT AVG(sum_price)
FROM
(
SELECT category, sum(price) AS sum_price
FROM film_list
GROUP BY category
) AS inner_query
);
Any help will be much appreciated, thanks!
Try adding group by and then using having
SELECT category, SUM(price)
FROM film_list
GROUP BY category
HAVING SUM(price) >
(
SELECT AVG(sum_price)
FROM
(
SELECT category, sum(price) AS sum_price
FROM film_list
GROUP BY category
) AS inner_query
);
This is most easily solved using window functions:
SELECT c.*
FROM (SELECT category, SUM(price) AS sum_price,
AVG(SUM(price)) OVER () as avg_total_price
FROM film_list
GROUP BY category
) c
WHERE sum_price > avg_total_price

I want to select customers whose price is higher than the average price

In SQL, Please is this correct?
SELECT COUNT (Price)
FROM orders
WHERE Price > AVG(Price);
you can try sth like this
SELECT customers FROM orders WHERE price > (select AVG(price) from orders) ;

Can't run mysql request

I need to select products (their names) and prices records that have an above average price, but cheaper than 33
My request:
select ProductName, Price
from Products
where Price>(select avg(Price) from Products)
and
Where Price < 33;
It's not working. Why?
Thank you in advance.
Remove the duplicate where like so:
select ProductName, Price
from Products
where Price > (select avg(Price) from Products)
and Price < 33;

Not sure how to write a simple query

Hi I Have a table which has number of products and rating for each, due to some reasons I have different row of each product along with rate of each person who rated it, as following:
p1 2
p2 4
p3 4
p1 4
p1 5
p1 3
p2 7
I am using following query but it shows the average rate of all products, but I am expecting it to show the average rate of each product seperately.
Select ProductName, AVG(Rate) FROM Products
Just GROUP BY productName:
Select ProductName, AVG(Rate) AS 'Average Rate'
FROM Products
GROUP BY productName
Edit: To show the one with highest rate:
SELECT ProductName, Rate
FROM Products
ORDER BY rate DESC
LIMIT 1
Edit 2: To get the product name with the highest average rate:
SELECT productname, AVG(rate) Avgrate
FROM Products
GROUP BY productname
HAVING AVG(rate) =
(
SELECT AVG(rate)
FROM Products
GROUP BY productname
ORDER BY AVG(rate) DESC
LIMIT 1
)
You need to add Group By.
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName
To Sort Result use Order By after Group By
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName ORDER BY ProductName
Above query sort result in Ascending order if you want to sort descending then use DESC,
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName ORDER BY ProductName DESC
To Select Higher Rating Product
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName
HAVING AVG(Rate) = (SELECT MAX(AVG(Rate)) FROM Products GROUP BY ProductName)
This query return all product which have max average rating.
Only One Product with Higher rating use LIMIT in Descending Sort
Select ProductName, AVG(Rate) FROM Products GROUP BY ProductName
ORDER BY ProductName DESC
LIMIT 1

Select cheapest where sku or ean is even

I've a simple query like:
SELECT id, name, price, sku, ean
FROM products
WHERE id IN (1,2,3,4,5,6)
ORDER BY name ASC
Some products are double present (same sku or ean) with diffrent prices.
How to select the cheapest products where sku or ean is the same?