What is the distinction among WHERE and HAVING clause in SQL is a standout amongst the most prominent questions during a SQL and database interviews, particularly for the beginners of the field? Since programming employments require more than one expert, it's very basic to see a couple of SQL Interview inquiries in Java and .NET meetings. Even though practically 50% of the developers say that WHERE is utilized in any SELECT query while HAVING clause is just utilized in SELECT queries, which contains total capacity or gathering by clause, which is right. Despite the fact that both WHERE and HAVING clause is utilized to indicate sifting condition in SQL, there are unpretentious distinctions between them. The real tough time that comes into the picture of your interview is when you are asked to clarify the result from a SELECT query, which contains both WHERE and HAVING clause. Many recruiting managers say that only 10% of the candidates can answer that. Today we shall take a deep look into the differences between these two clauses of SQL environment. The blog covers the following points-
SQL is the Structured Query Language that is comprehensive or declarative in nature and used to access data from databases. In SQL and database interview questions, one of the most asked questions by beginners is how Where and Having clause are different in SQL. Since programming language usually needs more than one skill, it is common to evaluate the database knowledge of a candidate in Java or the .Net interviews too. Many Java programmers or .NET developers are not supposed to have the SQL knowledge, fail to answer this question. However, most of the programmers think that where clause is used in any SELECT query while Having clause is only used in SELECT queries that contain aggregate functions or Group by clause too. Though both Where and Having clauses are used to specify the filter conditions in SQL, there is a significant difference between Having and Where clause in SQL. One more major difference between two clauses is that Where clause is used to fetch the data from the table and data which does not meet the condition will not be fetched for the result set. At the same time, having a clause is used to filter summarize or grouped data. Let us understand the concepts deeply by discussing both clauses individually one by one.
|Comparison Parameters||Where Clause||Having Clause|
|Implementation||This clause is implemented in row operations.||This clause is implemented in column operations.|
|Application||This clause is applied to a single row.||This clause is applied to summarized rows or groups.|
|Data extraction||It fetches the selected data from the table based on the condition.||Complete data is fetched together and separated based on condition later.|
|Aggregate Functions||These functions don’t appear in Where clause.||These functions can appear in Having clause.|
|Used along||It can be used with SELECT and other statements like Update, Insert, Delete etc.||It cannot be used without a SELECT query.|
|Acts as||It acts as the pre-filter.||It acts as the post-filter.|
|Group By||Group by clause comes after Where.||Group by clause comes after Having.|
The SQL where clause is used to describe the condition at the time of data retrieval from a single table or by combining multiple tables together. It helps to return a value from the table once the condition is fulfilled. Hence, the Where clause retrieves needed parameters only and rest other parameters are violated. SQL also used logical conditions with Where clause like
“and”, “OR” etc that are known as Boolean conditions too. If the condition sets true then tuples are retrieved quickly, if the condition is set false then it will retrieve any tuples. The logic expression involves comparison operator too such as
<, <=, >, >=, =, and <>. These comparison parameters are used to compare strings or arithmetic expressions. They are frequently used with SELECT statement and other similar statements like Update, Delete etc. Let us understand this clause with the help of an example. There is one Sales Table below that consists of the Product name and Sales amount attributes. The following query can be used to calculate the total sales of phones and speakers. The final output can be given as below. This is the resulting output where rows are filtered first, phone and speaker rows are filtered then the aggregate function is performed.
The having clause in SQL can be used along in conjunction with Group By clause. Having clause helps to retrieve values of groups that fulfil certain conditions. The Where clause is generally used in conjunction with Having clause. Where clause will filter individual row and Having clause will filter summarized data or grouped data. Having clause behaves similarly to Where clause when Group By clause is not used. The group functions like min, max, avg, sum, count appear in two clauses only. These are SELECT or HAVING clause. It specifies the condition on tuples corresponding to each value in the group of attributes. A set of records that satisfy the condition is shown as the result. Consider the same example as given above for the Sales table where total sales of phone and speakers are calculated by Having clause. The query can be written as given below. The output is the same where products are retrieved first then the aggregate function is performed and groups are filtered in the end unlike Where clause. If we want to filter only those products where the total sale is greater than 1000 then the query can be written as below. The final output, in this case, would be: We cannot find this result using Where clause in spite of having and it generates an error message because Where clause cannot be used with aggregate functions.
Conclusion: This is pretty much it. I am sure if you read this blog before your interview, you will ace that one question which is the favorite of the interviewers. Good Luck!
A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.
Receive Latest Materials and Offers on SQL Server Course