{"id":175676,"date":"2024-01-04T17:33:57","date_gmt":"2024-01-04T16:33:57","guid":{"rendered":"https:\/\/liora.io\/en\/?p=175676"},"modified":"2026-02-06T08:39:45","modified_gmt":"2026-02-06T07:39:45","slug":"sql-group-by-everything-you-need-to-know-about-this-query","status":"publish","type":"post","link":"https:\/\/liora.io\/en\/sql-group-by-everything-you-need-to-know-about-this-query","title":{"rendered":"SQL GROUP BY: Everything you need to know about this query"},"content":{"rendered":"<p><strong>One of the most useful SQL commands for data analysts is GROUP BY. It allows you to easily generate advanced statistics from categories. Suffice to say, it&#8217;s one of the 10 commands you&#8217;ll use the most, so you might as well know what it&#8217;s all about.<\/strong><\/p>\t\t\n\t\t<p><a href=\"https:\/\/liora.io\/en\/sql-queries-the-5-most-important-commands-to-know\">SQL&#8217;s strengths<\/a> are well established. This language offers an unrivalled level of abstraction for analyzing vast volumes of information. Whatever the database software used (<a href=\"https:\/\/liora.io\/en\/demystifying-mysql-a-comprehensive-guide-to-relational-data-managemen\">MySQL<\/a>, Ingres, Oracle Database, <strong>Microsoft SQL Server,<\/strong> PostgreSQL, SQLite, Firebird&#8230;), the data analyst knows he&#8217;ll get the desired result without having to program the precise logic.<\/p><p>&nbsp;<\/p><p>?Related articles:<\/p><table dir=\"ltr\" border=\"1\" cellspacing=\"0\" cellpadding=\"0\" data-sheets-root=\"1\"><colgroup><col width=\"656\"><\/colgroup><tbody><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;SQL DELETE: How do I use this query?&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/sql-delete-how-do-i-use-this-query\"><a href=\"https:\/\/liora.io\/en\/sql-delete-how-do-i-use-this-query\" target=\"_blank\" rel=\"noopener\">SQL DELETE: How do I use this query?<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;SQL Developer: What is it? What's it for?&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/sql-developer-what-is-it-whats-it-for\"><a href=\"https:\/\/liora.io\/en\/sql-developer-what-is-it-whats-it-for\" target=\"_blank\" rel=\"noopener\">SQL Developer: What is it? What&#8217;s it for?<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;SQL Tutorial: Top 5 Most Useful Methods&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/sql-tutorial-top-5-most-useful-methods\"><a href=\"https:\/\/liora.io\/en\/sql-tutorial-top-5-most-useful-methods\" target=\"_blank\" rel=\"noopener\">SQL Tutorial: Top 5 Most Useful Methods<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;SQL joins: everything you need to know about table associations&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/sql-joins-everything-you-need-to-know-about\"><a href=\"https:\/\/liora.io\/en\/sql-joins-everything-you-need-to-know-about\" target=\"_blank\" rel=\"noopener\">SQL joins: everything you need to know about table associations<\/a><\/td><\/tr><tr><td data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;SQL vs NoSQL: differences, uses, advantages and disadvantages&quot;}\" data-sheets-hyperlink=\"https:\/\/liora.io\/en\/sql-vs-nosql\"><a href=\"https:\/\/liora.io\/en\/sql-vs-nosql\" target=\"_blank\" rel=\"noopener\">SQL vs NoSQL: differences, uses, advantages and disadvantages<\/a><\/td><\/tr><\/tbody><\/table>\t\t\n\t\t\t<h3>How do you group information in a database?<\/h3>\t\t\n\t\t<p>If there&#8217;s one particularly important command you&#8217;ll appreciate, it&#8217;s <strong>SQL GROUP BY<\/strong>. It groups information from a database according to a particular column, from which it is possible to obtain statistical information: sum, maximum, minimum, average, etc.<\/p><p>To better understand this concept, it&#8217;s best to start with an example. Here&#8217;s an extract from a table called Staff<\/p>\t\t\n\t\t\t<style type=\"text\/css\">\n  .tg  {border-collapse:collapse;border-spacing:0;}\n  .tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n    overflow:hidden;padding:10px 5px;word-break:normal;}\n  .tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n    font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n  .tg .tg-8a1x{background-color:#6665cd;color:#efefef;font-family:Tahoma, Geneva, sans-serif !important;font-size:20px;\n    font-weight:bold;text-align:center;vertical-align:top}\n  .tg .tg-r3ey{font-family:Tahoma, Geneva, sans-serif !important;font-size:100%;text-align:center;vertical-align:top}\n<\/style>\n<table style=\"undefined;table-layout: fixed; width: 300px\">\n  <colgroup>\n    <col style=\"width: 100px\">\n    <col style=\"width: 100px\">\n    <col style=\"width: 100px\">\n  <\/colgroup>\n  <thead>\n    <tr>\n      <th>Name<\/th>\n      <th>Department<\/th>\n      <th>Salary<\/th>\n    <\/tr>\n  <\/thead>\n  <tbody>\n    <tr>\n      <td>Paul<\/td>\n      <td>Finance<\/td>\n      <td>$3,000<\/td>\n    <\/tr>\n    <tr>\n      <td>Ray<\/td>\n      <td>Sales<\/td>\n      <td>$2,500<\/td>\n    <\/tr>\n    <tr>\n      <td>Julia<\/td>\n      <td>Finance<\/td>\n      <td>$3,200<\/td>\n    <\/tr>\n    <tr>\n      <td>Dan<\/td>\n      <td>Marketing<\/td>\n      <td>$4,300<\/td>\n    <\/tr>\n    <tr>\n      <td>Josie<\/td>\n      <td>Sales<\/td>\n      <td>$2,200<\/td>\n    <\/tr>\n    <tr>\n      <td>Donna<\/td>\n      <td>Finance<\/td>\n      <td>$2,700<\/td>\n    <\/tr>\n  <\/tbody>\n<\/table>\n\t\t<p>If we wanted to obtain the sum of <strong>salaries by department,<\/strong> we&#8217;d simply use a sequence like the following:<\/p><p>SELECT Department, SUM(Salary)<\/p><p>FROM Staff<\/p><p>GROUP BY Service<\/p><p>ORDER BY Service;<\/p><p>In this example, we group the table according to the Service column and ask for the sum of salaries for each of them.<\/p><p>The answer would be &#8211; based on the sample shown above :<\/p>\t\t\n\t\t\t<style type=\"text\/css\">\n.tg {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px ;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px ;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-r3ey{font-family:Tahoma, Geneva, sans-serif !important;font-size:100%;text-align:center;vertical-align:top}\n<\/style>\n<h3>How do I use the HAVING BY clause?<\/h3><p><strong>HAVING BY<\/strong> completes the <strong>SQL GROUP BY<\/strong> command by opening up the possibility of specifying a condition. In the example below, we&#8217;d like to display the average salary (the AVG function), but only if this average is greater than 2500:<\/p><p>SELECT Department, AVG(Salary)<\/p><p>FROM Personnel<\/p><p>GROUP BY Service<\/p><p>HAVING AVG(Salary) &gt; 2500<\/p><p>ORDER BY Service;<\/p><p>The result is :<\/p><table style=\"undefined;table-layout : fixed ; width : 200px\">\n<colgroup>\n<col style=\"width : 100px\">\n<col style=\"width : 100px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>Finances<\/th>\n    <th>8 900<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>Marketing<\/td>\n    <td>4 300<\/td>\n  <\/tr>\n  <tr>\n    <td>Sales<\/td>\n    <td>4 700<\/td>\n  <\/tr>\n<\/tbody>\n\t\t\n\t\t\t\t\t\n\t\t\t\t\n\t\t\t<style type=\"text\/css\">\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;\n  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}\n.tg .tg-r3ey{font-family:Tahoma, Geneva, sans-serif !important;font-size:100%;text-align:center;vertical-align:top}\n<\/style>\n<\/table><table style=\"undefined;table-layout: fixed; width: 200px\">\n<colgroup>\n<col style=\"width: 100px\">\n<col style=\"width: 100px\">\n<\/colgroup>\n<thead>\n  <tr>\n    <th>Finances<\/th>\n    <th>2 967<\/th>\n  <\/tr>\n<\/thead>\n<tbody>\n  <tr>\n    <td>Marketing<\/td>\n    <td>4 300<\/td>\n  <\/tr>\n<\/tbody>\n<\/table>\t\t\n\t\t<p>And that&#8217;s it! It&#8217;s that simple. The ease with which such operations can be implemented is a testament to the qualities of the <a href=\"https:\/\/liora.io\/en\/sql-tutorial-top-5-most-useful-methods\">SQL language.<\/a><\/p>\t\t\n\t\t\t\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center\"><div class=\"wp-block-button \"><a class=\"wp-block-button__link wp-element-button \" href=\"\/en\/courses\/data-ai\/data-scientist\">Training in SQL for data<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>One of the most useful SQL commands for data analysts is GROUP BY. It allows you to easily generate advanced statistics from categories. Suffice to say, it&#8217;s one of the 10 commands you&#8217;ll use the most, so you might as well know what it&#8217;s all about. SQL&#8217;s strengths are well established. This language offers an [&hellip;]<\/p>\n","protected":false},"author":76,"featured_media":175678,"comment_status":"open","ping_status":"open","sticky":false,"template":"elementor_theme","format":"standard","meta":{"_acf_changed":false,"editor_notices":[],"footnotes":""},"categories":[2433],"class_list":["post-175676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai"],"acf":[],"_links":{"self":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/users\/76"}],"replies":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/comments?post=175676"}],"version-history":[{"count":1,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175676\/revisions"}],"predecessor-version":[{"id":206155,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175676\/revisions\/206155"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media\/175678"}],"wp:attachment":[{"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/media?parent=175676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/categories?post=175676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}