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_id | Name | Gender | Hours |
101 | Bryan | M | 12 |
103 | Mike | M | 10 |
104 | Daren | F | 5 |
105 | Marie | F | 8 |
106 | Marco | M | 9 |
107 | Daren | F | 12 |
108 | Mike | M | 10 |
109 | Marco | M | 6 |
110 | Bryan | M | 5 |
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_id | Name | Gender | Hours |
101 | Bryan | M | 12 |
103 | Mike | M | 10 |
107 | Daren | F | 12 |
108 | Mike | M | 10 |
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_id | Name | Gender | Hours |
101 | Bryan | M | 12 |
103 | Mike | M | 10 |
107 | Daren | F | 12 |
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_id | Name | Gender | Hours |
101 | Bryan | M | 12 |
103 | Mike | M | 10 |
104 | Daren | F | 5 |
105 | Marie | F | 8 |
106 | Marco | M | 9 |
107 | Daren | F | 12 |
108 | Mike | M | 10 |
109 | Marco | M | 6 |
110 | Bryan | M | 5 |
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:
Name | Hours |
Bryan | 17 |
Mike | 20 |
Daren | 17 |
Marie | 8 |
Marco | 15 |
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 Clause | HAVING 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 condition | This 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.