The Having clause is optional and used in combination with the Group By clause. It is similar to the Where clause, but the Having clause establishes restrictions that determine which records are displayed after they have been grouped. It is typically placed near the end of the SQL statement, and a SQL statement with the Having clause may or may not include the Group By clause.
The syntax for Having is as follows:
|
Example #1
|
This query returns a list of different items and a field called ‘TotalSum’ that counts the result sum of antiques that supply each item. The Having clause will return only those items with result sum greater than 57 dollars.
Example #2
|
This example displays a list of seller’s ids, along with their count, but only where there are more than one seller with BuyerID equal to 15.
Example #3
|
This example returns a list of items only where the whole group meets the criteria of the Having clause, that is only items of which there are more than 1, and none of which cost more than $500. This is an example of linking multiple expressions together by logical operators such as AND and OR. A Having clause can contain up to 40 such expressions.