{"id":174,"date":"2016-03-25T07:57:36","date_gmt":"2016-03-25T07:57:36","guid":{"rendered":"https:\/\/sqltutorial.org\/?page_id=174"},"modified":"2025-02-07T21:14:50","modified_gmt":"2025-02-08T04:14:50","slug":"sql-group-by","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-group-by\/","title":{"rendered":"SQL GROUP BY"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the SQL <code>GROUP BY<\/code> clause to group rows based on one or more columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-sql-group-by-clause'>Introduction to SQL GROUP BY clause <a href=\"#introduction-to-sql-group-by-clause\" class=\"anchor\" id=\"introduction-to-sql-group-by-clause\" title=\"Anchor for Introduction to SQL GROUP BY clause\">#<\/a><\/h2>\n\n\n\n<p>The <code>GROUP BY<\/code> is an optional clause of the <code><a href=\"https:\/\/www.sqltutorial.org\/sql-select\/\">SELECT<\/a><\/code> statement. The <code>GROUP BY<\/code> clause allows you to group rows based on values of one or more columns. It returns one row for each group.<\/p>\n\n\n\n<p>The following shows the basic syntax of the <code>GROUP BY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  column1,\n  column2,\n  aggregate_function (column3)\n<span class=\"hljs-keyword\">FROM<\/span>\n  table_name\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  column1,\n  column2;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following picture illustrates shows how the <code>GROUP BY<\/code> clause works:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2022\/05\/sql-group-by.svg\" alt=\"\" class=\"wp-image-2309\"\/><\/figure>\n\n\n\n<p>The table on the left side has two columns <code>id<\/code> and <code>fruit<\/code>. When you apply the <code>GROUP BY<\/code> clause to the <code>fruit<\/code> column, it returns the result set that includes unique values from the <code>fruit<\/code> column:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">SELECT\n  fruit\nFROM\n  sample_table\nGROUP BY\n  fruit;<\/code><\/span><\/pre>\n\n\n<p>In practice, you often use the <code>GROUP BY<\/code> clause with an <a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/\">aggregate function<\/a> such as <a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-min\/\">MIN<\/a>, <a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-max\/\">MAX<\/a>, <a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-avg\/\">AVG<\/a>, <a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-sum\/\">SUM<\/a>, or <a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-count\/\">COUNT<\/a> to calculate a measure that provides the information for each group. <\/p>\n\n\n\n<p>For example, the following illustrates how the <code>GROUP BY<\/code> clause works with the <code>COUNT<\/code> aggregate function:   <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2022\/05\/sql-group-by-with-count.svg\" alt=\"\" class=\"wp-image-2310\"\/><\/figure>\n\n\n\n<p>In this example, we group the rows by the values of the <code>fruit<\/code> column and apply the <code>COUNT<\/code> function to the <code>id<\/code> column. The result set includes the unique values of the fruit columns and the number of the corresponding rows.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">SELECT\n  fruit,\n  COUNT(id)\nFROM\n  sample_table\nGROUP BY\n  fruit;<\/code><\/span><\/pre>\n\n\n<p>The columns that appear in the <code>GROUP BY<\/code> clause are called <em>grouping columns<\/em>. If a grouping column contains <a href=\"https:\/\/www.sqltutorial.org\/sql-is-null\/\">NULL<\/a> values, all NULL values are summarized into a single group because the <code>GROUP BY<\/code> clause considers all NULL values equal.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='sql-group-by-examples'>SQL GROUP BY examples <a href=\"#sql-group-by-examples\" class=\"anchor\" id=\"sql-group-by-examples\" title=\"Anchor for SQL GROUP BY examples\">#<\/a><\/h2>\n\n\n\n<p>We will use the <code>employees<\/code> and <code>departments<\/code> tables in the <a href=\"https:\/\/www.sqltutorial.org\/sql-sample-database\/\">sample database<\/a> to demonstrate how the <code>GROUP BY<\/code> clause works.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"467\" height=\"281\" src=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/emp_dept_tables.png\" alt=\"emp_dept_tables\" class=\"wp-image-230\" srcset=\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/emp_dept_tables.png 467w, https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2016\/03\/emp_dept_tables-300x181.png 300w\" sizes=\"auto, (max-width: 467px) 100vw, 467px\" \/><\/figure>\n\n\n\n<p>The following example uses the <code>GROUP BY<\/code> clause to group the values in <code>department_id<\/code> column of the <code>employees<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  department_id\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  department_id\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  department_id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGRlcGFydG1lbnRfaWQgRlJPTSBlbXBsb3llZXMgR1JPVVAgQlkgZGVwYXJ0bWVudF9pZCBPUkRFUiBCWSBkZXBhcnRtZW50X2lkOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> department_id\n---------------\n             1\n             2\n             3\n             4\n             5\n             6\n             7\n             8\n             9\n            10\n            11<\/code><\/span><\/pre>\n\n\n<p>In this example: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code>SELECT<\/code> clause returns all values from the <code>department_id<\/code> column of <code>employees<\/code> table.<\/li>\n\n\n\n<li>Second, the <code>GROUP BY<\/code> clause groups all values into groups.<\/li>\n<\/ul>\n\n\n\n<p>The <code>department_id<\/code> column of the <code>employees<\/code> table has 40 rows, including duplicate <code>department_id<\/code> values. However, the <code>GROUP BY<\/code> groups these values into groups.<\/p>\n\n\n\n<p>Without an aggregate function, the <code>GROUP BY<\/code> behaves like the <code>DISTINCT<\/code> keyword:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> <span class=\"hljs-keyword\">DISTINCT<\/span>\n  department_id\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  department_id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIERJU1RJTkNUIGRlcGFydG1lbnRfaWQgRlJPTSBlbXBsb3llZXMgT1JERVIgQlkgZGVwYXJ0bWVudF9pZDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The <code>GROUP BY<\/code> clause will be more useful when you use it with an aggregate function. <\/p>\n\n\n\n<p>For example, the following statement uses the <code>GROUP BY<\/code> clause with the <code><a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-count\/\">COUNT<\/a><\/code> function to count the number of employees by department:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  department_id,\n  <span class=\"hljs-keyword\">COUNT<\/span>(employee_id) headcount\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  department_id;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGRlcGFydG1lbnRfaWQsIENPVU5UKGVtcGxveWVlX2lkKSBoZWFkY291bnQgRlJPTSBlbXBsb3llZXMgR1JPVVAgQlkgZGVwYXJ0bWVudF9pZDs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> department_id | headcount\n---------------+-----------\n            11 |         2\n             9 |         3\n             3 |         6\n             5 |         7\n             4 |         1\n            10 |         6\n             6 |         5\n             2 |         2\n             7 |         1\n             1 |         1\n             8 |         6<\/code><\/span><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, the <code>GROUP BY<\/code> clause groups the rows in the <code>employees<\/code> table by department id.<\/li>\n\n\n\n<li>Second, the <code>COUNT(employee_id)<\/code> returns the number of employee id values in each group.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id='sql-group-by-with-inner-join-example'>SQL GROUP BY with INNER JOIN example <a href=\"#sql-group-by-with-inner-join-example\" class=\"anchor\" id=\"sql-group-by-with-inner-join-example\" title=\"Anchor for SQL GROUP BY with INNER JOIN example\">#<\/a><\/h3>\n\n\n\n<p>The following example returns the number of employees by department. And it uses an <code><a href=\"https:\/\/www.sqltutorial.org\/sql-inner-join\/\">INNER JOIN<\/a><\/code> clause to include the department name in the result:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  department_name,\n  <span class=\"hljs-keyword\">COUNT<\/span>(employee_id) headcount\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees e\n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> departments d <span class=\"hljs-keyword\">ON<\/span> d.department_id = e.department_id\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  department_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGRlcGFydG1lbnRfbmFtZSwgQ09VTlQoZW1wbG95ZWVfaWQpIGhlYWRjb3VudCBGUk9NIGVtcGxveWVlcyBlIElOTkVSIEpPSU4gZGVwYXJ0bWVudHMgZCBPTiBkLmRlcGFydG1lbnRfaWQgPSBlLmRlcGFydG1lbnRfaWQgR1JPVVAgQlkgZGVwYXJ0bWVudF9uYW1lOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"> department_name  | headcount\n------------------+-----------\n Accounting       |         <span class=\"hljs-number\">2<\/span>\n Purchasing       |         <span class=\"hljs-number\">6<\/span>\n Marketing        |         <span class=\"hljs-number\">2<\/span>\n Administration   |         <span class=\"hljs-number\">1<\/span>\n Finance          |         <span class=\"hljs-number\">6<\/span>\n Human Resources  |         <span class=\"hljs-number\">1<\/span>\n <span class=\"hljs-keyword\">Public<\/span> Relations |         <span class=\"hljs-number\">1<\/span>\n Executive        |         <span class=\"hljs-number\">3<\/span>\n Shipping         |         <span class=\"hljs-number\">7<\/span>\n Sales            |         <span class=\"hljs-number\">6<\/span>\n IT               |         <span class=\"hljs-number\">5<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='sql-group-by-with-having-example'>SQL GROUP BY with HAVING example <a href=\"#sql-group-by-with-having-example\" class=\"anchor\" id=\"sql-group-by-with-having-example\" title=\"Anchor for SQL GROUP BY with HAVING example\">#<\/a><\/h3>\n\n\n\n<p>The following example uses the <code><a href=\"https:\/\/www.sqltutorial.org\/sql-having\/\">HAVING<\/a><\/code> clause to find departments with headcounts are greater than 5:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  department_name,\n  <span class=\"hljs-keyword\">COUNT<\/span>(employee_id) headcount\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees e\n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> departments d <span class=\"hljs-keyword\">ON<\/span> d.department_id = e.department_id\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  department_name\n<span class=\"hljs-keyword\">HAVING<\/span>\n  <span class=\"hljs-keyword\">COUNT<\/span>(employee_id) &gt; <span class=\"hljs-number\">5<\/span>\n<span class=\"hljs-keyword\">ORDER<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  headcount <span class=\"hljs-keyword\">DESC<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGRlcGFydG1lbnRfbmFtZSwgQ09VTlQoZW1wbG95ZWVfaWQpIGhlYWRjb3VudCBGUk9NIGVtcGxveWVlcyBlIElOTkVSIEpPSU4gZGVwYXJ0bWVudHMgZCBPTiBkLmRlcGFydG1lbnRfaWQgPSBlLmRlcGFydG1lbnRfaWQgR1JPVVAgQlkgZGVwYXJ0bWVudF9uYW1lIEhBVklORyBDT1VOVChlbXBsb3llZV9pZCkgPiA1IE9SREVSIEJZIGhlYWRjb3VudCBERVNDOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> department_name | headcount\n-----------------+-----------\n Shipping        |         7\n Purchasing      |         6\n Finance         |         6\n Sales           |         6<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='sql-group-by-with-min-max-and-avg-example'>SQL GROUP BY with MIN, MAX, and AVG example <a href=\"#sql-group-by-with-min-max-and-avg-example\" class=\"anchor\" id=\"sql-group-by-with-min-max-and-avg-example\" title=\"Anchor for SQL GROUP BY with MIN, MAX, and AVG example\">#<\/a><\/h3>\n\n\n\n<p>The following query returns the <a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-min\/\">minimum<\/a>, <a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-max\/\">maximum<\/a>, and <a href=\"https:\/\/www.sqltutorial.org\/sql-aggregate-functions\/sql-avg\/\">average<\/a> salary of employees in each department.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  department_name,\n  <span class=\"hljs-keyword\">MIN<\/span>(salary) min_salary,\n  <span class=\"hljs-keyword\">MAX<\/span>(salary) max_salary,\n  <span class=\"hljs-keyword\">ROUND<\/span>(<span class=\"hljs-keyword\">AVG<\/span>(salary), <span class=\"hljs-number\">2<\/span>) average_salary\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees e\n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> departments d <span class=\"hljs-keyword\">ON<\/span> d.department_id = e.department_id\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  department_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGRlcGFydG1lbnRfbmFtZSwgTUlOKHNhbGFyeSkgbWluX3NhbGFyeSwgTUFYKHNhbGFyeSkgbWF4X3NhbGFyeSwgUk9VTkQoQVZHKHNhbGFyeSksIDIpIGF2ZXJhZ2Vfc2FsYXJ5IEZST00gZW1wbG95ZWVzIGUgSU5ORVIgSk9JTiBkZXBhcnRtZW50cyBkIE9OIGQuZGVwYXJ0bWVudF9pZCA9IGUuZGVwYXJ0bWVudF9pZCBHUk9VUCBCWSBkZXBhcnRtZW50X25hbWU7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"> department_name  | min_salary | max_salary | average_salary\n------------------+------------+------------+----------------\n Accounting       |    <span class=\"hljs-number\">8300.00<\/span> |   <span class=\"hljs-number\">12000.00<\/span> |       <span class=\"hljs-number\">10150.00<\/span>\n Purchasing       |    <span class=\"hljs-number\">2500.00<\/span> |   <span class=\"hljs-number\">11000.00<\/span> |        <span class=\"hljs-number\">4150.00<\/span>\n Marketing        |    <span class=\"hljs-number\">6000.00<\/span> |   <span class=\"hljs-number\">13000.00<\/span> |        <span class=\"hljs-number\">9500.00<\/span>\n Administration   |    <span class=\"hljs-number\">4400.00<\/span> |    <span class=\"hljs-number\">4400.00<\/span> |        <span class=\"hljs-number\">4400.00<\/span>\n Finance          |    <span class=\"hljs-number\">6900.00<\/span> |   <span class=\"hljs-number\">12000.00<\/span> |        <span class=\"hljs-number\">8600.00<\/span>\n Human Resources  |    <span class=\"hljs-number\">6500.00<\/span> |    <span class=\"hljs-number\">6500.00<\/span> |        <span class=\"hljs-number\">6500.00<\/span>\n <span class=\"hljs-keyword\">Public<\/span> Relations |   <span class=\"hljs-number\">10000.00<\/span> |   <span class=\"hljs-number\">10000.00<\/span> |       <span class=\"hljs-number\">10000.00<\/span>\n Executive        |   <span class=\"hljs-number\">17000.00<\/span> |   <span class=\"hljs-number\">24000.00<\/span> |       <span class=\"hljs-number\">19333.33<\/span>\n Shipping         |    <span class=\"hljs-number\">2700.00<\/span> |    <span class=\"hljs-number\">8200.00<\/span> |        <span class=\"hljs-number\">5885.71<\/span>\n Sales            |    <span class=\"hljs-number\">6200.00<\/span> |   <span class=\"hljs-number\">14000.00<\/span> |        <span class=\"hljs-number\">9616.67<\/span>\n IT               |    <span class=\"hljs-number\">4200.00<\/span> |    <span class=\"hljs-number\">9000.00<\/span> |        <span class=\"hljs-number\">5760.00<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='sql-group-by-with-sum-function-example'>SQL GROUP BY with SUM function example <a href=\"#sql-group-by-with-sum-function-example\" class=\"anchor\" id=\"sql-group-by-with-sum-function-example\" title=\"Anchor for SQL GROUP BY with SUM function example\">#<\/a><\/h3>\n\n\n\n<p>To get the total salary per department, you apply the <a href=\"https:\/\/www.sqltutorial.org\/sql-sum\/\">SUM<\/a> function to the <code>salary<\/code> column and group employees by the <code>department_id<\/code> column as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  department_name,\n  <span class=\"hljs-keyword\">SUM<\/span>(salary) total_salary\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees e\n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> departments d <span class=\"hljs-keyword\">ON<\/span> d.department_id = e.department_id\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  department_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGRlcGFydG1lbnRfbmFtZSwgU1VNKHNhbGFyeSkgdG90YWxfc2FsYXJ5IEZST00gZW1wbG95ZWVzIGUgSU5ORVIgSk9JTiBkZXBhcnRtZW50cyBkIE9OIGQuZGVwYXJ0bWVudF9pZCA9IGUuZGVwYXJ0bWVudF9pZCBHUk9VUCBCWSBkZXBhcnRtZW50X25hbWU7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"> department_name  | total_salary\n------------------+--------------\n Accounting       |     <span class=\"hljs-number\">20300.00<\/span>\n Purchasing       |     <span class=\"hljs-number\">24900.00<\/span>\n Marketing        |     <span class=\"hljs-number\">19000.00<\/span>\n Administration   |      <span class=\"hljs-number\">4400.00<\/span>\n Finance          |     <span class=\"hljs-number\">51600.00<\/span>\n Human Resources  |      <span class=\"hljs-number\">6500.00<\/span>\n <span class=\"hljs-keyword\">Public<\/span> Relations |     <span class=\"hljs-number\">10000.00<\/span>\n Executive        |     <span class=\"hljs-number\">58000.00<\/span>\n Shipping         |     <span class=\"hljs-number\">41200.00<\/span>\n Sales            |     <span class=\"hljs-number\">57700.00<\/span>\n IT               |     <span class=\"hljs-number\">28800.00<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='sql-group-by-multiple-columns'>SQL GROUP BY multiple columns <a href=\"#sql-group-by-multiple-columns\" class=\"anchor\" id=\"sql-group-by-multiple-columns\" title=\"Anchor for SQL GROUP BY multiple columns\">#<\/a><\/h2>\n\n\n\n<p>So far, you have seen that we have grouped all employees by one column. For example, the following clause places all rows with the same values in the <code>department_id<\/code> column in one group.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">GROUP BY department_id<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How about grouping employees by values in both <code>department_id<\/code> and <code>job_id<\/code> columns?<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">GROUP BY department_id, job_id<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This clause will group all employees with the same values in both <code>department_id<\/code> and <code>job_id<\/code> columns in one group.<\/p>\n\n\n\n<p>The following statement groups rows with the same values in both <code>department_id<\/code> and <code>job_id<\/code> columns in the same group then return the rows for each of these groups.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n  department_name,\n  job_title,\n  <span class=\"hljs-keyword\">COUNT<\/span>(employee_id)\n<span class=\"hljs-keyword\">FROM<\/span>\n  employees e\n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> departments d <span class=\"hljs-keyword\">ON<\/span> d.department_id = e.department_id\n  <span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> jobs j <span class=\"hljs-keyword\">ON<\/span> j.job_id = e.job_id\n<span class=\"hljs-keyword\">GROUP<\/span> <span class=\"hljs-keyword\">BY<\/span>\n  department_name,\n  job_title;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIGRlcGFydG1lbnRfbmFtZSwgam9iX3RpdGxlLCBDT1VOVChlbXBsb3llZV9pZCkgRlJPTSBlbXBsb3llZXMgZSBJTk5FUiBKT0lOIGRlcGFydG1lbnRzIGQgT04gZC5kZXBhcnRtZW50X2lkID0gZS5kZXBhcnRtZW50X2lkIElOTkVSIEpPSU4gam9icyBqIE9OIGouam9iX2lkID0gZS5qb2JfaWQgR1JPVVAgQlkgZGVwYXJ0bWVudF9uYW1lLCBqb2JfdGl0bGU7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"> department_name  |            job_title            | count\n------------------+---------------------------------+-------\n Purchasing       | Purchasing Clerk                |     <span class=\"hljs-number\">5<\/span>\n <span class=\"hljs-keyword\">Public<\/span> Relations | <span class=\"hljs-keyword\">Public<\/span> Relations Representative |     <span class=\"hljs-number\">1<\/span>\n Administration   | Administration Assistant        |     <span class=\"hljs-number\">1<\/span>\n Marketing        | Marketing Representative        |     <span class=\"hljs-number\">1<\/span>\n Shipping         | Shipping Clerk                  |     <span class=\"hljs-number\">2<\/span>\n Shipping         | Stock Clerk                     |     <span class=\"hljs-number\">1<\/span>\n Purchasing       | Purchasing Manager              |     <span class=\"hljs-number\">1<\/span>\n IT               | Programmer                      |     <span class=\"hljs-number\">5<\/span>\n Accounting       | Accounting Manager              |     <span class=\"hljs-number\">1<\/span>\n Accounting       | <span class=\"hljs-keyword\">Public<\/span> Accountant               |     <span class=\"hljs-number\">1<\/span>\n Finance          | Accountant                      |     <span class=\"hljs-number\">5<\/span>\n Sales            | Sales Manager                   |     <span class=\"hljs-number\">2<\/span>\n Human Resources  | Human Resources Representative  |     <span class=\"hljs-number\">1<\/span>\n Sales            | Sales Representative            |     <span class=\"hljs-number\">4<\/span>\n Executive        | President                       |     <span class=\"hljs-number\">1<\/span>\n Shipping         | Stock Manager                   |     <span class=\"hljs-number\">4<\/span>\n Marketing        | Marketing Manager               |     <span class=\"hljs-number\">1<\/span>\n Finance          | Finance Manager                 |     <span class=\"hljs-number\">1<\/span>\n Executive        | Administration Vice President   |     <span class=\"hljs-number\">2<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>GROUP BY<\/code> clause groups the rows into groups based on the values of one or more columns.<\/li>\n\n\n\n<li>Use an aggregate function with the <code>GROUP BY<\/code> clause to calculate the summarized value for each group.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='databases'>Databases <a href=\"#databases\" class=\"anchor\" id=\"databases\" title=\"Anchor for Databases\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-group-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL GROUP BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.oracletutorial.com\/oracle-basics\/oracle-group-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">Oracle GROUP BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-group-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQL Server GROUP BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-group-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL GROUP BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-group-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQLite GROUP BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-group-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">Db2 GROUP BY clause<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mariadbtutorial.com\/mariadb-basics\/mariadb-group-by\/\" target=\"_blank\" rel=\"noreferrer noopener\">MariaDB GROUP BY clause<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=group-by\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"174\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-group-by\/\"\n\t\t\t\tdata-post-title=\"SQL GROUP BY\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"174\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-group-by\/\"\n\t\t\t\tdata-post-title=\"SQL GROUP BY\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use the SQL GROUP BY clause to group rows based on one or more columns. Introduction to SQL GROUP BY clause # The GROUP BY is an optional clause of the SELECT statement. The GROUP BY clause allows you to group rows based on values of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":22,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-174","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL GROUP BY<\/title>\n<meta name=\"description\" content=\"This tutorial introduces you SQL GROUP BY that combines rows into groups and apply aggregate function such as AVG, SUM, COUNT, MIN, MAX to each group.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqltutorial.org\/sql-group-by\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL GROUP BY\" \/>\n<meta property=\"og:description\" content=\"This tutorial introduces you SQL GROUP BY that combines rows into groups and apply aggregate function such as AVG, SUM, COUNT, MIN, MAX to each group.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-group-by\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-08T04:14:50+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-group-by\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-group-by\/\",\"name\":\"SQL GROUP BY\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-group-by\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-group-by\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2022\/05\/sql-group-by.svg\",\"datePublished\":\"2016-03-25T07:57:36+00:00\",\"dateModified\":\"2025-02-08T04:14:50+00:00\",\"description\":\"This tutorial introduces you SQL GROUP BY that combines rows into groups and apply aggregate function such as AVG, SUM, COUNT, MIN, MAX to each group.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-group-by\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-group-by\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-group-by\/#primaryimage\",\"url\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2022\/05\/sql-group-by.svg\",\"contentUrl\":\"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2022\/05\/sql-group-by.svg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-group-by\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL GROUP BY\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL GROUP BY","description":"This tutorial introduces you SQL GROUP BY that combines rows into groups and apply aggregate function such as AVG, SUM, COUNT, MIN, MAX to each group.","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:\/\/www.sqltutorial.org\/sql-group-by\/","og_locale":"en_US","og_type":"article","og_title":"SQL GROUP BY","og_description":"This tutorial introduces you SQL GROUP BY that combines rows into groups and apply aggregate function such as AVG, SUM, COUNT, MIN, MAX to each group.","og_url":"https:\/\/www.sqltutorial.org\/sql-group-by\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-02-08T04:14:50+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqltutorial.org\/sql-group-by\/","url":"https:\/\/www.sqltutorial.org\/sql-group-by\/","name":"SQL GROUP BY","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqltutorial.org\/sql-group-by\/#primaryimage"},"image":{"@id":"https:\/\/www.sqltutorial.org\/sql-group-by\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2022\/05\/sql-group-by.svg","datePublished":"2016-03-25T07:57:36+00:00","dateModified":"2025-02-08T04:14:50+00:00","description":"This tutorial introduces you SQL GROUP BY that combines rows into groups and apply aggregate function such as AVG, SUM, COUNT, MIN, MAX to each group.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-group-by\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-group-by\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqltutorial.org\/sql-group-by\/#primaryimage","url":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2022\/05\/sql-group-by.svg","contentUrl":"https:\/\/www.sqltutorial.org\/wp-content\/uploads\/2022\/05\/sql-group-by.svg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-group-by\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL GROUP BY"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=174"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/174\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}