In this article, we will be discussing having clause in MS SQL Server.
There are certain instances where the data to be extracted from the queries is done using certain conditions. To do this, having clause is used. Having clause extracts the rows based on the conditions given by the user in the query. Having clause has to be paired with the group by clause in order to extract the data. Otherwise, an error is produced.
select select_list from table_name group by group_list having conditions
Suppose, a user wants to extract the roll numbers of the students whose name start with R from the Student table, the query is as follows –
select roll number from student having name like 'R%'
The output will display an error. This is because the group by clause has not been included in the query.
The modified query to get the desired results is as follows –
select roll number from student group by name having name like 'r%'
The output is as follows –
‘Where’ clause is generally used for extracting queries by including conditions. ‘Where’ and ‘Having’ clause, both are used for extracting data using certain conditions. Yet, ‘Having’ clause extracts data by grouping based on the group list and ‘Where’ clause extracts data directly by inserting conditions.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Where clause in MS SQL Server
- Distinct clause in MS SQL Server
- Difference between Having clause and Group by clause
- SQL | ON Clause
- SQL | USING Clause
- SQL | WHERE Clause
- Having vs Where Clause in SQL
- SQL | WITH clause
- SQL | Except Clause
- SQL | LIMIT Clause
- SQL | Sub queries in From Clause
- SQL | Intersect & Except clause
- SQL | With Ties Clause
- SQL | Distinct Clause
- SQL | SELECT TOP Clause
- SQL | Union Clause
- Difference between From and Where Clause in SQL
- Difference between Where and Having Clause in SQL
- PHP | MySQL WHERE Clause
- Update clause in Cassandra
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.