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

Comments (54)

  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. Pingback: b2b

  6. Pingback: situs bola

  7. 응답

    You complete a number of earn points near. I did a explore resting on the topic and found mainly people will support with your website.

  8. Pingback: replica rolex sea dweller

  9. Pingback: 홀덤사이트

  10. Pingback: Köp Lyrica online

  11. Pingback: health insurance

  12. Pingback: Sexual Revolution (Roger Waters song)

  13. Pingback: UNICORN

  14. Pingback: fake rolex

  15. Pingback: buy morphine online

  16. Pingback: World Market URL

  17. Pingback: Köp smärtpiller i Sverige

  18. Pingback: cabins for sale in payson az

  19. Pingback: sagaming

  20. Pingback: happymod.net.br

  21. Pingback: hackear whatsapp gratis

  22. 샌즈카지노

    응답

    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. 샌즈카지노

  23. 정선카지노

    응답

    Given the fact there is nothing in the game rules to discriminate against female players, it’s only natural to wonder why there aren’t more women in poker? 정선카지노

  24. 라이브카지노

    응답

    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. 라이브카지노

  25. 샌즈카지노

    응답

    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. 샌즈카지노

  26. 골프게임

    응답

    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. 골프게임

  27. 비트코인

    응답

    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. 비트코인

  28. 바카라

    응답

    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. 바카라

  29. 라이브카지노

    응답

    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. 라이브카지노

  30. Pingback: linh tiên song đằng tố

  31. Pingback: best site to buy dumps

  32. Pingback: microsoft exchange online plan 1

  33. Pingback: bulut depolama,bulut,yedekleme,ücretsiz bulut hizmeti,en iyi bulut şirketi,en iyi bulut firması,bulut depolama fiyatı,en ucuz bulut depolama,en iyi bulut depolama hangisi,en iyi bulut depolama,bulut arşivleme sistemi,online yedekleme,bulut yedekleme

  34. Pingback: exchange bulut mail

  35. Pingback: dell sunucu teknik servis

  36. Pingback: kurumsal sunucu servisi

  37. Pingback: it danışmanlık ücretleri

  38. Pingback: Hp Sunucu destek

  39. Pingback: microsoft exchange hosting fiyat

  40. Pingback: world market link

  41. Pingback: W88.tips

  42. 응답

    viagra free sample
    cvs pharmacy alcohol and viagra viagra shelf life canadian pharmacy reviews [url=https://cats-ads.com/user/profile/51479]www.canadian pharmacy[/url] cialis canada canada drugs online canada medications price for cialis in canada [url=https://musescore.com/user/39555010]best price cialis[/url]
    cialis pharmacy rx one cvs pharmacy costco pharmacy viagra canada online [url=https://bit.ly/37lLyf0+]my canadian pharcharmy online[/url]

  43. 응답

    buy cialis online canada pharmacy
    alcohol and viagra online cheap viagra viagra online canada pharmacy mail order viagra [url=http://allfreead.com/index.php?page=user&action=pub_profile&id=342288]cvs pharmacy[/url] buy viagra online uk over the counter viagra alternative viagra overnight shipping viagra-canada [url=https://saveyoursite.win/story.php?title=putting-a-stop-to-the-panic-handling-and-also-treating-anxiety-attack#discuss]canadian pharmacies viagra[/url]
    buy viagra online without script canada med canadian cialis online pharmacy cialis mexico pharmacy [url=https://ioby.org/user/birthanger79]mail order prescriptions from canada[/url]

  44. 응답

    online prescription
    canada pharmacy viagra canadian prescription drugstore viagra free trial generic viagra cheap [url=http://abroadsanjal.com/index.php?qa=user&qa_1=notifydetail37]viagra cost[/url] canada for viagra online drugs order cialis in canada real viagra [url=http://bbs.yx.dodjoy.com/home.php?mod=space&uid=412764]cheapest viagra prices[/url]
    canadian pharmacies without a prescription herbal viagra reviews buy generic cialis canadian pharmacy viagra online sales [url=https://myemotion.faith/wiki/Take_advantage_of_Your_Charm_Routine_With_These_Tips]alternatives to viagra[/url]

  45. 응답

    pills from canada
    canadianpharmacymeds.com medications.com how to take viagra ?viagra canada [url=https://git.skewed.de/atmedward90]viagra mail order[/url] cialis pharmacy tadalafil in canada northwestpharmacy.com canada no prescription pharmacy [url=https://www.hostingendomeinen.nl/support/profile.php?section=personal&id=460166]canadian online drugstore[/url]
    free viagra sample pack canadian pharmacy viagra online overseas pharmacy viagra pill [url=https://easybookmark.win/story.php?title=exactly-how-to-order-as-well-as-usage-your-generic-cialis-online-without-prescription-7#discuss]viagra from canadian pharmacy[/url]

  46. 응답

    viagra from india
    viagra online canada pharmacy cialis in canadian pharmacies viagra without a doctor prescription best price on cialis [url=http://ffskybbsjp.azurewebsites.net/home.php?mod=space&uid=6830751]viagra safe[/url] meds canada cheap viagra uk canada medication canadian prescription [url=http://www.atlasroleplay.com/forum/profile/birthgarden38]pharmacy rx one 60 mg cialis[/url]
    northwestern pharmacy canada canadian-drugstore viagra substitute generic viagra india [url=https://www.sitiosperuanos.com/author/ringchange46/]www.canadianpharmacymeds.com[/url]

Leave a comment

이메일 주소는 공개되지 않습니다.