{"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":"2025-06-07T09:57:51","modified_gmt":"2025-06-07T09:57:51","slug":"sql-group-by","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/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},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3934","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3934","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"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"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}