{"id":3934,"date":"2025-06-06T20:46:46","date_gmt":"2025-06-06T20:46:46","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3934"},"modified":"2026-09-26T13:14:55","modified_gmt":"2026-09-26T07:44:55","slug":"sql-group-by","status":"publish","type":"post","link":"https:\/\/w3buddy.com\/blog\/sql-group-by\/","title":{"rendered":"SQL GROUP BY"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>GROUP BY<\/code> is used to <strong>group rows that share the same values<\/strong> in specified columns and apply aggregate functions to each group.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Basic Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT column1, aggregate_function(column2)\nFROM table_name\nGROUP BY column1;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Example: Group Employees by Department<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT department, COUNT(*) AS total_employees, AVG(salary) AS avg_salary\nFROM employees\nGROUP BY department;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This query groups employees by department and calculates:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Total employees per department<\/li>\n\n\n\n<li>Average salary per department<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Multiple Columns Grouping<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT department, job_title, COUNT(*)\nFROM employees\nGROUP BY department, job_title;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Groups data by both department and job title.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Using GROUP BY with HAVING (Filter Groups)<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>SELECT department, COUNT(*) AS total_employees\nFROM employees\nGROUP BY department\nHAVING COUNT(*) &gt; 5;<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Only shows departments with more than 5 employees.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Concept<\/th><th>Description<\/th><\/tr><\/thead><tbody><tr><td><code>GROUP BY<\/code><\/td><td>Groups rows sharing same column values<\/td><\/tr><tr><td>Works with<\/td><td>Aggregate functions like COUNT, SUM, AVG<\/td><\/tr><tr><td>Can group by<\/td><td>One or more columns<\/td><\/tr><tr><td><code>HAVING<\/code> clause<\/td><td>Filters grouped results (unlike WHERE)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Use <code>GROUP BY<\/code> to summarize data by categories<br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>GROUP BY is used to group rows that share the same values in specified columns and apply aggregate functions to each group. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example: Group Employees by Department This query groups employees by department and calculates: \ud83d\udd39 Multiple Columns Grouping Groups data by both department and job title. \ud83d\udd39 Using GROUP BY [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"googlesitekit_rrm_CAowu461DA:productID":"","footnotes":""},"categories":[1225,979],"tags":[],"class_list":["post-3934","post","type-post","status-publish","format-standard","hentry","category-database","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/3934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/comments?post=3934"}],"version-history":[{"count":2,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/3934\/revisions"}],"predecessor-version":[{"id":4038,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/posts\/3934\/revisions\/4038"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/tags?post=3934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}