Search code examples
sqlhaving

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

  • What about this?

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