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.

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

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:

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:

Example:

Let’s consider the same Employees table.

Emp_idNameGenderHours
101BryanM12
103MikeM10
104DarenF5
105MarieF8
106MarcoM9
107DarenF12
108MikeM10
109MarcoM6
110BryanM5

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.

Pin It on Pinterest

Share This