Home > Net >  How can I filter a table using an aggregate condition?
How can I filter a table using an aggregate condition?

Time:03-24

I have a simple table of 10 customers with ID, Name and Age. How can I filter this so that I only select Customers with Age > 30? I tried the HAVING clause but keep running into problems.

Thanks in advance.

Here's my code below: My Code below:

SELECT *
FROM Customer_Table
HAVING Age > AVG(AGE)

CodePudding user response:

One option would be

select <columns> from (
  select *, avg(age) over() AvgAge
  from Customer_Table
)t
where age > AvgAge;

CodePudding user response:

What about this?

SELECT *
FROM Customer_Table
GROUP BY Age
HAVING Age > (SELECT avg(Age) from Customer_Table)
ORDER BY Age desc;
  • Related