Today's Offer - SQL Server Certification Training - Enroll at Flat 10% Off.

- SQL Server Blogs -

What Is The Difference Between Having And Where Clause In SQL?

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-

Difference between Having and Where

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. SQL Server Curriculum 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.

Read: Top 50 SAS Interview Questions and Answers For Fresher, Experienced

Here is the comparison chart of Having and Where.

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.

What is Where Clause in SQL?

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. What is Where Clause in SQL? The following query can be used to calculate the total sales of phones and speakers. What is Where Clause in SQL? The final output can be given as below. What is Where Clause in SQL? This is the resulting output where rows are filtered first, phone and speaker rows are filtered then the aggregate function is performed.

What is a Having Clause in SQL?

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. SQL Server quiz 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. What is a Having Clause in SQL? 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. What is a Having Clause in SQL? If we want to filter only those products where the total sale is greater than 1000 then the query can be written as below. What is a Having Clause in SQL? The final output, in this case, would be: What is a Having Clause in SQL? 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. free SQL Server demo

Read: How to Prevent SQL Injection Attacks?

What are the Key Differences between Having and Where?

  • Where clause is used in row operations and it is generally applied on a single row only whereas Having clause is used in column operations and it is generally applied on summarized data and groups.
  • In where clause, the desired data is fetched according to the applied condition. In contrast, having clause fetches the whole data then separation is made based on the condition.
  • Aggregate functions like min, max, avg, sum, the count can never appear with Where clause while all of these can be used along Having Clause.
  • We cannot use Having clause without a SELECT statement. Conversely, we can use Where clause with SELECT, Update or Delete statements.
  • Where clause usually behaves as the pre-filter while having clause behaves like a post-filter.
  • Where clause can be used with Group by the statement and it comes before Group by clause. It signifies that Where clause filters the rows before aggregate calculations are performed. At the same time, having clause comes before Group by a clause that means it filters data after aggregate functions are performed.
  • Key point, which is additionally the primary contrast between the WHERE and HAVE clause in SQL is that condition indicated in WHERE statement is utilized while getting information (lines) from table, and information which doesn't pass the condition won't be passed into the result set, then again HAVE clause is later used to channel the abridged data or gathered data.
  • In short if both WHERE and HAVING clause is utilized in a SELECT query with total capacity or GROUP BY clause, it will execute before HAVING clause.
  • Where clause can be connected to a solitary row, whereas the Having clause is connected to the entire group. However, it doesn't imply that we can't have both Where and Having clauses in a single query. In the event that both where and Having statements are utilized together in single query, where clause will apply the filter condition to the list of the records, and it will channel the gathering of records from the table and Have clause will be connected to this resultant gathering and other gatherings which meets the condition given in a having statement. It will be separated in the resultant table.
  • One syntax level contrast among WHERE and HAVING statement is that, the former one is utilized before GROUP BY clause, while the later one is utilized after the GROUP BY clause.
  • When WHERE and HAVING clause are utilized together in a SELECT query with a total capacity, WHERE clause is connected first on individual rows and just rows which pass the condition is incorporated for making gatherings. When the group is made, HAVING clause is utilized to channel groups dependent on the condition which has been determined already by the database administrator.

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!

Read: SQL Operators you Need to Know

SQL Tutorial Overview


    Janbask Training

    JanBask Training is a leading Global Online Training Provider through Live Sessions. The Live classes provide a blended approach of hands on experience along with theoretical knowledge which is driven by certified professionals.


Trending Courses

AWS

  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

4 days 24 Nov 2019

DevOps

  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

5 days 25 Nov 2019

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

5 days 25 Nov 2019

Hadoop

  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

6 days 26 Nov 2019

Salesforce

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

14 days 04 Dec 2019

Course for testing

  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

34 days 24 Dec 2019

QA

  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

13 days 03 Dec 2019

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

5 days 25 Nov 2019

SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

8 days 28 Nov 2019

Comments

Search Posts

Reset

Receive Latest Materials and Offers on SQL Server Course

Interviews