Open In App

Difference Between Where and Group By

Last Updated : 11 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

WHERE and GROUP BY clauses are essential tools for filtering and organizing data in SQL queries. While both are used to refine the output of a query, they serve distinct purposes.

In this article, we will explore the differences between WHERE and GROUP BY clauses, including their syntax, use cases, and how they can be applied together. Additionally, we’ll discuss the role of the HAVING clause in combination with GROUP BY.

Differences Between WHERE and GROUP BY Clauses

Here are the key differences between the WHERE and GROUP BY clauses:

filter the rows from a table based on a specific condition.

FeatureWHERE ClauseGROUP BY Clause
PurposeGroups rows by one or more columns.
Aggregate FunctionsNot used with aggregate functions.Used with aggregate functions.
UsageCan be used with SELECT, UPDATE, and DELETE.Only used with SELECT statements.
ScopeOperates on individual rows.Operates on groups of rows.
HAVING ClauseNot applicabg filtering.le.Often used for post-grouping filtering.

The WHERE Clause

The WHERE Clause generally is used to filter the rows from a table on a specific condition. WHERE clause specifies search conditions for the rows returned by the Query and limits rows to a specific row set. If a table has a huge amount of records and if someone wants to get the particular records then using the 'where' clause is useful. The Syntax of the WHERE Clause is

Syntax:

SELECT column1,column2,cloumn3....
FORM table_name
WHERE Condition;

Example:

SELECT * 
FROM [Sales].[Orders]
WHERE Order_Date >= '2017-01-01 00:00:00.000'
AND Order_Date < '2018-01-01 00:00:00.000'

This query returns all orders placed in the year 2017 by filtering rows based on the Order_Date column.

GROUP BY Clause

Group by clause is used to group rows by one or more columns . Group by Clause is used with aggregate functions like SUM(), AVG(), MIN() etc. GROUP BY clause summaries identical rows into a single/distinct group and returns a single row with the summary for each group, by using appropriate Aggregate function in the SELECT list, like COUNT(), AVG(), SUM(), MIN(), MAX(), etc. The Syntax of the GROUP BY Clause is

Syntax:

SELECT column1,column2
from table_name
GROUP BY column1,column2 ;

Example:

SELECT CustomerID, COUNT(*) AS OrderNumbers
FROM [Sales].[Orders]
WHERE Order_Date >= '2017-01-01 00:00:00.000'
AND Order_Date < '2018-01-01 00:00:00.000'
GROUP BY CustomerId

This will return the row set of the Customers (CustomerId) who made orders in theyear 2017 and the total count of orders each Customer made.

Combining GROUP BY with HAVING Clause

The HAVING clause is used to filter the results of aGROUP BYoperation. Unlike the WHERE clause, which operates on individual rows, the HAVING clause evaluates conditions on grouped data. We will see how this works with the help of an example. The below query filters out some of the rows.

Syntax:

SELECT column1,column2
from table_name
GROUP BY column1,column2 HAVING condition;

Example:

SELECT SalesOrderID,
SUM(UnitPrice* OrderQty) AS TotalPrice
FROM Sales.SalesOrderDetail
GROUP BY SalesOrderID
HAVING TotalPrice > 5000

Since the WHERE clause’s visibility is one row at a time, there isn’t a way for it to evaluate the SUM across all SalesOrderID’s. The HAVING clause is evaluated after the grouping is created. We can use the 'Where' clause with the 'Having' clause as well.

The WHERE clause is applied first to the individual rows in the tables. Only the rows that meet the conditions in the WHERE clause are grouped. The HAVING clause is then applied to the rows in the result set.

Example:

SELECT SalesOrderID,
SUM(UnitPrice * OrderQty) AS TotalPrice
FROM Sales.SalesOrderDetail
WHERE SalesOrderID > 500
GROUP BY SalesOrderID
HAVING SUM(UnitPrice * OrderQty) > 10000

Explanation:

  • The WHERE clause filters rows where SalesOrderID > 500.
  • The GROUP BY clause groups rows bySalesOrderID.
  • The HAVING clause filters groups where the total price exceeds 10,000

So here, the having clause will be applied to the rows that are filtered by the where clause. Having a clause can only compare the results of aggregated functions or column part of the group by.

Conclusion

In summary, the WHERE clause filters records before any grouping occurs, making it ideal for row-level filtering. The GROUP BY clause aggregates rows and returns summaries for each group, while the HAVING clause applies conditions to grouped data. Understanding the differences and proper usage of these clauses is essential for writing efficient and effective SQL queries.


Article Tags :

Similar Reads