'Average Constant In SQL Query

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?

sql


Solution 1:[1]

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;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1