×

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 Arithmetic Operators

This page contains all the information, learn about SQL Arithmetic Operators concept in the SQL table with the help of examples. The Arithmetic Operators is used to perform mathematical calculations on...

5 minutes read.

SQL SELECT IN

SQL SELECT IN is a logical operator in Structured Query Language. It is used in SQL queries to reduce the use of multiple 'OR' operators. s The IN operator in SQL...

6 minutes read.

SQL Aggregate Functions

In this tutorial, we will learn and understand the SQL Aggregate Functions concept with the help of examples. SQL Aggregate Function is a function used to perform calculation operations on one...

4 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 Alter Table

In Structured Query Language, if you want to add columns in an existing table, then modify the table, or delete columns from the table. All these operations are allowed only...

7 minutes read.

SQL Create Database

This tutorial will help us understand how to CREATE a Database in SQL with a few examples. The first step for storing structured records in the database is creating the databases. We...

4 minutes read.

SQL CRUD Operation

In any computer language, CRUD operation is a foundation. CRUD operation rules are applied in databases as well. These operations are the basic operations used to perform with any database...

7 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.

SQL INSERT Table

In this tutorial, we will help you to understand and learn how to add records to the table in SQL with the help of examples. SQL INSERT query is used to...

5 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

SQL COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

SQL SELECT MIN

The SQL Min() function is an aggregate function in SQL. SQL Min() returns the minimum value of a given condition. The expression may be numerical, or it may be an expression. The syntax...

5 minutes read.

How to use LIKE in SQL

In this SQL article, we will learn and understand how to use LIKE to the columns in the SQL tables. What is Like? Like is an operator in the SQL. It is...

7 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

DML Commands in SQL

DML is an abbreviation of Data Manipulation Language. Data Manipulation Language commands in Structured Query Language manipulate the data in the database. DML commands are used to retrieve records, add records,...

4 minutes read.

How to drop a column in SQL?

How to drop a column in SQL Introduction To delete a column from an already created table, one needs to use the ALTER command along with the DROP COLUMN clause. Syntax: ALTER TABLE tablename...

4 minutes read.

How to use the BETWEEN operator in SQL

In this entire SQL article, we will understand and learn about the BETWEEN operator concept and how to use it in SQL. What is the BETWEEN operator in SQL? The Between operator...

4 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.

SQL Topological Sorting

It is for Directed Acyclic Graph, which linearly describes the ordering of graphs. It is not possible for all graphs, it is possible for only Directed acyclic graphs. Applications of Topological...

3 minutes read.