Home > Net >  Average Constant In SQL Query
Average Constant In SQL Query

Time:03-16

I have a database used for a store. I have a table with different items and their prices. I need to get the name and price of each item whose price is greater than the average price of all items, but I also need to get that average value in the same query.

I can easily get the name and price of those items like this:

select art_name, art_price
from articles
where art_price>(select avg(art_price) from articles);

But I don't know how to get the average value in the same query.

What happens to you to solve this problem?

CodePudding user response:

Use having

select art_name, art_price
from articles
having art_price>avg(art_price);

EDIT after comment:

select art_name, art_price, avg(art_price) as average
from articles
group by art_name, art_price;
  •  Tags:  
  • sql
  • Related