Sql Having

Sql having can be regarded as a WHERE clause for Group By statement. Just as Sql Where limits the rows, so is HAVING clause,but it limits the rows returned by the Group By.

The conditions in HAVING clause can be non-aggregate or aggregate, while in WHERE conditons can only be non aggregates.

The syntax:

SELECT column_list,
       Aggretate_function()
FROM   table_name [WHERE conditions ]
GROUP  BY column_list
HAVING conditions 

Group By clause puts together rows into sets and Having clause puts a conditions on the sets

Example: Use Contact Table, show the State that appears more than once.

CPIdSurnameFirstnameAddressSuburbStatePostcode
1 White Alan 210 George St Sydney NSW 2000
2 Johnson Benjamin 11 Cooper St Surry Hill NSW 2010
3 Brown Peter 72 Haymarket St Haymarket NSW 2000
4 Anderson Thomas 33 Collins St Melbourne VIC 3000
5 Gomdas Tony 78 Marine Pde Coolangatta QLD 4225
6 Jones David 2 Park St Sydney NSW 2000
7 Gutman Philip 27 Swanson St Melbourne VIC 3000

SQL Statement:

SELECT state,
       COUNT(*) AS total
FROM   contacts
GROUP  BY state
HAVING COUNT(*) > 1 

Result:

StateTotal
NSW 4
VIC 2

Important note on the use of WHERE and HAVING clause. WHERE clause limits the rows before grouping, while HAVING chooses the rows after grouping.

From here, you can learn the following:

Sql Select
The core of Sql, selecting data from table.

Sql Distinct
Eliminating duplicate values in the output.

Sql Where
Conditionally selecting rows from table.

Sql Like
Pattern searching for conditions in Where or Having clause

Sql Between
Added to conditions to show results between two values.

Sql In
It enables us to specify multiple values, either as a list or a subquery.

Sql And Or
Connecting two or more conditions in the Where or Having clause.

Sql Count
Counting number of rows.

Sql Sum
Getting the sum of a field

Sql Max
This function will return the highest value.

Sql Min
This function will return the lowest value.

Sql Avg
This function will return the average value.

Sql Order By
Sorting the output

Sql Group By
Grouping rows into sets.

Journey
Buy at AllPosters.com
A bend in the road is not the end of the road... unless you fail to make the turn

Positive Resources

All of us have at least two or three things that excite us. We could talk about these subjects for hours. Good news!. Did you know that Site Built It! can turn this type of deep interest and enthusiasm into a revenue-generating business?

Review some of the Site Build It! case studies and you'll absolutely discover something that ignites more positive ideas into your mind.

SBI! Video Tour


Invest the most
important 30 minutes
you will spend online.

Click, sit back, and
understand why SBI!
makes you succeed.