blog post

The GROUP BY clause groups records into summary rows.
GROUP BY returns one records for each group.
GROUP BY typically also involves aggregates: COUNT, MAX, SUM, AVG, etc.
GROUP BY can group by one or more columns.

SQL GROUP BY Examples

Problem: List the number of customers in each country
SELECT COUNT(Id), Country 
  FROM Customer
 GROUP BY Country

Problem: List the number of customers in each country sorted high to low
SELECT COUNT(Id), Country 
  FROM Customer
 GROUP BY Country
 ORDER BY COUNT(Id) DESC

Problem: List the total amount ordered for each customer
SELECT SUM(O.TotalPrice), C.FirstName, C.LastName
  FROM [Order] O JOIN Customer C 
    ON O.CustomerId = C.Id
 GROUP BY C.FirstName, C.LastName
 ORDER BY SUM(O.TotalPrice) DESC

This query uses a JOIN with Customer to obtain customer names

  • Share This Story


Pleas give your valuable comments to improve the contents.

about author

Atul kumar shukla


I'm Atul Kumar Shukla, working as a web developer.
Welcome to my Blog. This is my personal blog where i post my own questions and answers , articles about ASP.Net., SQL Server, HTML, CSS, Jquery and many more..

Visit Profile

Leave a comment

Name
E-mail
Url
Message

Get More Stuff
Subscribing to the TheOneblogs newsletter and get intersting stuff and updates to your email inbox.
Your Email

 

Style Switcher

Change Color

×! We use cookies to improve your user experience. More information