Search code examples
sqlrdbms

having clause is not working in sql


I dont understand whats wrong in this statment.

  Select customername,LEN(address) 
  FROM customers group by customername having LEN(address) = 13;

This is the error message HAVING clause (LEN(address)=13) without grouping or aggregation.


Solution

  • Neither address nor LEN(address) is in the GROUP BY. So, you either need to add them or wrap the expressions in an aggregation function:

    SELECT customername, MAX(LEN(address))
    FROM customers 
    GROUP BY customername 
    HAVING MAX(LEN(address)) = 13;
    

    Or if you just want customers with a length of 13, perhaps no aggregation is needed at all:

    SELECT customername
    FROM customers 
    WHERE LEN(address) = 13;