{"id":493,"date":"2026-05-17T07:02:30","date_gmt":"2026-05-16T23:02:30","guid":{"rendered":"https:\/\/junai.ai\/blog\/pg-group-by-8\/"},"modified":"2026-05-17T07:02:30","modified_gmt":"2026-05-16T23:02:30","slug":"pg-group-by-8","status":"publish","type":"post","link":"https:\/\/junai.ai\/blog\/pg-group-by-8\/","title":{"rendered":"GROUP BY \uc640 \uc9d1\uacc4 \ud568\uc218\u00b7HAVING (\uae30\ucd08 8\ud3b8)"},"content":{"rendered":"\n<!-- WordPress REST API \ubc1c\ud589\uc6a9 HTML (\uc790\ub3d9 \uc0dd\uc131) -->\n<!-- WP-FEATURED-MEDIA-ID: 277 -->\n<div style=\"max-width:800px;margin:0 auto;\">\n<style>\n:root{--color-primary:#336791;--color-accent:#60a5fa;--color-bg:#fafafa;--color-bg-card:#ffffff;--color-text:#1e293b;--color-text-muted:#64748b;--hero-start:#0f172a;--hero-end:#336791;--font-body:-apple-system,BlinkMacSystemFont,'Apple SD Gothic Neo','Noto Sans KR',sans-serif;--size-body:17px;--line-height:1.75;}\n*{box-sizing:border-box;}\n.container{max-width:760px;margin:0 auto;padding:0 22px 80px;}\n.hero{background:linear-gradient(135deg,var(--hero-start) 0%,var(--hero-end) 100%);color:#fff;padding:72px 22px 56px;text-align:center;}\n.hero .badge{display:inline-block;background:rgba(96,165,250,0.18);color:var(--color-accent);padding:6px 14px;border-radius:999px;font-size:13px;font-weight:600;letter-spacing:0.5px;margin-bottom:18px;}\n.hero h1{margin:0 0 18px;font-size:36px;line-height:1.3;letter-spacing:-0.3px;}\n.hero p.sub{margin:0 auto;max-width:580px;font-size:17px;color:#bfdbfe;}\n.hero img{width:100%;max-width:720px;height:auto;margin:36px auto 0;display:block;border-radius:10px;box-shadow:0 8px 32px rgba(0,0,0,0.3);}\n.meta{display:flex;gap:14px;justify-content:center;margin-top:20px;font-size:13px;color:#93c5fd;flex-wrap:wrap;}\n.meta span::before{content:\"\u00b7\";margin-right:14px;color:#1e3a8a;}\n.meta span:first-child::before{content:\"\";margin:0;}\narticle{background:var(--color-bg-card);margin-top:-36px;padding:44px 28px;border-radius:14px;box-shadow:0 2px 18px rgba(0,0,0,0.06);}\narticle p{margin:0 0 18px;}\nh2{font-size:28px;line-height:1.35;letter-spacing:-0.3px;margin:48px 0 18px;padding-bottom:10px;border-bottom:2px solid var(--color-primary);}\nh2:first-of-type{margin-top:8px;}\nh3{font-size:21px;line-height:1.4;margin:30px 0 12px;color:var(--color-primary);}\ncode{background:#f1f5f9;color:#0f172a;padding:2px 6px;border-radius:4px;font-family:'SFMono-Regular',Menlo,Consolas,monospace;font-size:0.92em;}\npre{background:#0f172a;color:#e2e8f0;padding:18px 20px;border-radius:10px;overflow-x:auto;font-size:14.5px;line-height:1.65;margin:18px 0;}\npre code{background:transparent;color:inherit;padding:0;}\nul,ol{margin:0 0 18px;padding-left:24px;}\nli{margin-bottom:8px;}\nstrong{color:#0f172a;}\n.databox{background:#eff6ff;border-left:4px solid var(--color-primary);padding:18px 20px;border-radius:6px;margin:22px 0;}\n.databox p{margin:0 0 8px;}\n.databox p:last-child{margin:0;}\n.databox strong{color:var(--color-primary);}\n.warnbox{background:linear-gradient(135deg,#fef3c7 0%,#fde68a 100%);border-left:4px solid #d97706;padding:18px 20px;border-radius:6px;margin:22px 0;}\n.warnbox strong{color:#92400e;}\n.tablewrap{overflow-x:auto;-webkit-overflow-scrolling:touch;margin:22px 0;}\ntable{width:100%;border-collapse:collapse;font-size:15px;background:var(--color-bg-card);}\nth,td{padding:11px 12px;text-align:left;border-bottom:1px solid #e2e8f0;vertical-align:top;}\nth{background:#f1f5f9;font-weight:700;color:#0f172a;}\ntd:first-child,th:first-child{font-weight:700;}\n.cta{background:linear-gradient(135deg,#336791 0%,#60a5fa 100%);color:#fff;padding:30px 24px;border-radius:12px;margin-top:44px;text-align:center;}\n.cta h3{color:#fff;margin:0 0 10px;}\n.cta p{margin:0;color:#dbeafe;}\n.series-nav{background:#eff6ff;padding:18px 22px;border-radius:10px;margin-top:24px;font-size:14.5px;color:var(--color-text-muted);}\n.series-nav strong{color:var(--color-primary);}\n@media (max-width:480px){.hero{padding:52px 18px 44px;}.hero h1{font-size:26px;}.hero p.sub{font-size:15px;}article{padding:28px 18px;border-radius:10px;}h2{font-size:22px;}h3{font-size:18px;}body{font-size:16px;}pre{font-size:13px;padding:14px 16px;}}\n@media (max-width:560px){.tablewrap table,.tablewrap thead,.tablewrap tbody,.tablewrap tr,.tablewrap th,.tablewrap td{display:block;width:auto;}.tablewrap thead{display:none;}.tablewrap tr{margin:0 0 14px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden;}.tablewrap td{border:none;border-bottom:1px solid #f1f5f9;padding:9px 14px;}.tablewrap td:first-child{background:#eff6ff;font-weight:800;font-size:15.5px;}.tablewrap td:last-child{border-bottom:none;}.tablewrap td[data-label]::before{content:attr(data-label) \" \u2014 \";font-weight:700;color:var(--color-primary);}}\n<\/style>\n<header class=\"hero\">\n  <span class=\"badge\">PostgreSQL \uad50\uc7ac \u00b7 8\ud3b8 \/ 24\ud3b8<\/span>\n  <h1>GROUP BY \uc640 \uc9d1\uacc4 \u2014 COUNT\u00b7SUM\u00b7AVG\u00b7HAVING<\/h1>\n  <p class=\"sub\">&#8220;\uadf8\ub8f9\ubcc4 \ud1b5\uacc4&#8221; \uc758 \uc815\uc11d\uacfc HAVING \uc758 \uc815\ud655\ud55c \uc790\ub9ac.<\/p>\n  <div class=\"meta\"><span>SQL \uae30\ucd08<\/span><span>\uc77d\ub294 \uc2dc\uac04 7\ubd84<\/span><span>2026-05-17<\/span><\/div>\n  <img decoding=\"async\" src=\"https:\/\/junai.ai\/blog\/wp-content\/uploads\/2026\/05\/hero-57.jpg\" alt=\"GROUP BY \uac00 \ub370\uc774\ud130\ub97c \uadf8\ub8f9\ubcc4\ub85c \ubb36\uace0 \uc9d1\uacc4 \ud568\uc218\uac00 \ud569\uce58\ub294 \ub3c4\uc2dd\">\n<\/header>\n\n<div class=\"container\">\n<article>\n\n<p>&#8220;\uce74\ud14c\uace0\ub9ac\ubcc4 \uc0c1\ud488 \uc218&#8221; &#8220;\uc6d4\ubcc4 \ub9e4\ucd9c&#8221; &#8220;\uc0ac\uc6a9\uc790\ubcc4 \ucd5c\uadfc \uc8fc\ubb38 \ud69f\uc218&#8221; \u2014 \uac70\uc758 \ubaa8\ub4e0 \ub300\uc2dc\ubcf4\ub4dc \ucffc\ub9ac\uac00 <strong>GROUP BY + \uc9d1\uacc4 \ud568\uc218<\/strong>\uc785\ub2c8\ub2e4. 8\ud3b8\uc740 5\uac1c \uc9d1\uacc4 \ud568\uc218, \uc5ec\ub7ec \uceec\ub7fc \uadf8\ub8f9\ud654, \uadf8\ub9ac\uace0 \uc790\uc8fc \ud63c\ub3d9\ub418\ub294 <strong>HAVING vs WHERE<\/strong> \uc758 \uc815\ud655\ud55c \uc790\ub9ac\ub97c \uc815\ub9ac\ud569\ub2c8\ub2e4.<\/p>\n\n<h2>5\uac00\uc9c0 \uc9d1\uacc4 \ud568\uc218<\/h2>\n\n<div class=\"tablewrap\">\n<table>\n  <thead>\n    <tr><th>\ud568\uc218<\/th><th>\uc758\ubbf8<\/th><th>NULL \ucc98\ub9ac<\/th><\/tr>\n  <\/thead>\n  <tbody>\n    <tr><td data-label=\"\ud568\uc218\">COUNT(*)<\/td><td data-label=\"\uc758\ubbf8\">\uc804\uccb4 \ud589 \uc218<\/td><td data-label=\"NULL\">NULL \ud3ec\ud568<\/td><\/tr>\n    <tr><td data-label=\"\ud568\uc218\">COUNT(col)<\/td><td data-label=\"\uc758\ubbf8\">col \uc774 NULL \uc544\ub2cc \ud589 \uc218<\/td><td data-label=\"NULL\">NULL \uc81c\uc678<\/td><\/tr>\n    <tr><td data-label=\"\ud568\uc218\">COUNT(DISTINCT col)<\/td><td data-label=\"\uc758\ubbf8\">\uace0\uc720\uac12 \uac1c\uc218<\/td><td data-label=\"NULL\">NULL \uc81c\uc678<\/td><\/tr>\n    <tr><td data-label=\"\ud568\uc218\">SUM(col)<\/td><td data-label=\"\uc758\ubbf8\">\ud569\uacc4<\/td><td data-label=\"NULL\">NULL \ubb34\uc2dc<\/td><\/tr>\n    <tr><td data-label=\"\ud568\uc218\">AVG(col)<\/td><td data-label=\"\uc758\ubbf8\">\ud3c9\uade0<\/td><td data-label=\"NULL\">NULL \ubb34\uc2dc (\ubd84\ubaa8\uc5d0\uc11c\ub3c4 \uc81c\uc678)<\/td><\/tr>\n    <tr><td data-label=\"\ud568\uc218\">MIN(col) \/ MAX(col)<\/td><td data-label=\"\uc758\ubbf8\">\ucd5c\uc18c\u00b7\ucd5c\ub300<\/td><td data-label=\"NULL\">NULL \ubb34\uc2dc<\/td><\/tr>\n  <\/tbody>\n<\/table>\n<\/div>\n\n<pre><code>SELECT COUNT(*)            AS total_orders,\n       COUNT(coupon_code)  AS used_coupon,\n       SUM(total)          AS revenue,\n       AVG(total)::numeric(10,2) AS avg_ticket,\n       MIN(created_at)     AS first_order,\n       MAX(created_at)     AS last_order\nFROM   orders;<\/code><\/pre>\n\n<div class=\"warnbox\">\n  <p><strong>NULL + AVG \uc758 \ubbf8\ubb18\ud568.<\/strong> <code>AVG(rating)<\/code> \uc740 NULL \ud589\uc744 \ubd84\uc790\u00b7\ubd84\ubaa8 \ubaa8\ub450\uc5d0\uc11c \uc81c\uc678\ud569\ub2c8\ub2e4. \ub9cc\uc57d &#8220;\ud3c9\uac00 \uc548 \ud55c \uc0ac\ub78c\uc740 0\uc810\uc73c\ub85c \uce58\uace0 \ud3c9\uade0&#8221; \uc744 \uc6d0\ud55c\ub2e4\uba74 <code>AVG(COALESCE(rating, 0))<\/code> \ucc98\ub7fc \uba85\uc2dc. \ubd84\uae30\u00b7\ubd84\uc11d\uc5d0\uc11c \uc790\uc8fc \ube60\uc9c0\ub294 \ud568\uc815.<\/p>\n<\/div>\n\n<h2>GROUP BY \u2014 \uadf8\ub8f9\ubcc4 \uc9d1\uacc4<\/h2>\n\n<pre><code>-- \uce74\ud14c\uace0\ub9ac\ubcc4 \uc0c1\ud488 \uc218\nSELECT category_id, COUNT(*) AS n\nFROM   products\nGROUP BY category_id;\n\n-- \uc5ec\ub7ec \uceec\ub7fc (cross-tab \uc2dc\uc791)\nSELECT region, status, COUNT(*) AS n, SUM(total) AS rev\nFROM   orders\nGROUP BY region, status;\n\n-- \ud45c\ud604\uc2dd \uadf8\ub8f9\ud654\nSELECT date_trunc('month', created_at) AS month, COUNT(*) AS n\nFROM   orders\nGROUP BY date_trunc('month', created_at)\nORDER BY month;<\/code><\/pre>\n\n<div class=\"databox\">\n  <p><strong>SQL \uc758 \ud669\uae08 \uaddc\uce59.<\/strong> SELECT \uc808\uc5d0 \ub4f1\uc7a5\ud558\ub294 \uceec\ub7fc\uc740 <strong>\u2460 GROUP BY \uc5d0 \uc788\uac70\ub098, \u2461 \uc9d1\uacc4 \ud568\uc218 \uc548\uc5d0 \uc788\uac70\ub098<\/strong> \ub458 \uc911 \ud558\ub098\uc5ec\uc57c \ud569\ub2c8\ub2e4. PostgreSQL \uc740 \uc774 \uaddc\uce59\uc744 \uc5c4\uaca9\ud788 \uc9c0\ud0b5\ub2c8\ub2e4(MySQL \uc61b \ubc84\uc804\uc740 \ub290\uc2a8\ud574\uc11c \ud5f7\uac08\ub9ac\ub294 \uacb0\uacfc\uac00 \ub0ac\uc74c).<\/p>\n<\/div>\n\n<pre><code>-- \u274c \uc5d0\ub7ec \u2014 name \uc740 GROUP BY \ub3c4, \uc9d1\uacc4 \uc548\ub3c4 \uc544\ub2d8\nSELECT category_id, name, COUNT(*)\nFROM   products\nGROUP BY category_id;\n\n-- \u2705 name \uc744 GROUP BY \uc5d0 \ucd94\uac00\nSELECT category_id, name, COUNT(*)\nFROM   products\nGROUP BY category_id, name;<\/code><\/pre>\n\n<h2>HAVING \u2014 \uadf8\ub8f9 \uacb0\uacfc\uc5d0 \ud544\ud130<\/h2>\n\n<pre><code>-- \uc0c1\ud488\uc774 10\uac1c \uc774\uc0c1\uc778 \uce74\ud14c\uace0\ub9ac\ub9cc\nSELECT category_id, COUNT(*) AS n\nFROM   products\nGROUP BY category_id\nHAVING COUNT(*) &gt;= 10;\n\n-- \ud3c9\uade0 \uc8fc\ubb38 \uae08\uc561\uc774 50K \uc774\uc0c1\uc778 \uc9c0\uc5ed\nSELECT region, AVG(total) AS avg_t\nFROM   orders\nGROUP BY region\nHAVING AVG(total) &gt;= 50000;<\/code><\/pre>\n\n<h2>HAVING vs WHERE \u2014 \uac00\uc7a5 \uc790\uc8fc \ud5f7\uac08\ub9ac\ub294 \ud55c \uac00\uc9c0<\/h2>\n\n<div class=\"databox\">\n  <p><strong>\ud55c \uc904 \ucc28\uc774.<\/strong><\/p>\n  <p>\u00b7 <strong>WHERE<\/strong> \u2014 \uadf8\ub8f9\ud654 <strong>\uc804\uc5d0<\/strong> \ud589\uc5d0 \ud544\ud130. \uc9d1\uacc4 \ud568\uc218 \uc0ac\uc6a9 \ubd88\uac00.<\/p>\n  <p>\u00b7 <strong>HAVING<\/strong> \u2014 \uadf8\ub8f9\ud654 <strong>\ud6c4\uc5d0<\/strong> \uadf8\ub8f9\uc5d0 \ud544\ud130. \uc9d1\uacc4 \ud568\uc218 \uc0ac\uc6a9 \uac00\ub2a5.<\/p>\n<\/div>\n\n<pre><code>-- 2026\ub144 \uc8fc\ubb38\ub9cc \uac00\uc838\uc640\uc11c (WHERE) \u2192 \uc9c0\uc5ed\ubcc4 \ud569\uacc4 (GROUP) \u2192 \ud569\uacc4 100\ub9cc \uc774\uc0c1\ub9cc (HAVING)\nSELECT region, SUM(total) AS rev\nFROM   orders\nWHERE  created_at &gt;= '2026-01-01'\nGROUP BY region\nHAVING SUM(total) &gt;= 1000000\nORDER BY rev DESC;<\/code><\/pre>\n\n<p>WHERE \uac00 \uba3c\uc800 \ud589 \uc218\ub97c \uc904\uc774\ub294 \uac8c \uac70\uc758 \ud56d\uc0c1 \ub354 \ube60\ub985\ub2c8\ub2e4. \uac00\ub2a5\ud558\uba74 <strong>WHERE \ub85c \uba3c\uc800 \uc881\ud788\uace0, HAVING \uc740 \uadf8\ub8f9 \ub2e8\uc704 \ud544\ud130\uc5d0\ub9cc<\/strong>.<\/p>\n\n<h2>FILTER \u2014 \uc870\uac74\ubd80 \uc9d1\uacc4 (PostgreSQL \ud655\uc7a5)<\/h2>\n\n<pre><code>-- \ud55c \ucffc\ub9ac\ub85c \uc5ec\ub7ec \uc870\uac74\uc758 \uce74\uc6b4\ud2b8\nSELECT\n  COUNT(*)                                  AS total,\n  COUNT(*) FILTER (WHERE status = 'paid')   AS paid,\n  COUNT(*) FILTER (WHERE status = 'done')   AS done,\n  SUM(total) FILTER (WHERE region = 'KR')   AS rev_kr\nFROM orders;<\/code><\/pre>\n\n<p>\ud45c\uc900 SQL \uc758 <code>FILTER (WHERE ...)<\/code> \uad6c\ubb38. CASE \uc2dd (<code>SUM(CASE WHEN x THEN 1 ELSE 0 END)<\/code>) \ubcf4\ub2e4 \ud6e8\uc52c \uac00\ub3c5\uc131 \uc88b\uace0, MySQL \uc548 \ub428\/Postgres \ub428.<\/p>\n\n<h2>GROUPING SETS \u00b7 ROLLUP \u00b7 CUBE \u2014 \ub2e4\ucc28\uc6d0 \uc9d1\uacc4<\/h2>\n\n<pre><code>-- \ubd80\ubd84\ud569\u00b7\ucd1d\ud569\uc744 \ud55c \ucffc\ub9ac\ub85c\nSELECT region, category, SUM(total)\nFROM   orders\nGROUP BY ROLLUP (region, category);\n-- (region, category) \ubcc4 + (region) \ubcc4 + \ucd1d\ud569 \ud55c \ubc88\uc5d0<\/code><\/pre>\n\n<p>OLAP \uc2a4\ud0c0\uc77c \ucffc\ub9ac. \ub300\uc2dc\ubcf4\ub4dc\uc758 \ud569\uacc4 \ud589\u00b7\ucd1d\ud569 \ud589\uc744 \ud55c \ubc88\uc5d0 \uc5bb\uc744 \ub54c \uc720\uc6a9. \uc790\uc8fc \uc548 \uc368\ub3c4, \uc54c\uc544\ub450\uba74 BI \ucffc\ub9ac\uac00 \uac00\ubcbc\uc6cc\uc9d1\ub2c8\ub2e4.<\/p>\n\n<div class=\"cta\">\n  <h3>9\ud3b8 \u2014 JOIN 4\uc885 (INNER\u00b7LEFT\u00b7RIGHT\u00b7FULL)<\/h3>\n  <p>\uad00\uacc4\ud615\uc758 \uc9c4\uac00. \ub450 \ud14c\uc774\ube14\uc744 \uc5f0\uacb0\ud558\ub294 4\uac00\uc9c0\uc640 \uce74\ud2f0\uc2dc\uc548 \uacf1\uc758 \ud568\uc815.<\/p>\n<\/div>\n\n<div class=\"series-nav\">\n  <strong>\ud83d\udcda PostgreSQL \ubc30\uc6b0\uae30 \uad50\uc7ac<\/strong><br>\n  \uc774\uc804: 7\ud3b8 ORDER BY \u00b7 \ud604\uc7ac: <strong>8\ud3b8 (SQL \uae30\ucd08)<\/strong> \u00b7 \ub2e4\uc74c \u2192 9\ud3b8 JOIN \u00b7 \uc9c4\ud589: <strong>8\/24<\/strong>\n<\/div>\n\n<\/article>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\uc9d1\uacc4 \ud568\uc218\uc640 \uadf8\ub8f9\ud654, HAVING vs WHERE \ucc28\uc774, FILTER \uae4c\uc9c0. 24\ud3b8 \uad50\uc7ac 8\ud3b8.<\/p>\n","protected":false},"author":1,"featured_media":277,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-493","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-postgresql"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts\/493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/comments?post=493"}],"version-history":[{"count":0,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts\/493\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/media\/277"}],"wp:attachment":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/media?parent=493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/categories?post=493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/tags?post=493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}