{"id":491,"date":"2026-05-17T07:02:23","date_gmt":"2026-05-16T23:02:23","guid":{"rendered":"https:\/\/junai.ai\/blog\/pg-order-limit-7\/"},"modified":"2026-05-17T07:02:23","modified_gmt":"2026-05-16T23:02:23","slug":"pg-order-limit-7","status":"publish","type":"post","link":"https:\/\/junai.ai\/blog\/pg-order-limit-7\/","title":{"rendered":"ORDER BY\u00b7LIMIT \u2014 \uc815\ub82c\uacfc \ud398\uc774\uc9c0\ub124\uc774\uc158 (\uae30\ucd08 7\ud3b8)"},"content":{"rendered":"\n<!-- WordPress REST API \ubc1c\ud589\uc6a9 HTML (\uc790\ub3d9 \uc0dd\uc131) -->\n<!-- WP-FEATURED-MEDIA-ID: 274 -->\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 7\ud3b8 \/ 24\ud3b8<\/span>\n  <h1>ORDER BY\u00b7LIMIT\u00b7OFFSET \u2014 \uc815\ub82c\uacfc \ud398\uc774\uc9c0\ub124\uc774\uc158<\/h1>\n  <p class=\"sub\">\uc815\ub82c \uc635\uc158, LIMIT\/OFFSET \uc758 \ud568\uc815, \uadf8\ub9ac\uace0 \uc9c4\uc9dc \ube60\ub978 keyset pagination.<\/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-54.jpg\" alt=\"ORDER BY\u00b7LIMIT\u00b7OFFSET \uc758 \ub3d9\uc791\uc774 \uc815\ub82c\u00b7\ud398\uc774\uc9c0 \ub3c4\uc2dd\uc73c\ub85c \ud45c\ud604\ub41c \uc77c\ub7ec\uc2a4\ud2b8\">\n<\/header>\n\n<div class=\"container\">\n<article>\n\n<p>&#8220;\ucd5c\uadfc \uae00 10\uac1c&#8221; &#8220;\uac00\uaca9 \ub0ae\uc740 \uc21c&#8221; &#8220;\ub2e4\uc74c \ud398\uc774\uc9c0&#8221; \u2014 \uac70\uc758 \ubaa8\ub4e0 \ud654\uba74\uc774 ORDER BY \uc640 LIMIT \ub85c \ub9cc\ub4e4\uc5b4\uc9d1\ub2c8\ub2e4. 7\ud3b8\uc740 \ub458\uc758 \uae30\ubcf8\ubd80\ud130, \uadf8\ub9ac\uace0 \ub370\uc774\ud130\uac00 \ucee4\uc9c0\uba74 \ubc18\ub4dc\uc2dc \ubd80\ub52a\uce58\ub294 <strong>OFFSET \uc758 \ub290\ub9bc<\/strong> \uacfc \uadf8 \ud574\uacb0\ucc45 <strong>keyset pagination<\/strong> \uae4c\uc9c0.<\/p>\n\n<h2>ORDER BY \u2014 \uc815\ub82c \uae30\ubcf8<\/h2>\n\n<pre><code>-- \ud55c \uceec\ub7fc\nSELECT * FROM posts ORDER BY created_at DESC;\n\n-- \uc5ec\ub7ec \uceec\ub7fc (\uc55e \uceec\ub7fc \uba3c\uc800, \uac19\uc73c\uba74 \ub2e4\uc74c)\nSELECT * FROM users ORDER BY active DESC, age ASC;\n\n-- \uceec\ub7fc \ubc88\ud638 (\uac00\ub3c5\uc131 \ub098\uc068 \u2014 \uad8c\uc7a5 X)\nSELECT id, name FROM users ORDER BY 2;\n\n-- \ud45c\ud604\uc2dd \uc815\ub82c\nSELECT * FROM users ORDER BY lower(email);\n\n-- \uc815\ub82c \uacb0\uacfc \uceec\ub7fc\ub9cc \uac00\uc838\uc624\uba74 \uc778\ub371\uc2a4 \ud65c\uc6a9\ub3c4 \u2191\nSELECT id FROM posts ORDER BY created_at DESC LIMIT 10;<\/code><\/pre>\n\n<h2>NULLS FIRST \/ LAST<\/h2>\n\n<pre><code>-- \uae30\ubcf8: ASC \ub294 NULL \ub9c8\uc9c0\ub9c9, DESC \ub294 NULL \ucc98\uc74c\nSELECT name, phone FROM users ORDER BY phone;             -- NULL \ub9c8\uc9c0\ub9c9\nSELECT name, phone FROM users ORDER BY phone DESC;        -- NULL \ucc98\uc74c\n\n-- \uba85\uc2dc\nSELECT name, phone FROM users ORDER BY phone NULLS FIRST;\nSELECT name, phone FROM users ORDER BY phone DESC NULLS LAST;<\/code><\/pre>\n\n<div class=\"databox\">\n  <p><strong>\uc65c \uba85\uc2dc.<\/strong> \uc0ac\uc6a9\uc790 \ud654\uba74\uc5d0\uc11c &#8220;\uc804\ud654\ubc88\ud638 \uc5c6\ub294 \uc0ac\ub78c \uba3c\uc800&#8221; \uac19\uc740 \uc758\ub3c4\uac00 \ubd84\uba85\ud560 \ub54c NULLS FIRST\/LAST \uac00 \uac00\ub3c5\uc131\uc774 \ud6e8\uc52c \uc88b\uc2b5\ub2c8\ub2e4. \uadf8\ub9ac\uace0 \uc778\ub371\uc2a4\uc5d0\ub3c4 \uac19\uc740 NULLS \uc635\uc158\uc744 \uc918\uc57c \uc815\ub82c\uc774 \uc778\ub371\uc2a4\ub97c 100% \ud65c\uc6a9\ud569\ub2c8\ub2e4(16\ud3b8).<\/p>\n<\/div>\n\n<h2>LIMIT \u00b7 OFFSET \u2014 \uae30\ubcf8 \ud398\uc774\uc9c0\ub124\uc774\uc158<\/h2>\n\n<pre><code>-- \ucc98\uc74c 10\uac1c\nSELECT * FROM posts ORDER BY created_at DESC LIMIT 10;\n\n-- \ub450 \ubc88\uc9f8 \ud398\uc774\uc9c0 (10\uac1c\uc529, 10\ubc88\uc9f8 \ub2e4\uc74c\ubd80\ud130)\nSELECT * FROM posts\nORDER BY created_at DESC\nLIMIT 10 OFFSET 10;\n\n-- \ud398\uc774\uc9c0 N \u2014 1-base\n-- offset = (N - 1) * size\nSELECT * FROM posts\nORDER BY created_at DESC\nLIMIT 10 OFFSET (3 - 1) * 10;     -- 3\ud398\uc774\uc9c0\n\n-- \ud45c\uc900 SQL \ud45c\ud604 (FETCH FIRST)\nSELECT * FROM posts\nORDER BY created_at DESC\nFETCH FIRST 10 ROWS ONLY;<\/code><\/pre>\n\n<h2>OFFSET \uc758 \ud568\uc815 \u2014 \ud398\uc774\uc9c0\uac00 \uae4a\uc744\uc218\ub85d \ub290\ub824\uc9c4\ub2e4<\/h2>\n\n<pre><code>-- 99,900 \ud589\uc744 skip \ud558\uace0 100\uac1c \uac00\uc838\uc624\uae30\nEXPLAIN ANALYZE\nSELECT * FROM posts\nORDER BY created_at DESC\nLIMIT 100 OFFSET 99900;<\/code><\/pre>\n\n<div class=\"warnbox\">\n  <p><strong>\uc65c \ub290\ub9b4\uae4c.<\/strong> OFFSET \uc740 \uc815\ud655\ud788 &#8220;\uc55e\uc758 N \uac1c \ud589\uc744 \uc2a4\uce94\ud558\uace0 \ubc84\ub9b0\ub2e4&#8221; \uc785\ub2c8\ub2e4. 100\ub9cc \ud589 \uc911 \ud398\uc774\uc9c0 1000(OFFSET 99,000)\uc744 \uac00\uc838\uc624\ub824\uba74 99,000 \ud589\uc744 \uc77d\uace0 \ubc84\ub9b0 \ub4a4 100 \uac1c\ub97c \ub354 \uc77d\uc2b5\ub2c8\ub2e4. \ud398\uc774\uc9c0\uac00 \uae4a\uc744\uc218\ub85d \uc120\ud615\uc73c\ub85c \ub290\ub824\uc838\uc694. \ud398\uc774\uc9c0 5,000 \uc740 \uac70\uc758 \uc548 \ub429\ub2c8\ub2e4.<\/p>\n<\/div>\n\n<h2>keyset pagination \u2014 \uc9c4\uc9dc \ube60\ub978 \ubc29\uc2dd<\/h2>\n\n<pre><code>-- 1\ud398\uc774\uc9c0 (\uac00\uc7a5 \ucd5c\uadfc 10\uac1c)\nSELECT id, title, created_at\nFROM   posts\nORDER BY created_at DESC, id DESC\nLIMIT 10;\n-- \uacb0\uacfc\uc758 \ub9c8\uc9c0\ub9c9 \ud589: created_at='2026-05-17 02:00', id=12345\n\n-- 2\ud398\uc774\uc9c0 \u2014 \ub9c8\uc9c0\ub9c9 \ud589 \"\uc774\ud6c4\" \ubd80\ud130\nSELECT id, title, created_at\nFROM   posts\nWHERE  (created_at, id) &lt; ('2026-05-17 02:00', 12345)\nORDER BY created_at DESC, id DESC\nLIMIT 10;<\/code><\/pre>\n\n<p>&#8220;\uc55e \ud398\uc774\uc9c0\uc758 \ub9c8\uc9c0\ub9c9 \ud589\ubcf4\ub2e4 \uc791\uc740 \uac83&#8221; \uc774\ub77c\ub294 \uba85\ud655\ud55c \uc870\uac74\uc774 \uc778\ub371\uc2a4 \uac80\uc0c9\uc5d0 \uc815\ud655\ud788 \ub4e4\uc5b4\ub9de\uc2b5\ub2c8\ub2e4. <strong>\uba87 \ucc9c \ud398\uc774\uc9c0\ub97c \uac00\ub3c4 \uccab \ud398\uc774\uc9c0\uc640 \uac19\uc740 \uc18d\ub3c4<\/strong>. (created_at, id) \uac19\uc740 \ubcf5\ud569 \uc778\ub371\uc2a4\uac00 \uc788\uc73c\uba74 \uc644\ubcbd\ud569\ub2c8\ub2e4.<\/p>\n\n<div class=\"tablewrap\">\n<table>\n  <thead>\n    <tr><th>\ubc29\uc2dd<\/th><th>\uc7a5\uc810<\/th><th>\ub2e8\uc810<\/th><\/tr>\n  <\/thead>\n  <tbody>\n    <tr><td data-label=\"\ubc29\uc2dd\">OFFSET<\/td><td data-label=\"\uc7a5\uc810\">&#8220;X \ud398\uc774\uc9c0\ub85c \uc774\ub3d9&#8221; \uc784\uc758 \uc810\ud504 \uac00\ub2a5<\/td><td data-label=\"\ub2e8\uc810\">\uae4a\uc740 \ud398\uc774\uc9c0 \ub9e4\uc6b0 \ub290\ub9bc + \uc911\uac04 INSERT \uc2dc \uc911\ubcf5<\/td><\/tr>\n    <tr><td data-label=\"\ubc29\uc2dd\">keyset<\/td><td data-label=\"\uc7a5\uc810\">\uc18d\ub3c4 \uc77c\uc815, \uc911\uac04 \ubcc0\ub3d9\uc5d0 \uc548\uc804<\/td><td data-label=\"\ub2e8\uc810\">&#8220;X \ud398\uc774\uc9c0\ub85c \uc810\ud504&#8221; \ubd88\uac00\ub2a5 (\ub2e4\uc74c\/\uc774\uc804\ub9cc)<\/td><\/tr>\n  <\/tbody>\n<\/table>\n<\/div>\n\n<div class=\"databox\">\n  <p><strong>UX \uad8c\uc7a5.<\/strong> \ubb34\ud55c \uc2a4\ud06c\ub864\u00b7\ub2e4\uc74c \ubc84\ud2bc UI \ub294 <strong>keyset<\/strong> \uc774 \uc815\ub2f5. 1-2-3-4-5 \ud398\uc774\uc9c0 \ubc88\ud638 UI \ub294 OFFSET \uc774 \uc790\uc5f0\uc2a4\ub7ec\uc6b0\ub2c8 \ub370\uc774\ud130 1\ub9cc \uac74 \uc774\ud558\uc5d0\uc11c\ub9cc \uc720\uc9c0. \uadf8 \uc774\uc0c1\uc740 \uac80\uc0c9\u00b7\ud544\ud130\ub85c \uc881\ud78c \ub2e4\uc74c keyset.<\/p>\n<\/div>\n\n<h2>&#8220;\uba87 \ud398\uc774\uc9c0 \uc788\ub294\uc9c0&#8221; \u2014 COUNT \uc758 \ube44\uc6a9<\/h2>\n\n<pre><code>-- \uc815\ud655\ud55c \ucd1d \ud589 \uc218 \u2014 \ub290\ub9bc (\uc804\uccb4 \uc2a4\uce94)\nSELECT count(*) FROM posts;\n\n-- \uc815\ud655\ud558\uc9c0 \uc54a\uc544\ub3c4 OK \uba74 \ud1b5\uacc4 \uc0ac\uc6a9 (\uc989\uc2dc)\nSELECT reltuples::bigint FROM pg_class WHERE relname = 'posts';\n\n-- \ud398\uc774\uc9c0\ub124\uc774\uc158\uacfc \ud568\uaed8 (\uc804\uccb4 count \ud638\ucd9c\uc740 \uad8c\uc7a5 X)\n-- \ud654\uba74\uc5d0 \"X \ud398\uc774\uc9c0 \uc911 Y\" \ub97c \uc815\ub9d0 \ubcf4\uc5ec\uc918\uc57c \ud55c\ub2e4\uba74 \uce90\uc2dc<\/code><\/pre>\n\n<p>\ub300\uaddc\ubaa8 \ud14c\uc774\ube14\uc758 COUNT(*) \ub294 \ud398\uc774\uc9c0 1\uc744 \uadf8\ub9b4 \ub54c\ub9c8\ub2e4 \ubd80\ub974\uba74 \uc548 \ub429\ub2c8\ub2e4. UX \uc801\uc73c\ub85c\ub3c4 &#8220;\ucd1d 53,427\uac1c \uc911 1-10&#8221; \ubcf4\ub2e4 &#8220;\ub354 \ubcf4\uae30&#8221; \uac00 \ubcf4\ud1b5 \ub354 \uc790\uc5f0\uc2a4\ub7fd\uc2b5\ub2c8\ub2e4.<\/p>\n\n<div class=\"cta\">\n  <h3>8\ud3b8 \u2014 GROUP BY \uc640 \uc9d1\uacc4 \ud568\uc218<\/h3>\n  <p>COUNT\u00b7SUM\u00b7AVG\u00b7HAVING, &#8220;\uadf8\ub8f9\ubcc4 \ud1b5\uacc4&#8221; \uc758 \uc815\uc11d.<\/p>\n<\/div>\n\n<div class=\"series-nav\">\n  <strong>\ud83d\udcda PostgreSQL \ubc30\uc6b0\uae30 \uad50\uc7ac<\/strong><br>\n  \uc774\uc804: 6\ud3b8 WHERE \u00b7 \ud604\uc7ac: <strong>7\ud3b8 (SQL \uae30\ucd08)<\/strong> \u00b7 \ub2e4\uc74c \u2192 8\ud3b8 GROUP BY \u00b7 \uc9c4\ud589: <strong>7\/24<\/strong>\n<\/div>\n\n<\/article>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>ORDER BY\u00b7LIMIT\u00b7OFFSET \uc0ac\uc6a9\uacfc keyset pagination. 24\ud3b8 \uad50\uc7ac 7\ud3b8.<\/p>\n","protected":false},"author":1,"featured_media":274,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-491","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\/491","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=491"}],"version-history":[{"count":0,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/posts\/491\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/media\/274"}],"wp:attachment":[{"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/media?parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/categories?post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/junai.ai\/blog\/wp-json\/wp\/v2\/tags?post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}