Thank you.
GROUP BY In SQL Server
I want to know why we use GROUP BY In SQL server ?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Hemant SrivastavaRank #581 · 4.2K reputationPosted Aug 5, 2012, 7:40 PM EDT
Following example will show why we use "GROUP BY"
We have the following "Orders" table:SQL GROUP BY Example
We will have to use the GROUP BY statement to group the customers.
We use the following SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
The result-set will look like this:
Let's see what happens if we omit the GROUP BY statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
The result-set will look like this:
If it helps you, pls make it accepted.
Thanks
Jignesh TrivediRank #16 · 50.3K reputationPosted Aug 5, 2012, 7:56 PM EDT
Grouping records is the process of combining like SELECT columns together. Once combined, Aggregate functions such as SUM and AVERAGE can be performed on the set.
GROUP BY should be used to apply aggregate operators to each group.
SELECT Employee, Rank
FROM Employees
GROUP BY Employee, Rank
http://msdn.microsoft.com/en-us/library/ms177673.aspx
hope this will help you.
Rahul Bhatt165 reputationPosted Aug 6, 2012, 1:04 AM EDT
Using Group By, you have sum, min, max, count based on value
Below query return sum of all quantity based on CustomerID
select CustomerID ,SUM(Qty) from ProductDetailTable group by CustomerID
IF you want to take greater then 100 Qty based on CustmerID then user Having Clause
select CustomerID ,SUM(Qty) from ProductDetailTable group by CustomerID having SUM(Qty)>100
Having Clasue used with Group By Clasuse
Satyapriya NayakRank #47 · 31.5K reputationPosted Aug 6, 2012, 4:09 AM EDT
Akkiraju IvaturiRank #473 · 5.3K reputationPosted Aug 6, 2012, 3:35 PM EDT