SQL GROUP BY Clause

The SQL GROUP BY syntax

The general syntax is:

  1. SELECT columnnames
  2. FROM tablename
  3. WHERE condition
  4. GROUP BY columnnames

The general syntax with ORDER BY is:

  1. SELECT columnnames
  2. FROM tablename
  3. WHERE condition
  4. GROUP BY columnnames
  5. ORDER BY columnnames

 


 

SQL GROUP BY Examples

Problem: List the number of customers in each country

  1. SELECT COUNT(Id), Country
  2. FROM Customer
  3. GROUP BY Country

Results: 21 records.

Count Country
3 Argentina
2 Austria
2 Belgium
9 Brazil
3 Canada

 


Problem: List the number of customers in each country sorted high to low

  1. SELECT COUNT(Id), Country
  2. FROM Customer
  3. GROUP BY Country
  4. ORDER BY COUNT(Id) DESC

Results: 21 records.

Count Country
13 USA
11 France
11 Germany
9 Brazil
7 UK

 


Problem: List the total amount ordered for each customer

  1. SELECT SUM(O.TotalPrice), C.FirstName, C.LastName
  2. FROM [Order] O JOIN Customer C
  3. ON O.CustomerId = C.Id
  4. GROUP BY C.FirstName, C.LastName
  5. ORDER BY SUM(O.TotalPrice) DESC

This query uses a JOIN with Customer to obtain customer names
Results: 89 records.

Sum FirstName LastName
117483.39 Horst Kloss
115673.39 Jose Pavarotti
113236.68 Roland Mendel
57317.39 Patricia McKenna
52245.90 Paula Wilson
34101.15 Mario Pontes
32555.55 Maria Larsson

“SQL GROUP BY Clause”의 38개의 댓글

  1. I feel that is among the such a lot vital info for me.
    And i am satisfied studying your article. But want to commentary on few common issues, The site style is perfect, the articles is
    truly great : D. Good job, cheers

  2. Cbd oil that works 2020
    I have been browsing on-line greater than 3 hours these days, yet I never
    discovered any fascinating article like yours. It’s
    beautiful price sufficient for me. In my view, if
    all web owners and bloggers made excellent content material as you probably did,
    the web might be a lot more useful than ever before. best rated cbd oil http://j.mp/3drY6lE cbd oil
    that works 2020 http://j.mp/3drY6lE

  3. After checking out a number of the articles on your blog, I honestly appreciate your way of writing a blog.

    I saved as a favorite it to my bookmark webpage list and will be checking back in the near
    future. Please check out my web site too and let me know your opinion.

  4. Hi, I believe your web site could possibly be having browser
    compatibility issues. Whenever I look at your web site
    in Safari, it looks fine but when opening in Internet Explorer, it’s got some overlapping issues.
    I just wanted to provide you with a quick heads up!
    Aside from that, wonderful site! adreamoftrains web
    hosting reviews

  5. 핑백: b2b

  6. 핑백: situs bola

  7. 핑백: replica rolex sea dweller

  8. 핑백: 홀덤사이트

  9. 핑백: Köp Lyrica online

  10. 핑백: health insurance

  11. 핑백: Sexual Revolution (Roger Waters song)

  12. 핑백: UNICORN

  13. 핑백: fake rolex

  14. 핑백: buy morphine online

  15. 핑백: World Market URL

  16. 핑백: Köp smärtpiller i Sverige

  17. 핑백: cabins for sale in payson az

  18. 핑백: sagaming

  19. 핑백: happymod.net.br

  20. 핑백: hackear whatsapp gratis

  21. One of the best things about poker is that it’s a game available to everyone. Regardless of your age, profession, or sex, you can learn to play poker and enjoy it online and live without any real obstacles. 샌즈카지노

  22. Poker started as a male-dominated activity, and during the early days of the game, it was played almost exclusively by men. Rare female players to hit the felt were definitely going against the social norms of the period. 라이브카지노

  23. Of course, things have changed quite a bit over the decades, and we’re no longer living in the Wild West era, but the fact remains that the game started off on the wrong foot for the inclusion of women. 샌즈카지노

  24. You’re won’t enjoy yourself, you don’t feel welcome, and at the end of a session, even if you end up winning, you’ll go home with a bitter taste in your mouth. 골프게임

  25. Although there are no real studies to refer to in this regard, it’s safe to assume that many more women play the game online in a safe and protected environment. 비트코인

  26. Also, in most countries, men generally have access to more disposable income, and more importantly, they are probably more keen to put that money on the line in the game. 바카라

  27. Women and men are widely considered to differ in terms of their emotions. Although things are never so clear-cut and this statement is pretty reductive, in general, men will find much more satisfaction in crushing their opponent’s soul than women. 라이브카지노

  28. 핑백: linh tiên song đằng tố

답글 남기기