{"id":72,"date":"2026-03-06T06:19:41","date_gmt":"2026-03-06T06:19:41","guid":{"rendered":"https:\/\/gigz.pk\/sql\/?post_type=lesson&#038;p=72"},"modified":"2026-03-16T18:49:39","modified_gmt":"2026-03-16T18:49:39","slug":"group-by","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/sql\/lesson\/group-by\/","title":{"rendered":"\u00a0GROUP BY"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Introduction<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>GROUP BY<\/code> statement in SQL is used to <strong>arrange identical data into groups<\/strong>. It is usually combined with aggregate functions like <code>COUNT<\/code>, <code>SUM<\/code>, <code>AVG<\/code>, <code>MAX<\/code>, or <code>MIN<\/code> to perform calculations on each group.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Syntax<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT column1, aggregate_function(column2)<br>FROM table_name<br>WHERE condition<br>GROUP BY column1;<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>column1<\/strong> \u2013 the column you want to group by<\/li>\n\n\n\n<li><strong>aggregate_function(column2)<\/strong> \u2013 function to calculate on grouped data<\/li>\n\n\n\n<li><strong>table_name<\/strong> \u2013 the table containing your data<\/li>\n\n\n\n<li><strong>WHERE condition<\/strong> \u2013 optional filter before grouping<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example 1: Counting Records<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we have a table <code>Employees<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>EmployeeID<\/th><th>Department<\/th><th>Salary<\/th><\/tr><\/thead><tbody><tr><td>1<\/td><td>HR<\/td><td>5000<\/td><\/tr><tr><td>2<\/td><td>IT<\/td><td>6000<\/td><\/tr><tr><td>3<\/td><td>HR<\/td><td>5500<\/td><\/tr><tr><td>4<\/td><td>IT<\/td><td>6200<\/td><\/tr><tr><td>5<\/td><td>Sales<\/td><td>4500<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">We want to <strong>count employees in each department<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT Department, COUNT(EmployeeID) AS TotalEmployees<br>FROM Employees<br>GROUP BY Department;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Department<\/th><th>TotalEmployees<\/th><\/tr><\/thead><tbody><tr><td>HR<\/td><td>2<\/td><\/tr><tr><td>IT<\/td><td>2<\/td><\/tr><tr><td>Sales<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example 2: Sum of Salaries by Department<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To find <strong>total salary for each department<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT Department, SUM(Salary) AS TotalSalary<br>FROM Employees<br>GROUP BY Department;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Department<\/th><th>TotalSalary<\/th><\/tr><\/thead><tbody><tr><td>HR<\/td><td>10500<\/td><\/tr><tr><td>IT<\/td><td>12200<\/td><\/tr><tr><td>Sales<\/td><td>4500<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example 3: Average Salary by Department<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To find <strong>average salary for each department<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">SELECT Department, AVG(Salary) AS AverageSalary<br>FROM Employees<br>GROUP BY Department;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Result:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Department<\/th><th>AverageSalary<\/th><\/tr><\/thead><tbody><tr><td>HR<\/td><td>5250<\/td><\/tr><tr><td>IT<\/td><td>6100<\/td><\/tr><tr><td>Sales<\/td><td>4500<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Key Points<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>GROUP BY<\/code> is always used <strong>after WHERE but before ORDER BY<\/strong><\/li>\n\n\n\n<li>All columns in <code>SELECT<\/code> that are <strong>not aggregated<\/strong> must be included in <code>GROUP BY<\/code><\/li>\n\n\n\n<li><code>GROUP BY<\/code> helps <strong>summarize data<\/strong> for reports and analysis<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Practice Exercise<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Using the <code>Employees<\/code> table, write SQL queries to:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Find the <strong>highest salary<\/strong> in each department<\/li>\n\n\n\n<li>Count how many employees earn <strong>more than 5000<\/strong> in each department<\/li>\n\n\n\n<li>Find the <strong>average salary<\/strong> of IT department<\/li>\n<\/ol>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/sql\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">SQL Foundations Program (SQL-101) > Working with Functions > GROUP BY<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773565719441\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":20,"template":"","class_list":["post-72","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u00a0GROUP BY - SQL Learning Hub<\/title>\n<meta name=\"description\" content=\"Learn SQL GROUP BY with simple examples. Understand syntax, aggregate functions, and data grouping for reports and analysis in SQL.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/sql\/lesson\/group-by\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0GROUP BY - SQL Learning Hub\" \/>\n<meta property=\"og:description\" content=\"Learn SQL GROUP BY with simple examples. Understand syntax, aggregate functions, and data grouping for reports and analysis in SQL.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/sql\/lesson\/group-by\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Learning Hub\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T18:49:39+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=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/group-by\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/group-by\\\/\",\"name\":\"\u00a0GROUP BY - SQL Learning Hub\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\"},\"datePublished\":\"2026-03-06T06:19:41+00:00\",\"dateModified\":\"2026-03-16T18:49:39+00:00\",\"description\":\"Learn SQL GROUP BY with simple examples. Understand syntax, aggregate functions, and data grouping for reports and analysis in SQL.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/group-by\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/group-by\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/group-by\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Foundations Program (SQL-101) > Working with Functions > GROUP BY\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\",\"name\":\"SQL Learning Hub\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/?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":"\u00a0GROUP BY - SQL Learning Hub","description":"Learn SQL GROUP BY with simple examples. Understand syntax, aggregate functions, and data grouping for reports and analysis in SQL.","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:\/\/gigz.pk\/sql\/lesson\/group-by\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0GROUP BY - SQL Learning Hub","og_description":"Learn SQL GROUP BY with simple examples. Understand syntax, aggregate functions, and data grouping for reports and analysis in SQL.","og_url":"https:\/\/gigz.pk\/sql\/lesson\/group-by\/","og_site_name":"SQL Learning Hub","article_modified_time":"2026-03-16T18:49:39+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/sql\/lesson\/group-by\/","url":"https:\/\/gigz.pk\/sql\/lesson\/group-by\/","name":"\u00a0GROUP BY - SQL Learning Hub","isPartOf":{"@id":"https:\/\/gigz.pk\/sql\/#website"},"datePublished":"2026-03-06T06:19:41+00:00","dateModified":"2026-03-16T18:49:39+00:00","description":"Learn SQL GROUP BY with simple examples. Understand syntax, aggregate functions, and data grouping for reports and analysis in SQL.","breadcrumb":{"@id":"https:\/\/gigz.pk\/sql\/lesson\/group-by\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/sql\/lesson\/group-by\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/sql\/lesson\/group-by\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/sql\/"},{"@type":"ListItem","position":2,"name":"SQL Foundations Program (SQL-101) > Working with Functions > GROUP BY"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/sql\/#website","url":"https:\/\/gigz.pk\/sql\/","name":"SQL Learning Hub","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/sql\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson\/72","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/media?parent=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}