00 Votes

MySQL: Read out Prices over or under the average Price

Question by Guest | 2016-07-12 at 23:13

In my MySQL database, I have a table in which I have saved my products and the corresponding prices.

Now, I would like to create a MySQL query returning all products that are either listed over or under the average or mean price of all products in my query or database.

I am still feasible to calculate the average, however, I do not know how to put this into the query. Can someone help me?

ReplyPositiveNegative
0Best Answer0 Votes

You can just calculate the average price using "SELECT AVG(price) FROM products".

After that, you only have to compare the price of each product with this calculated average:

SELECT product, price FROM products
WHERE price > (SELECT AVG(price) FROM products)

With this, you are reading out all prices lying over the average. If you want to get the prices under the average instead, you have to replace the > with a < to make it work.
2016-07-13 at 08:08

ReplyPositive Negative
Reply

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.