{"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-08-09T18:49:33","modified_gmt":"2026-08-09T17:49:33","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":"\n<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>\n\n\n\n<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>\n\n\n\n<div class=\"wp-block-group has-background has-global-padding is-layout-constrained wp-container-core-group-is-layout-7441ce17 wp-block-group-is-layout-constrained\" style=\"border-left-color:#ff5c43;border-left-width:4px;border-radius:12px;background-color:#fff5f2;margin-top:32px;margin-bottom:32px;padding-top:24px;padding-right:28px;padding-bottom:24px;padding-left:28px\">\n\n<p style=\"margin-top:0;margin-bottom:16px;font-size:clamp(14.642px, 0.915rem + ((1vw - 3.2px) * 0.575), 22px);font-style:normal;font-weight:600\">Related articles<\/p>\n\n\n\n<ul style=\"margin-top:0;margin-bottom:0;padding-left:20px\" class=\"wp-block-list\">\n<li><a href=\"https:\/\/liora.io\/en\/sql-delete-how-do-i-use-this-query\" rel=\"noopener\" target=\"_blank\">SQL DELETE: How do I use this query?<\/a><\/li><li><a href=\"https:\/\/liora.io\/en\/sql-developer-what-is-it-whats-it-for\" rel=\"noopener\" target=\"_blank\">SQL Developer: What is it? What&#8217;s it for?<\/a><\/li><li><a href=\"https:\/\/liora.io\/en\/sql-tutorial-top-5-most-useful-methods\" rel=\"noopener\" target=\"_blank\">SQL Tutorial: Top 5 Most Useful Methods<\/a><\/li><li><a href=\"https:\/\/liora.io\/en\/sql-joins-everything-you-need-to-know-about\" rel=\"noopener\" target=\"_blank\">SQL joins: everything you need to know about table associations<\/a><\/li><li><a href=\"https:\/\/liora.io\/en\/sql-vs-nosql\" rel=\"noopener\" target=\"_blank\">SQL vs NoSQL: differences, uses, advantages and disadvantages<\/a><\/li>\n<\/ul>\n\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-do-you-group-information-in-a-database\">How do you group information in a database?<\/h2>\n\n\n\n<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>\n\n\n\n<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>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<colgroup>\n<col\/>\n<col\/>\n<col\/>\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><\/figure>\n\n\n\n<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>\n\n\n\n<p>SELECT Department, SUM(Salary)<\/p>\n\n\n\n<p>FROM Staff<\/p>\n\n\n\n<p>GROUP BY Service<\/p>\n\n\n\n<p>ORDER BY Service;<\/p>\n\n\n\n<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>\n\n\n\n<p>The answer would be ,  based on the sample shown above :<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"how-do-i-use-the-having-by-clause\">How do I use the HAVING BY clause?<\/h2>\n\n\n\n<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>\n\n\n\n<p>SELECT Department, AVG(Salary)<\/p>\n\n\n\n<p>FROM Personnel<\/p>\n\n\n\n<p>GROUP BY Service<\/p>\n\n\n\n<p>HAVING AVG(Salary) &gt; 2500<\/p>\n\n\n\n<p>ORDER BY Service;<\/p>\n\n\n\n<p>The result is :<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<colgroup>\n<col\/>\n<col\/>\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<\/table><\/figure>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table>\n<colgroup>\n<col\/>\n<col\/>\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><\/figure>\n\n\n\n<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>\n\n\n\n<div class=\"wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex is-content-justification-center wp-container-core-buttons-is-layout-5ee10de4\" style=\"margin-top:32px;margin-bottom:32px\"><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\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":4,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175676\/revisions"}],"predecessor-version":[{"id":210971,"href":"https:\/\/liora.io\/en\/wp-json\/wp\/v2\/posts\/175676\/revisions\/210971"}],"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}]}}