'How can I filter a table using an aggregate condition?
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)
Solution 1:[1]
What about this?
SELECT *
FROM Customer_Table
GROUP BY Age
HAVING Age > (SELECT avg(Age) from Customer_Table)
ORDER BY Age desc;
Solution 2:[2]
One option would be
select <columns> from (
select *, avg(age) over() AvgAge
from Customer_Table
)t
where age > AvgAge;
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 | hallibut |
| Solution 2 | Stu |
