{"id":3556,"date":"2023-04-12T15:39:28","date_gmt":"2023-04-12T15:39:28","guid":{"rendered":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/"},"modified":"2026-01-15T18:20:24","modified_gmt":"2026-01-15T18:20:24","slug":"basic-sql-functions-part-3-group-by-statement","status":"publish","type":"post","link":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/","title":{"rendered":"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries?"},"content":{"rendered":"<p>So far, we have gone over the SELECT, FROM and WHERE clauses. These three clauses are used to control <strong>what <\/strong>we are seeing in a view. What if we want to control <strong>how<\/strong> what we are searching for appears in the view, that is where GROUP BY comes in. The GROUP BY clause is used to arrange how records will appear in a view and does this by organizing the data that has an identical value for a particular column in a group, hence the name. If we go back to the customer-transaction table as an example, if a customer made several transactions across the history of the table these transactions may be scattered throughout the view; GROUP BY allows us to package all of these transactions together by customer.<\/p>\n<p>The GROUP BY clause is placed after the WHERE clause, think \u201cLook at these columns, from this table, where the total amount is more than 100, and group it by the customer\u201d. Let\u2019s introduce a new example with a different table.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1043 aligncenter\" src=\"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2020\/06\/P3A-min-276x300.jpg\" alt=\"\" width=\"276\" height=\"300\" \/><\/p>\n<p>You\u2019ll notice that there are two columns in the GROUP BY statement. What this is saying is to group by the ProductID and then the SalesOrderID. This query lets us see a view that shows which order each particular product was assigned to and is grouped by the product.<\/p>\n<p>&nbsp;<\/p>\n<p>Click here for <a href=\"https:\/\/quebit.com\/askquebit\/quebit-products\/basic-sql-functions-part-4-order-by-statement\/\">Part 4: ORDER BY statement<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So far, we have gone over the SELECT, FROM and WHERE clauses. These three clauses are used to control what we are seeing in a view. What if we want to control how what we are searching for appears in the view, that is where GROUP BY comes in. The GROUP BY clause is used&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[27],"tags":[32],"class_list":["post-3556","post","type-post","status-publish","format-standard","hentry","category-quebit-value","tag-sql"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries? - QueBIT<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries? - QueBIT\" \/>\n<meta property=\"og:description\" content=\"So far, we have gone over the SELECT, FROM and WHERE clauses. These three clauses are used to control what we are seeing in a view. What if we want to control how what we are searching for appears in the view, that is where GROUP BY comes in. The GROUP BY clause is used&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/\" \/>\n<meta property=\"og:site_name\" content=\"QueBIT\" \/>\n<meta property=\"article:published_time\" content=\"2023-04-12T15:39:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-01-15T18:20:24+00:00\" \/>\n<meta name=\"author\" content=\"agoddard\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"agoddard\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/\"},\"author\":{\"name\":\"agoddard\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#\\\/schema\\\/person\\\/e52d72da0fd2f5f70d189343fe4f5084\"},\"headline\":\"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries?\",\"datePublished\":\"2023-04-12T15:39:28+00:00\",\"dateModified\":\"2026-01-15T18:20:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/\"},\"wordCount\":243,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/P3A-min-276x300.jpg\",\"keywords\":[\"SQL\"],\"articleSection\":[\"QueBIT Value\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/\",\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/\",\"name\":\"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries? - QueBIT\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/P3A-min-276x300.jpg\",\"datePublished\":\"2023-04-12T15:39:28+00:00\",\"dateModified\":\"2026-01-15T18:20:24+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#\\\/schema\\\/person\\\/e52d72da0fd2f5f70d189343fe4f5084\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/#primaryimage\",\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/P3A-min-276x300.jpg\",\"contentUrl\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/wp-content\\\/uploads\\\/2020\\\/06\\\/P3A-min-276x300.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/basic-sql-functions-part-3-group-by-statement\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#website\",\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/\",\"name\":\"QueBIT\",\"description\":\"QueBIT\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/#\\\/schema\\\/person\\\/e52d72da0fd2f5f70d189343fe4f5084\",\"name\":\"agoddard\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g\",\"caption\":\"agoddard\"},\"sameAs\":[\"https:\\\/\\\/quebit.com\\\/askquebit\"],\"url\":\"https:\\\/\\\/quebit.com\\\/askquebit\\\/author\\\/agoddard\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries? - QueBIT","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/","og_locale":"en_US","og_type":"article","og_title":"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries? - QueBIT","og_description":"So far, we have gone over the SELECT, FROM and WHERE clauses. These three clauses are used to control what we are seeing in a view. What if we want to control how what we are searching for appears in the view, that is where GROUP BY comes in. The GROUP BY clause is used&hellip;","og_url":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/","og_site_name":"QueBIT","article_published_time":"2023-04-12T15:39:28+00:00","article_modified_time":"2026-01-15T18:20:24+00:00","author":"agoddard","twitter_card":"summary_large_image","twitter_misc":{"Written by":"agoddard","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/#article","isPartOf":{"@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/"},"author":{"name":"agoddard","@id":"https:\/\/quebit.com\/askquebit\/#\/schema\/person\/e52d72da0fd2f5f70d189343fe4f5084"},"headline":"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries?","datePublished":"2023-04-12T15:39:28+00:00","dateModified":"2026-01-15T18:20:24+00:00","mainEntityOfPage":{"@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/"},"wordCount":243,"commentCount":0,"image":{"@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/#primaryimage"},"thumbnailUrl":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2020\/06\/P3A-min-276x300.jpg","keywords":["SQL"],"articleSection":["QueBIT Value"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/","url":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/","name":"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries? - QueBIT","isPartOf":{"@id":"https:\/\/quebit.com\/askquebit\/#website"},"primaryImageOfPage":{"@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/#primaryimage"},"image":{"@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/#primaryimage"},"thumbnailUrl":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2020\/06\/P3A-min-276x300.jpg","datePublished":"2023-04-12T15:39:28+00:00","dateModified":"2026-01-15T18:20:24+00:00","author":{"@id":"https:\/\/quebit.com\/askquebit\/#\/schema\/person\/e52d72da0fd2f5f70d189343fe4f5084"},"breadcrumb":{"@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/#primaryimage","url":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2020\/06\/P3A-min-276x300.jpg","contentUrl":"https:\/\/quebit.com\/askquebit\/wp-content\/uploads\/2020\/06\/P3A-min-276x300.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/quebit.com\/askquebit\/basic-sql-functions-part-3-group-by-statement\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/quebit.com\/askquebit\/"},{"@type":"ListItem","position":2,"name":"How Do I Use the SQL GROUP BY Statement to Aggregate Data in Queries?"}]},{"@type":"WebSite","@id":"https:\/\/quebit.com\/askquebit\/#website","url":"https:\/\/quebit.com\/askquebit\/","name":"QueBIT","description":"QueBIT","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/quebit.com\/askquebit\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/quebit.com\/askquebit\/#\/schema\/person\/e52d72da0fd2f5f70d189343fe4f5084","name":"agoddard","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d817b364cff1d66116debde8d1c85e5e76eeece9c5ae731b19276a6040231455?s=96&d=mm&r=g","caption":"agoddard"},"sameAs":["https:\/\/quebit.com\/askquebit"],"url":"https:\/\/quebit.com\/askquebit\/author\/agoddard\/"}]}},"_links":{"self":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts\/3556","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/comments?post=3556"}],"version-history":[{"count":1,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts\/3556\/revisions"}],"predecessor-version":[{"id":4923,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/posts\/3556\/revisions\/4923"}],"wp:attachment":[{"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/media?parent=3556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/categories?post=3556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/quebit.com\/askquebit\/wp-json\/wp\/v2\/tags?post=3556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}