Hi
What is the use of Having clause in SQL ?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Priya LingeRank #785 · 2.9K reputationPosted Dec 4, 2011, 8:22 PM EST
1.The SQL HAVING clause is used to restrict conditionally the output of a SQL statement,
by a SQL aggregate function used in your SELECT list of columns.
2.We can't specify criteria in a SQL WHERE clause against a column in the SELECT list for which SQL aggregate function is used. For example the following SQL statement
will generate an error:
SELECT Employee, SUM (Hours)
FROM EmployeeHours
WHERE SUM (Hours) > 24
GROUP BY Employee
3.The SQL HAVING clause is used to do exactly this, to specify a condition for an aggregate function which is used in your query:
SELECT Employee, SUM (Hours)
FROM EmployeeHours
GROUP BY Employee
HAVING SUM (Hours) > 24
OutPut will be :
Employee Hours
John Smith 25
Tina Crown 27
Hope this will help you.
Thanks.
Satyapriya NayakRank #47 · 31.5K reputationPosted Dec 4, 2011, 8:32 PM EST
The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.
The HAVING clause is 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,
SELECT "column_name1", SUM("column_name2")
FROM "table_name"
GROUP BY "column_name1"
HAVING (arithmetic function condition)
Note: the GROUP BY clause is optional.
In our example, table Store_Information,
Table Store_Information
store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999
we would type,
SELECT store_name, SUM(sales)
FROM Store_Information
GROUP BY store_name
HAVING SUM(sales) > 1500
Result:
store_name SUM(Sales)
Los Angeles $1800
Thanks
Prashant ChaudharyRank #1,151 · 1.6K reputationPosted Dec 4, 2011, 10:12 PM EST