Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the opposite from condition HAVING?

What I have tried:

Select from dbo.test HAVING condition

Select from dbo.test NOT HAVING condition
Posted
Updated 1-Sep-20 1:39am

HAVING is only used in GROUP BY statements, it can't be used in a "vanilla" SELECT.
Instead, you need a WHERE clause.
SQL
SELECT * FROM MyTable WHERE <condition>

If you want all the rows where a column does not contain 666 for example, you can either reverse the condition:
SQL
SELECT * FROM MyTable WHERE MyColumn != 666
Or use the NOT operator:
SQL
SELECT * FROM MyTable WHERE NOT MyColumn = 666;
 
Share this answer
 
SQL syntax of HAVING:
SQL
SELECT expression1, expression2, ... expression_n, 
       aggregate_function (aggregate_expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n
HAVING condition;

All Having needs is a condition. That condition is on you to define. You can always write the opposite of it and be done. Why do you need a NOT?

SQL
--NOT HAVING usage for above HAVING a>10
HAVING a<10

Another example:
SQL
--USe negate in the condition itself
HAVING SOMEID NOT IN (2,4)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900