×

Where vs Having

Difference Between Where and Having

The WHERE and HAVING clauses in SQL are used to filter records stored in tables in the databases. The dissimilarities between the WHERE and HAVING clause is a frequent doubtone might have. The fundamental dissimilarities between them are:

  • The WHERE clause is used to set out a condition for clearing records before any groupings are categorized.
  • The HAVING clause is used to set out a condition for clearingrecords from a group.

Each of the two clausesis discussed in detail in below section.

WHERE Clause

The WHERE clause in MySQL is used withqueries to sort the data from the table. It defines a particular condition when fetching records from a table or multiple tables. If the particular condition is met, only then it returns the value.

The WHERE clause is also capable of implementing the logical connectives. They are also referred to as the Boolean condition that must be true to fetch the data.

SELECT column_name,      
FROM table_name   
WHERE conditions   
GROUP BY column_name;    

The following is the syntax of the WHERE clause:

Example:

Let’s consider the following Employees table.

Emp_idNameGenderHours
101BryanM12
103MikeM10
104DarenF5
105MarieF8
106MarcoM9
107DarenF12
108MikeM10
109MarcoM6
110BryanM5
SELECT * FROM Employees WHERE Hours > 9;

Query:

The above query will return all the records of those employees who have worked for more than 9 hours.

Output:

Emp_idNameGenderHours
101BryanM12
103MikeM10
107DarenF12
108MikeM10

The WHERE clause can also be used with the GROUP BY clause. If this same query is used with the GROUP BY clause, it will give a different result.

Query:

SELECT * FROM 
Employees WHERE 
Hours > 9 
GROUP BY Name;

Output:

Emp_idNameGenderHours
101BryanM12
103MikeM10
107DarenF12

HAVING Clause

The HAVING clause in MySQL is used along with the GROUP BY clause, and this enables users to specify conditions that filter which group results appear in the result. The WHERE and HAVING clausescan be used in a combined manner as well. In such cases, the WHERE clause sorts the data from each row, and then these rows are grouped. After that, calculations are performed on the groups, and lastly, the HAVING clause operates on these groups.

The following is the syntax for the WHERE clause:

SELECT column_name, 
aggregate_function (expression) 
FROM table_name 
WHERE conditions GROUP BY 
column_name HAVING condition;  

Example:

Let’s consider the same Employees table.

Emp_idNameGenderHours
101BryanM12
103MikeM10
104DarenF5
105MarieF8
106MarcoM9
107DarenF12
108MikeM10
109MarcoM6
110BryanM5
SELECT name, 
SUM(Hour) 
FROM employees 
GROUP BY name 
HAVING SUM(Hour) > 6;

Query:

The above query will return the name and the total working hours of all the employees who have worked for more than 6 hours.

Output:

NameHours
Bryan17
Mike20
Daren17
Marie8
Marco15

Difference table between WHERE and HAVING

The key differences between the WHERE clause and the HAVING clause in SQL are listed below in a tabular manner.

WHERE ClauseHAVING Clause
This clause sorts each row.This clause sorts the groups.
This clause is utilized while performing row operations.This clause is utilized while performing column operations.
This clause returns the data from rows based on the given conditionThis clause returns the complete data and then sorts it according to the given condition.
Aggregate functions cannot be used with this clause.Aggregate functions can be used with this clause.
This clause can be used with the SELECT, UPDATE, and DELETE statements.This clause can be used only with the SELECT statement.
The GROUP BY succeeds the WHERE clause.The GROUP BY clause precedes the HAVING clause.

Conclusion

Both these clauses perform similar ways in sorting out the data, although some specific extra features make the HAVING clause more sought after. One can use aggregate functions while querying data using the HAVING clause while such options are not there for the WHERE clause.


Related Topics

SQL SELECT AVG

In this tutorial, we will learn about the aggregate function name avg() function concept in SQL with the help of examples. The AVG() function is one of the aggregate functions in...

3 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

Types of SQL Commands

The Structured Query Language is used to deal with structured data. The data which are stored in the form of tables are structured data. These SQL commands store records or...

10 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL WHERE Clause

The WHERE clause is a search filter that returns only true records. The WHERE clause chooses the selected records based on the given conditions. The WHERE clause is used with...

4 minutes read.

Trigger in SQL

In this article, we will learn about the concept of trigger in SQL and its implementation with the help of an example. A Trigger in Structured Query Language is a set...

4 minutes read.

Grant Command in SQL

What is DCL (Data Control Language)? Data Control Language (DCL) is a computer programming language with syntax intended to manage access to data kept in databases. It is a part of...

3 minutes read.

SQL Count

Structured Query Language Count() Function is used with Structured Query Language SELECT Statement. SQL Count() function returns the number of items that match the specified criteria in the SELECT statement. Count()...

2 minutes read.

SQL INSERT INTO Values

This article will help you in getting a better understanding of a very important SQL INSERT INTO VALUES function. INSERT INTO statement is used to insert or add a new record...

4 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

DDL Commands in SQL

DDL stands for Data Definition Language. Data Definition Language is a bunch of SQL commands used to create, modify and delete Database schema but not the records or data. Data Definition...

5 minutes read.

SQL SET Operator

In this tutorial, we will understand the operator who falls under the SET operator in SQL with the help of different examples. The SQL SET Operator is used to merge the...

5 minutes read.

SQL SELECT SUM

The SQL Sum() function is an aggregate function in SQL that returns the total values of an expression. The expression may be numerical, or it may be an expression. Syntax: SELECT SUM(columnname)...

3 minutes read.

Drop vs Truncate in SQL

In this article, we will learn and understand the Drop and Truncate commands and the difference between these two commands. What is Drop Command? Drop is a Data Definition Language command in...

6 minutes read.

GROUP BY vs ORDER BY

The GROUP BY clause and ORDER BY clause in SQL are used to arrange data obtained by SQL queries. The important difference between the GROUP BY clause and ORDER BY...

4 minutes read.

SQL INSERT INTO SELECT

In this tutorial, we will help you to understand and learn how to copy records from one table and add them to another table in the SQL with the help...

5 minutes read.

SQL Primary Key

A field, which contains unique data in a table, is called a PRIMARY KEY (PK). It means, a PRIMARY KEY field must contain unique data in the table. A PRIMARY KEY...

4 minutes read.

SQL WHERE Statement

SQL WHERE Statement Introduction WHERE clause is used to include a condition while fetching data from tables.When you have to specify a condition that has to be obeyed while data is...

9 minutes read.

SQL Insert multiple rows

This article will deal with a new insertion method in SQL which inserts multiple rows at a time. Multiple records can be inserted at a time into a specific table with...

4 minutes read.

SQL Data Types

In SQL DATA TYPES, each column holds value. Data types can be an integer type, character type, etc., and such data is stored in the database table. The data type is...

4 minutes read.