{"id":7267,"date":"2024-08-31T08:37:16","date_gmt":"2024-08-31T08:37:16","guid":{"rendered":"https:\/\/pohang.eduwp.kr\/?p=7267"},"modified":"2026-04-20T04:31:03","modified_gmt":"2026-04-20T04:31:03","slug":"4%ec%a3%bc%ec%b0%a8-3","status":"publish","type":"post","link":"https:\/\/pohang.eduwp.kr\/?p=7267","title":{"rendered":"4\uc8fc\ucc28"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_81 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"\ubaa9\ucc28 \ud1a0\uae00\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/pohang.eduwp.kr\/?p=7267\/#%EC%A3%BC%EC%84%9D%EC%B2%98%EB%A6%AC\" >\uc8fc\uc11d\ucc98\ub9ac<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/pohang.eduwp.kr\/?p=7267\/#order_by_group_by_having\" >order by, group by , having<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/pohang.eduwp.kr\/?p=7267\/#%ED%95%A9%EA%B3%84%EB%A5%BC_%EB%B9%84%EB%A1%AF%ED%95%9C_%EB%8B%A4%EC%96%91%ED%95%9C_%EC%88%98%EC%8B%9D%ED%95%A8%EC%88%98\" >\ud569\uacc4\ub97c \ube44\ub86f\ud55c \ub2e4\uc591\ud55c \uc218\uc2dd\ud568\uc218<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/pohang.eduwp.kr\/?p=7267\/#subQeury_%EC%A1%B0%ED%95%A9%ED%95%98%EA%B8%B0\" >subQeury \uc870\ud569\ud558\uae30<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/pohang.eduwp.kr\/?p=7267\/#anyallsome_subQeury\" >any\/all\/some , subQeury<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/pohang.eduwp.kr\/?p=7267\/#order_by_%EC%98%A4%EB%A6%84%EC%B0%A8%EC%88%9C_%EB%82%B4%EB%A6%BC%EC%B0%A8%EC%88%9C_%EC%82%B4%ED%8E%B4%EB%B3%B4%EA%B8%B0\" >order by \uc624\ub984\ucc28\uc21c, \ub0b4\ub9bc\ucc28\uc21c \uc0b4\ud3b4\ubcf4\uae30<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/pohang.eduwp.kr\/?p=7267\/#distinct_limit\" >distinct , limit<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"%EC%A3%BC%EC%84%9D%EC%B2%98%EB%A6%AC\"><\/span>\uc8fc\uc11d\ucc98\ub9ac<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>&#8212; \ud55c\uc904 \uc8fc\uc11d \uc5f0\uc2b5<\/p>\n<p>\uc5ec\ub7ec\uc904 \uc8fc\uc11d<\/p>\n<p>\/*\u00a0 \u00a0\ub0b4\uc6a9\u00a0 \u00a0 \u00a0 *\/<\/p>\n<hr \/>\n<p><strong>select select_expr\u00a0<\/strong><\/p>\n<p><strong>[from table_references]<\/strong><\/p>\n<p><strong>[where where_condition]<\/strong><\/p>\n<p><strong>[group by [col_name | expr | positoin}]<\/strong><\/p>\n<p><strong>[having wherer_condition]<\/strong><\/p>\n<p><strong>[order by {col_name | expr | postion}]<\/strong><\/p>\n<p><strong>limit<\/strong><\/p>\n<hr \/>\n<p>&nbsp;<\/p>\n<h1><span class=\"ez-toc-section\" id=\"order_by_group_by_having\"><\/span>order by, group by , having<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>order by : \uc815\ub82c\uc21c\uc11c \uc9c0\uc815\ud558\uae30<\/p>\n<p>group by :\u00a0 \uc6d0\ud558\ub294 \uc544\uc774\ub514\ub97c \uadf8\ub8f9\uc744 \ub9cc\ub4e4\uc5b4\uc11c \uc815\ub82c\ud558\uae30 (ID\uac12\uc744 \uadf8\ub8f9 \uc9c0\uc5b4\uc11c \uc21c\uc11c \uc815\ub82c)<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">use sqldb;\r\nselect * from buytbl;\r\n\r\n\r\n-- \uc544\uc774\ub514\ub97c \uc21c\uc11c\ub370\ub85c \uc815\ub82c --\r\nselect userID, amount from buytbl order by userID;\r\n\r\n-- \uc544\uc774\ub514\ub97c \uadf8\ub8f9\uc9c0\uc5b4\uc11c \uc544\uc774\ub514\uc640 \uad6c\ub9e4\ud569\uacc4\ub97c \uc815\ub82c --\r\nselect userID, sum(amount) from buytbl group by userID;\r\n\r\n\r\n-- \uc54c\ub9ac\uc544\uc2a4\ud45c\ud604 --\r\nselect userID AS '\uc0ac\uc6a9\uc790 \uc544\uc774\ub514' , sum(amount) AS '\ucd1d\uad6c\ub9e4 \uac2f\uc218' from buytbl group by userID;\r\nselect userID AS '\uc0ac\uc6a9\uc790 \uc544\uc774\ub514' , sum(price * amount) AS '\ucd1d\uad6c\ub9e4 \uae08\uc561' from buytbl group by userID;<\/pre>\n<p>&nbsp;<\/p>\n<h1><span class=\"ez-toc-section\" id=\"%ED%95%A9%EA%B3%84%EB%A5%BC_%EB%B9%84%EB%A1%AF%ED%95%9C_%EB%8B%A4%EC%96%91%ED%95%9C_%EC%88%98%EC%8B%9D%ED%95%A8%EC%88%98\"><\/span>\ud569\uacc4\ub97c \ube44\ub86f\ud55c \ub2e4\uc591\ud55c \uc218\uc2dd\ud568\uc218<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>sum, avg, min, max, count, count(distinct). sedev, var_samp<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">select userID AS '\uc0ac\uc6a9\uc790 \uc544\uc774\ub514' , sum(amount) AS '\ucd1d\uad6c\ub9e4 \uac2f\uc218' from buytbl group by userID;\r\nselect userID AS '\uc0ac\uc6a9\uc790 \uc544\uc774\ub514' , sum(price * amount) AS '\ucd1d\uad6c\ub9e4 \uae08\uc561' from buytbl group by userID;\r\nselect userID AS '\uc0ac\uc6a9\uc790 \uc544\uc774\ub514' , avg(price * amount) AS '\ud3c9\uade0\uae08\uc561' from buytbl group by userID;\r\nselect userID AS '\uc0ac\uc6a9\uc790 \uc544\uc774\ub514' , min(price * amount) AS '\ucd5c\uc18c\uae08\uc561' from buytbl group by userID;\r\nselect userID AS '\uc0ac\uc6a9\uc790 \uc544\uc774\ub514' , count(price * amount) AS '\uad6c\ub9e4\ud69f\uc218' from buytbl group by userID;\r\nselect userID AS '\uc0ac\uc6a9\uc790 \uc544\uc774\ub514' , stddev(price * amount) AS '\ud45c\uc900\ud3b8\ucc28' from buytbl group by userID;\r\nselect userID AS '\uc0ac\uc6a9\uc790 \uc544\uc774\ub514' , var_samp(price * amount) AS '\ubd84\uc0b0' from buytbl group by userID;<\/pre>\n<p>&nbsp;<\/p>\n<p>\uad6c\ub9e4\uc790\uac00 \uad6c\ub9e4\ud55c \ubb3c\ud488\uc758 \uac1c\uc218\ub97c \ud3c9\uade0\ub97c \uad6c\ud558\ub294 \uc2dd<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">select avg(amount) as '\ud3c9\uade0 \uad6c\ub9e4 \uac1c\uc218' from buytbl;\r\nselect userID , avg(amount) as '\ud3c9\uade0 \uad6c\ub9e4 \uac1c\uc218' from buytbl group by userID;<\/pre>\n<p>&nbsp;<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"subQeury_%EC%A1%B0%ED%95%A9%ED%95%98%EA%B8%B0\"><\/span>subQeury \uc870\ud569\ud558\uae30<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">select name, height from usertbl;\r\nselect name, max(height), min(height) from usertbl;  -- \ud2c0\ub9bc --\r\nselect name, max(height), min(height) from usertbl group by Name; -- \ud2c0\ub9bc --\r\n\r\n-- \ud55c\uba85\uc529 \uc11c\ube0c\ucffc\ub9ac\ub97c \uc0dd\uc131\ud574\uc11c \ucd5c\ub300\ud0a4, \ucd5c\uc18c\ud0a4\uc758 \uac12\uc744 \ubf51\uc544\ub0b8\ub2e4.\r\n\r\nselect name, height from usertbl \r\nwhere height = (select max(height) from usertbl ) or height = ( select min(height) from usertbl);\r\n\r\n(\uac00\uc7a5 \ud070 \ud0a4\uac12\uc744 \ud55c\uba85 \ubf51\uc544\ub0b4\uace0) or (\uac00\uc7a5 \uc791\uc740 \ud0a4 \uac12\uc744 \ubf51\uc544\ub0b4\uace0) \ud45c\ud604\uc740 \uc11c\ube0c\ucffc\ub9ac\ub85c \ub3cc\ub824\uc57c \ud55c\ub2e4. \r\n\r\n\r\n\ud734\ub300\ud3f0\uc774 \uc788\ub294 \uc0ac\uc6a9\uc790 \uc218\u00a0\r\nselect count(mobile1) AS '\ud734\ub300\ud3f0\uc774 \uc788\ub294 \uc0ac\uc6a9\uc790' from usertbl;\u00a0\r\n\r\n\r\n\uad6c\ub9e4\ud14c\uc774\ube14\uc5d0\uc11c \uad6c\ub9e4\ub97c \uac00\uc7a5 \ub9ce\uc774 \ud68c\uc6d0\uc744 \ubd88\ub7ec\uc624\ub294 \ucffc\ub9ac\r\n\uc624\ub984\ucc28\uc21c\uacfc \ub0b4\ub9bc\ucc28\uc21c\uc744 \uc774\uc6a9\ud574\uc11c \uac00\uc7a5 \uc0c1\uc704\uac12\ub9cc \ubd88\ub7ec\uc624\uae30\r\n\r\nselect userID as '\uc0ac\uc6a9\uc790' , sum(price*amount) as '\ucd1d\uad6c\ub9e4\uc561' from buytbl;\r\n\r\nselect userID as '\uc0ac\uc6a9\uc790' , sum(price*amount) as '\ucd1d\uad6c\ub9e4\uc561' from buytbl group by userID;\r\n\r\nselect userID as '\uc0ac\uc6a9\uc790' , sum(price*amount) as '\ucd1d\uad6c\ub9e4\uc561' from buytbl group by userID order by userID;\r\n\r\nselect userID as '\uc0ac\uc6a9\uc790' , sum(price*amount) as '\ucd1d\uad6c\ub9e4\uc561' from buytbl group by userID order by userID desc limit 1;\r\n\r\nselect userID as '\uc0ac\uc6a9\uc790' , sum(price*amount) as '\ucd1d\uad6c\ub9e4\uc561' from buytbl group by userID order by userID asc limit 1;\r\n\r\n\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h1><span class=\"ez-toc-section\" id=\"anyallsome_subQeury\"><\/span>any\/all\/some , subQeury<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">-- 177\ubcf4\ub2e4 \ud0a4\uac00 \ud070 \uc0ac\ub78c \uc120\ud0dd\ud558\uae30 --\r\nselect name, height from usertbl where height &gt; 177;\u00a0\r\n\r\n\r\n-- \uae40\uacbd\ud638\uc758 \ud0a4\ub97c \ubd88\ub7ec\uc640\uc11c \uadf8 \ud0a4\uac12\ubcf4\ub2e4 \ud070 \uc0ac\ub78c \uc120\ud0dd\ud558\uae30 , \uae40\uacbd\ud638 \ud0a4\uac00 177\uc774\ubbc0\ub85c \uacb0\uad6d \uac19\uc740 \ub370\uc774\ud130 -\r\nselect name, height from usertbl where height &gt; (select height from usertbl where name = '\uae40\uacbd\ud638');\r\n\r\n\r\n-- \uc624\ub958 \uc6d0\uc778\uc744 \ud30c\uc545\ud558\uae30 , \uacbd\ub0a8\uc758 \ud0a4\uac12\uc740 2\uac1c\uac00 \ubc18\ud658\uc774 \ub41c\ub2e4. \uc815\ud655\ud55c \uae30\uc900\uc774 \uc5c6\uae30 \ub54c\ubb38\uc5d0 \uc624\ub958\uac00 \ub09c\ub2e4. --\r\nselect name, height from usertbl where height &gt; (select height from usertbl where addr = '\uacbd\ub0a8');\r\n\r\n\r\n-- any\ub97c \ubd99\uc774\uba74 173, 170 \ubaa8\ub450 \ud574\ub2f9\uc774 \ub418\uc5b4 \uacb0\uacfc\uc801\uc73c\ub85c 170 \uc774\uc0c1\uc744 \ubaa8\ub450 \uc120\ud0dd\ud558\uac8c \ub41c\ub2e4.  -- \r\nselect name, height from usertbl where height &gt;= any(select height from usertbl where addr = '\uacbd\ub0a8');\r\n\r\n-- all\uc744 \ubd99\uc774\uba74 173,170 \ubaa8\ub450 \ub9cc\uc871\uc774 \ub418\uc5b4\uc57c \ud558\uae30 \ub54c\ubb38\uc5d0 173 \uc774\uc0c1\uc744 \ubaa8\ub450 \uc120\ud0dd\ud558\uac8c \ub41c\ub2e4. -- \r\nselect name, height from usertbl where height &gt;= all(select height from usertbl where addr = '\uacbd\ub0a8');\r\n\r\n\r\n-- some, any \ub3d9\uc77c\ud55c \uc758\ubbf8 --\r\nselect name, height from usertbl wherer height = ANY( select height from usertbl where addr = '\uacbd\ub0a8');\r\nselect\u00a0name,\u00a0height\u00a0from\u00a0usertbl\u00a0where\u00a0height\u00a0&gt;=\u00a0any(select\u00a0height\u00a0from\u00a0usertbl\u00a0where\u00a0addr\u00a0=\u00a0'\uc11c\uc6b8');\r\n\r\n-- in \uc0ac\uc6a9\ubc95 \r\nselect name, height from usertbl where height IN ( select height from usertbl where addr = '\uacbd\ub0a8');\r\n<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">select name, height from usertbl where height in (select height from usertbl where addr = '\uacbd\ub0a8');\r\n<\/pre>\n<h1><span class=\"ez-toc-section\" id=\"order_by_%EC%98%A4%EB%A6%84%EC%B0%A8%EC%88%9C_%EB%82%B4%EB%A6%BC%EC%B0%A8%EC%88%9C_%EC%82%B4%ED%8E%B4%EB%B3%B4%EA%B8%B0\"><\/span>order by \uc624\ub984\ucc28\uc21c, \ub0b4\ub9bc\ucc28\uc21c \uc0b4\ud3b4\ubcf4\uae30<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">select name, mDate from usertbl order by mDate;\r\n\r\nselect name, mDate from usertbl order by mDate DESC;\r\n\r\nselect name, mDate from usertbl order by mDate ASC; \r\n\r\nASC\ub294 \uae30\ubcf8\uac12\r\n\r\nselect name, name from usertbl order by height DESC, name ASC;\r\n2\uac1c\ub97c \ucc98\ub9ac \r\n\r\n\r\n<\/pre>\n<p>order by\ub294 mysql \uc131\ub2a5\uc744 \ub5a8\uc5b4\ub728\ub9b4 \uc18c\uc9c0\uac00 \uc788\uc5b4\uc11c \ud544\uc694\ud560 \ub54c\ub9cc \uc0ac\uc6a9\ud55c\ub2e4.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h1><span class=\"ez-toc-section\" id=\"distinct_limit\"><\/span>distinct , limit<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">select addr from usertbl order by addr;\r\n\r\nselect distinct addr from usertbl order by addr;\r\n\r\n\r\n\r\n<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">select\u00a0distinct\u00a0addr,\u00a0height\u00a0from\u00a0usertbl\u00a0order\u00a0by\u00a0height\u00a0asc\u00a0limit\u00a03;\r\n\r\nselect\u00a0distinct\u00a0addr,\u00a0height\u00a0from\u00a0usertbl\u00a0order\u00a0by\u00a0height\u00a0desc\u00a0limit\u00a03;\r\n\r\nselect distinct addr, height from usertbl order by height desc limit 0, 3;\r\n\r\n<\/pre>\n<p>\uc911\ubcf5\ucd9c\ub825\ub418\ub294\uac83\uc744 \ud558\ub098\ub85c \ub300\uce58\ud558\ub294 \uba85\ub839, \ucffc\ub9ac \uacb0\uacfc\uac00 \ub9ce\uc744 \ub54c \uc81c\ud55c \uac78\uae30<\/p>\n<p>order by, asc , limit \ub85c \uc77c\ubd80\ub9cc \ucd9c\ub825\ud558\uae30<\/p>\n<h2><\/h2>\n<h1><\/h1>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\uc8fc\uc11d\ucc98\ub9ac &#8212; \ud55c\uc904 \uc8fc\uc11d \uc5f0\uc2b5 \uc5ec\ub7ec\uc904 \uc8fc\uc11d \/*\u00a0 \u00a0\ub0b4\uc6a9\u00a0 \u00a0 \u00a0 *\/ select select_expr\u00a0 [from table_references] [where where_condition] [group by [col_name | expr | positoin}] [having wherer_condition] [order by {col_name&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-7267","post","type-post","status-publish","format-standard","category-mysql"],"_links":{"self":[{"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=\/wp\/v2\/posts\/7267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7267"}],"version-history":[{"count":6,"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=\/wp\/v2\/posts\/7267\/revisions"}],"predecessor-version":[{"id":7442,"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=\/wp\/v2\/posts\/7267\/revisions\/7442"}],"wp:attachment":[{"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pohang.eduwp.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}