×

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

SQL CREATE TABLE

In SQL tutorial, we learned and created different databases. To stores data in databases, we need to create a table. To create the table, we need to use CREATE TABLE...

5 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

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

SQL Temporary Tables

Temporary Table: The Temporary tables are generally created in TempDB. If the last connection is terminated then the tables are deleted automatically. These are generally used to store and process the...

4 minutes read.

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 Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 minutes read.

TCL Commands in SQL

In Structured Query Language, TCL is an abbreviation for Transaction Control Language. A single unit of work in a database is formed after the consecutive execution of commands is known...

6 minutes read.

Save Point in SQL

In SQL, the classification is done into 4 languages. They are Data Definition Language (DDL)Data Manipulation Language (DML)Transaction Control Language (TCL)Data Control Language (DCL) Save Point falls under the Transaction Control Language....

4 minutes read.

SQL VIEW

The SQL VIEW concept helps to hide the difficulty of the records and provides limitations to access to the database. The SQL view is similar to the SQL tables. In SQL...

7 minutes read.

Pattern Matching in SQL

The LIKE in the Structured Query Language is a Logical Operator. The SQL LIKE is used within the WHERE clause in the SQL. This Like Operator is used with the...

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

SQL CROSS Join

In this tutorial, we will help you understand the concept of the SQL CROSS Join clause with the few examples. The SQL CROSS Join query is used to join one or...

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

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

3 minutes read.

SQL DROP Table

In this tutorial, we will help you to understand how to delete the table from the database in SQL with the help of examples. The DROP TABLE query is used to...

4 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.