SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries What are single row and multiple row subqueries?

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL How to get current year in SQL server 2012? User Input in PL/SQL

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.