Difference Between Where and Having Clause in SQL
As we have seen a fleeting introduction to Where and Having Clause in SQL. Now we will study the difference between Where and Having Clause in SQL. The notable differences between these two SQL are described in the table provided below:
Key Differences Between Where and Having Clause in SQL
Where Clause in SQL | Having Clause in SQL |
On the specified condition, the WHERE Clause filters the records from the given table based. | On the specified condition, the HAVING Clause filters the record from the given groups based. |
The WHERE clause can be used without considering the GROUP BY Clause. | The HAVING Clause cannot be used without considering the GROUP BY Clause |
This Clause is implemented in row operations. | This Clause is implemented in column operation. |
It cannot contain aggregate function. | It can contain aggregate functions. |
SELECT, UPDATE, DELETE statement can be used with the WHERE Clause. | Only the HAVING Clause can be used with HAVING SQL. |
This Clause is used before the GROUP BY Clause. | This Clause is used after the GROUP BY Clause. |
With the single-row function like UPPER, LOWER etc WHERE Clause is used. | With multiple-row functions like SUM, COUNT etc, HAVING Clause is used. |
What is the Where Clause in SQL?
WHERE Clause is used to search the records from the given table or the table’s rows before they are grouped, only those records will be taken which will satisfy the specified condition in the WHERE clause. Where Clause in SQL is also carried out by some operations such as SELECT, UPDATE, and DELETE statements.
Example of Where Clause in SQL
Consider a table given below for “T-SHIRTS”
T-SHIRTS_ID | T-SHIRTS_SIZE | T-SHIRTS_PRICE |
#010 | S | 230 |
#011 | M | 324 |
#012 | L | 430 |
#013 | XL | 730 |
Take the “Query” for example
SELECT T-SHIRTS_ID, PRICE FROM T-SHIRTS WHERE PRICE > 350
Difference Between Where and Having Clause in SQL PDF
Output for this query can be given as
T-SHIRTS_ID | T-SHIRTS_PRICE |
#012 | 430 |
#013 | 730 |
What is Having Clause in SQL?
The HAVING clause is generally used to filter the records of given data from the groups based on the given condition. This HAVING clause is used in the column functions, and according to given conditions, it is applied to aggregate rows or groups. Those groups that match the given condition will appear in the final result. The HAVING Clause is only used with the SELECT statement.
Example of Having Clause in SQL
Take the table below for T-SHIRTS
T-SHIRTS_ID | SIZE | PRICE |
#089 | M | 2200 |
#090 | M | 2000 |
#091 | L | 2500 |
Suppose we want the count of T-SHIRTS with the count of size > L. Applying the “Having” clause to the content in the table.
SELECT COUNT(T-SHIRTS_ID), T-SHIRTS SIZE
FROM T-SHIRTS
GROUP BY SIZE
HAVING COUNT(SIZE) > L;
The output is mentioned below in the table:
COUNT(T-SHIRTS) | T-SHIRTS SIZE |
2 | M |
Comments
write a comment