Difference between Where and Group By
WHERE clause specifies search conditions for the rows returned by the Query and limits rows to a specific row-set. If a table has huge amount of records and if someone wants to get the particular records then using ‘where’ clause is useful.
Attention reader! Don’t stop learning now. Learn SQL for interviews using SQL Course by GeeksforGeeks.
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(), SUM(), MIN(), MAX(), AVG(), etc.
Suppose some sales company wants to get a list of Customers who bought some number of items last year, so that they can sell more some stuff to them this year.
There is table called SalesOrder with columns CustomerId, SalesOrderId, Order_Date, OrderNumber, OrderItem, UnitPrice, OrderQty
Now we need to get the customers who made orders last year i.e. 2017
Using Where clause –
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 will return the row set with all the Customers and corresponding Orders of year 2017.
Using Group By clause –
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 year 2017 and total count of orders each Customer made.
Using Having Clause –
Having clause is used to filter values in Group By clause. The below query filters out some of the rows
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.
You can use ‘Where’ clause with ‘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.
SELECT SalesOrderID, SUM(UnitPrice * OrderQty) AS TotalPrice FROM Sales.SalesOrderDetail WHERE SalesOrderID > 500 GROUP BY SalesOrderID HAVING SUM(UnitPrice * OrderQty) > 10000
So here, the having clause will be applied on the rows that are filtered by where clause. Having clause can only compare results of aggregated functions or column part of the group by.
- WHERE is used to filter records before any groupings take place that is on single rows.
- GROUP BY aggregates/ groups the rows and returns the summary for each group.
- HAVING is used to filter values after they have been groups.