{"id":6546,"date":"2026-07-06T22:35:33","date_gmt":"2026-07-06T13:35:33","guid":{"rendered":"https:\/\/route-zero.com\/recruit\/?post_type=route&#038;p=6546"},"modified":"2026-11-06T22:39:42","modified_gmt":"2026-11-06T13:39:42","slug":"sql-rank-dense-rank-row-number-differences","status":"publish","type":"route","link":"https:\/\/route-zero.com\/recruit\/route\/6546\/","title":{"rendered":"SQL\u306eRANK\u30fbDENSE_RANK\u30fbROW_NUMBER\u306e\u9055\u3044\uff5c\u540c\u70b9\u6642\u306e\u9806\u4f4d"},"content":{"rendered":"<p>SQL\u3067\u9806\u4f4d\u3092\u4ed8\u3051\u305f\u3089\u300c1\u4f4d\u30011\u4f4d\u30013\u4f4d\u300d\u306b\u306a\u3063\u305f\u308a\u3001\u540c\u70b9\u306a\u306e\u306b\u756a\u53f7\u304c\u5206\u304b\u308c\u305f\u308a\u3057\u307e\u305b\u3093\u304b\u3002<strong>RANK\u30fbDENSE_RANK\u30fbROW_NUMBER<\/strong>\u306f\u3001\u540c\u70b9\u3092\u3069\u3046\u6271\u3046\u304b\u304c\u9055\u3044\u307e\u3059\u3002\u307e\u305a\u7d50\u679c\u306e\u9055\u3044\u3092\u898b\u3066\u304b\u3089\u3001\u624b\u5143\u3067\u78ba\u304b\u3081\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>\u7d50\u8ad6\uff1a\u540c\u70b9\u3092\u3069\u3046\u6271\u3044\u305f\u3044\u304b\u3067\u9078\u3076<\/h2>\n<p>3\u3064\u3068\u3082SQL\u306e<strong>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570<\/strong>\u3067\u3059\u3002\u884c\u3092\u307e\u3068\u3081\u3066\u6d88\u3055\u305a\u3001\u4e26\u3079\u305f\u30c7\u30fc\u30bf\u306e\u5404\u884c\u306b\u9806\u4f4d\u3084\u756a\u53f7\u3092\u4ed8\u3051\u3089\u308c\u307e\u3059\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u95a2\u6570<\/th>\n<th>\u540c\u70b9\u306e\u6271\u3044<\/th>\n<th>4\u884c\u306e\u4f8b\uff08\u70b9\u657090,90,80,70\uff09<\/th>\n<th>\u9078\u3076\u5834\u9762<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>ROW_NUMBER()<\/td>\n<td>\u6bce\u884c\u306b\u7570\u306a\u308b\u9023\u756a<\/td>\n<td>1,2,3,4<\/td>\n<td>\u4e00\u89a7\u30781\u884c\u305a\u3064\u756a\u53f7\u3092\u632f\u308a\u305f\u3044<\/td>\n<\/tr>\n<tr>\n<td>RANK()<\/td>\n<td>\u540c\u70b9\u306f\u540c\u9806\u4f4d\uff0f\u6b21\u306e\u9806\u4f4d\u306f\u98db\u3076<\/td>\n<td>1,1,3,4<\/td>\n<td>\u7af6\u6280\u306e\u9806\u4f4d\u306e\u3088\u3046\u306b\u6b20\u756a\u3092\u6b8b\u3057\u305f\u3044<\/td>\n<\/tr>\n<tr>\n<td>DENSE_RANK()<\/td>\n<td>\u540c\u70b9\u306f\u540c\u9806\u4f4d\uff0f\u6b21\u306e\u9806\u4f4d\u306f\u8a70\u3081\u308b<\/td>\n<td>1,1,2,3<\/td>\n<td>\u7570\u306a\u308b\u70b9\u6570\u306b1\u4f4d\u30012\u4f4d\u3068\u756a\u53f7\u3092\u632f\u308a\u305f\u3044<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<blockquote>\n<p><strong>\u7528\u8a9e\u89e3\u8aac\uff1a\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570<\/strong><br \/>\u3042\u308b\u884c\u3092\u6b8b\u3057\u305f\u307e\u307e\u3001\u305d\u306e\u884c\u3068\u95a2\u9023\u3059\u308b\u8907\u6570\u884c\u306e\u60c5\u5831\u3092\u4f7f\u3063\u3066\u8a08\u7b97\u3059\u308bSQL\u306e\u95a2\u6570\u30023\u3064\u306e\u9806\u4f4d\u95a2\u6570\u306f `OVER (ORDER BY &#8230;)` \u306e\u4e26\u3079\u65b9\u306b\u5f93\u3044\u307e\u3059\u3002<\/p>\n<\/blockquote>\n<p>\u305f\u3068\u3048\u307090\u70b9\u304c2\u4eba\u3044\u308b\u3068\u304d\u3001`RANK()`\u306f\u4e21\u8005\u30921\u4f4d\u3068\u3057\u3001\u6b21\u306e80\u70b9\u306f3\u4f4d\u3067\u3059\u3002`DENSE_RANK()`\u3067\u306f80\u70b9\u306f2\u4f4d\u3067\u3059\u3002<\/p>\n<hr>\n<h2>4\u4eba\u306e\u30c7\u30fc\u30bf\u3067\u7d50\u679c\u3092\u518d\u73fe\u3059\u308b<\/h2>\n<p>\u6b21\u306eSQL\u306f<strong>PostgreSQL\u3068SQLite<\/strong>\u3067\u8a66\u305b\u308b\u69cb\u6210\u3067\u3059\u3002`WITH &#8230; VALUES`\u3067\u7df4\u7fd2\u7528\u30c7\u30fc\u30bf\u3092\u305d\u306e\u5834\u306b\u7528\u610f\u3059\u308b\u305f\u3081\u3001\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3057\u306a\u304f\u3066\u3082\u7d50\u679c\u3092\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code>WITH scores(id, name, points) AS (\r\n  VALUES (1, &#39;\u4f50\u85e4&#39;, 90), (2, &#39;\u9234\u6728&#39;, 90),\r\n         (3, &#39;\u7530\u4e2d&#39;, 80), (4, &#39;\u9ad8\u6a4b&#39;, 70)\r\n)\r\nSELECT\r\n  name, points,\r\n  ROW_NUMBER() OVER (ORDER BY points DESC, id) AS row_no,\r\n  RANK() OVER (ORDER BY points DESC) AS rank_no,\r\n  DENSE_RANK() OVER (ORDER BY points DESC) AS dense_no\r\nFROM scores\r\nORDER BY points DESC, id;<\/code><\/pre>\n<table>\n<thead>\n<tr>\n<th>\u540d\u524d<\/th>\n<th>\u70b9\u6570<\/th>\n<th>row_no<\/th>\n<th>rank_no<\/th>\n<th>dense_no<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u4f50\u85e4<\/td>\n<td>90<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>\u9234\u6728<\/td>\n<td>90<\/td>\n<td>2<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>\u7530\u4e2d<\/td>\n<td>80<\/td>\n<td>3<\/td>\n<td>3<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>\u9ad8\u6a4b<\/td>\n<td>70<\/td>\n<td>4<\/td>\n<td>4<\/td>\n<td>3<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>`DESC`\u306f\u5927\u304d\u3044\u5024\u304b\u3089\u4e26\u3079\u308b\u6307\u5b9a\u3067\u3059\u3002`id`\u306f\u540c\u70b9\u306e2\u4eba\u3092\u5b89\u5b9a\u3057\u305f\u9806\u756a\u306b\u4e26\u3079\u308b\u305f\u3081\u3001`ROW_NUMBER()`\u3060\u3051\u306b\u52a0\u3048\u3066\u3044\u307e\u3059\u3002\u3053\u3053\u304c\u91cd\u8981\u3067\u3059\u3002<\/p>\n<hr>\n<h2>ROW_NUMBER\u3060\u3051id\u3092\u52a0\u3048\u308b\u7406\u7531<\/h2>\n<p>`ROW_NUMBER()`\u306f\u540c\u3058\u70b9\u6570\u306e\u884c\u306b\u3082\u5225\u3005\u306e\u756a\u53f7\u3092\u4ed8\u3051\u307e\u3059\u3002`ORDER BY points DESC`\u3060\u3051\u3060\u3068\u3001\u540c\u70b9\u306e\u3069\u3061\u3089\u3092\u5148\u306b\u3059\u308b\u304b\u6c7a\u3081\u308b\u60c5\u5831\u304c\u8db3\u308a\u307e\u305b\u3093\u3002\u756a\u53f7\u3092\u518d\u73fe\u53ef\u80fd\u306b\u3057\u305f\u3044\u3068\u304d\u306f\u3001\u4e00\u610f\u306e`id`\u306a\u3069\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002<\/p>\n<p>\u305f\u3060\u3057\u3001`RANK()`\u3084`DENSE_RANK()`\u306b\u3082\u540c\u3058`id`\u3092\u8ffd\u52a0\u3059\u308b\u3068\u3001\u70b9\u6570\u304c\u540c\u3058\u3067\u3082SQL\u304b\u3089\u306f\u7570\u306a\u308b\u4e26\u3073\u9806\u306e\u5024\u3068\u3057\u3066\u6271\u308f\u308c\u3001\u540c\u9806\u4f4d\u306b\u306a\u308a\u307e\u305b\u3093\u3002<strong>\u540c\u70b9\u306e\u57fa\u6e96<\/strong>\u306f`points`\u3060\u3051\u306b\u3059\u308b\u306e\u304c\u4eca\u56de\u306e\u76ee\u7684\u3067\u3059\u3002<\/p>\n<blockquote>\n<p><strong>\u7528\u8a9e\u89e3\u8aac\uff1a\u540c\u9806\u4f4d\uff08peer\uff09<\/strong><br \/>`OVER (ORDER BY &#8230;)`\u306b\u6307\u5b9a\u3057\u305f\u4e26\u3079\u66ff\u3048\u5024\u304c\u540c\u3058\u884c\u306e\u3053\u3068\u3002RANK\u3068DENSE_RANK\u3067\u306f\u540c\u9806\u4f4d\u3092\u5171\u6709\u3057\u307e\u3059\u3002<\/p>\n<\/blockquote>\n<p>\uff08ROW_NUMBER\u306b\u3088\u308b\u91cd\u8907\u30fb\u4e26\u3073\u306e\u6574\u7406\u306b\u3064\u3044\u3066\u306f\u300e<a href=\"https:\/\/route-zero.com\/recruit\/route\/1713\/\">SQL\u91cd\u8907\u9664\u5916\u306e\u30d9\u30b9\u30c8\u30d7\u30e9\u30af\u30c6\u30a3\u30b9\u5fb9\u5e95\u6bd4\u8f03<\/a>\u300f\u3092\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\uff09<\/p>\n<hr>\n<h2>\u300c\u4e0a\u4f4d2\u4f4d\u300d\u3092\u62bd\u51fa\u3059\u308b\u3068\u304d\u306e\u6ce8\u610f\u70b9<\/h2>\n<p>`ROW_NUMBER()`\u306e\u300c\u5148\u982d2\u884c\u300d\u306f\u5e38\u306b2\u884c\u3067\u3059\u304c\u3001`RANK()`\u306e\u300c2\u4f4d\u4ee5\u5185\u300d\u306f\u540c\u9806\u4f4d\u3092\u542b\u3080\u305f\u3081\u3001\u5fc5\u305a\u3057\u30822\u884c\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30b5\u30f3\u30d7\u30eb\u3067\u306f90\u70b9\u306e2\u4eba\u3060\u3051\u304c2\u4f4d\u4ee5\u5185\u3067\u3059\u3002<\/p>\n<p>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u3092\u8a08\u7b97\u3057\u305f\u5217\u540d\u306f\u3001\u901a\u5e38\u540c\u3058`SELECT`\u306e`WHERE`\u3067\u76f4\u63a5\u4f7f\u3048\u307e\u305b\u3093\u3002\u7d5e\u308a\u8fbc\u3080\u5834\u5408\u306f\u3001\u3044\u3063\u305f\u3093<strong>CTE<\/strong>\uff08`WITH`\u3067\u540d\u524d\u3092\u4ed8\u3051\u305f\u4e00\u6642\u7684\u306a\u554f\u3044\u5408\u308f\u305b\u7d50\u679c\uff09\u3084\u30b5\u30d6\u30af\u30a8\u30ea\uff08SQL\u306e\u4e2d\u306b\u5165\u308c\u305f\u5225\u306e\u554f\u3044\u5408\u308f\u305b\uff09\u3067\u9806\u4f4d\u3092\u4ed8\u3051\u3001\u305d\u306e\u5916\u5074\u3067\u6761\u4ef6\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre><code>WITH scores(id, name, points) AS (\r\n  VALUES (1, &#39;\u4f50\u85e4&#39;, 90), (2, &#39;\u9234\u6728&#39;, 90),\r\n         (3, &#39;\u7530\u4e2d&#39;, 80), (4, &#39;\u9ad8\u6a4b&#39;, 70)\r\n), ranked AS (\r\n  SELECT name, points,\r\n         RANK() OVER (ORDER BY points DESC) AS rank_no\r\n  FROM scores\r\n)\r\nSELECT name, points, rank_no\r\nFROM ranked\r\nWHERE rank_no &lt;= 2\r\nORDER BY points DESC, name;<\/code><\/pre>\n<p>\u3053\u306e\u4f8b\u3067\u306f90\u70b9\u306e2\u884c\u304c\u8fd4\u308a\u307e\u3059\u3002`RANK()`\u306e\u6b21\u306f3\u4f4d\u306a\u306e\u3067\u300180\u70b9\u306f\u542b\u307e\u308c\u307e\u305b\u3093\u3002<\/p>\n<p>\uff08ROW_NUMBER\u3092\u4f7f\u3063\u305f\u6700\u65b0\u30c7\u30fc\u30bf\u53d6\u5f97\u306e\u8003\u3048\u65b9\u306b\u3064\u3044\u3066\u306f\u300e<a href=\"https:\/\/route-zero.com\/recruit\/route\/1237\/\">MySQL\u306eROW_NUMBER()\u5b9f\u8df5\u30ac\u30a4\u30c9<\/a>\u300f\u3092\u3054\u53c2\u7167\u304f\u3060\u3055\u3044\uff09<\/p>\n<h2>\u7d50\u679c\u304c\u60f3\u5b9a\u3068\u9055\u3046\u3068\u304d\u306e\u78ba\u8a8d\u9806<\/h2>\n<ol>\n<li>`OVER (ORDER BY &#8230;)`\u306b\u3001\u9806\u4f4d\u306e\u57fa\u6e96\u3068\u306a\u308b\u5217\u304c\u5165\u3063\u3066\u3044\u308b\u304b\u3002\u6700\u5f8c\u306e`ORDER BY`\u306f\u7d50\u679c\u306e\u8868\u793a\u9806\u3067\u3042\u308a\u3001\u9806\u4f4d\u8a08\u7b97\u306e\u57fa\u6e96\u3068\u306f\u5225\u3067\u3059\u3002<\/li>\n<li>\u540c\u70b9\u3092\u6b8b\u3057\u305f\u3044\u306e\u306b\u3001`RANK()`\u306e`ORDER BY`\u3078`id`\u306a\u3069\u4e00\u610f\u306e\u5217\u3092\u8ffd\u52a0\u3057\u3066\u3044\u306a\u3044\u304b\u3002<\/li>\n<li>`ROW_NUMBER()`\u3067\u540c\u70b9\u306e\u9806\u756a\u304c\u5909\u308f\u308b\u306a\u3089\u3001\u5225\u306e\u4e00\u610f\u306a\u5217\u3067\u9806\u5e8f\u3092\u78ba\u5b9a\u3067\u304d\u308b\u304b\u3002<\/li>\n<li>\u300c\u4e0a\u4f4dN\u884c\u300d\u3068\u300cN\u4f4d\u4ee5\u5185\u300d\u306f\u9055\u3046\u8981\u4ef6\u3067\u3059\u3002\u62bd\u51fa\u3057\u305f\u3044\u306e\u304c\u884c\u6570\u304b\u9806\u4f4d\u304b\u3092\u5148\u306b\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/li>\n<\/ol>\n<h3>RANK\u3068DENSE_RANK\u306f\u540c\u3058\u70b9\u6570\u306a\u3089\u5fc5\u305a\u540c\u9806\u4f4d\u3067\u3059\u304b\uff1f<\/h3>\n<p>`OVER (ORDER BY &#8230;)`\u3067\u6bd4\u8f03\u3059\u308b\u5024\u304c\u540c\u3058\u306a\u3089\u540c\u9806\u4f4d\u306b\u306a\u308a\u307e\u3059\u3002\u70b9\u6570\u4ee5\u5916\u306e\u5217\u3082\u542b\u3081\u308b\u3068\u3001\u6bd4\u8f03\u5bfe\u8c61\u306e\u7d44\u307f\u5408\u308f\u305b\u304c\u7570\u306a\u308a\u540c\u9806\u4f4d\u306b\u306a\u3089\u306a\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3>GROUP BY\u3068\u306e\u9055\u3044\u306f\u4f55\u3067\u3059\u304b\uff1f<\/h3>\n<p>GROUP BY\u306f\u901a\u5e38\u3001\u540c\u3058\u30b0\u30eb\u30fc\u30d7\u306e\u884c\u3092\u307e\u3068\u3081\u3066\u96c6\u8a08\u3057\u307e\u3059\u3002\u4eca\u56de\u306e\u9806\u4f4d\u95a2\u6570\u306f\u5143\u306e\u884c\u3092\u6b8b\u3057\u3064\u3064\u3001\u5404\u884c\u3078\u8a08\u7b97\u3057\u305f\u9806\u4f4d\u3092\u4ed8\u3051\u307e\u3059\u3002<\/p>\n<h2>\u6b21\u306e\u5b66\u7fd2\uff1a\u9806\u4f4d\u3092\u4f7f\u3046\u76ee\u7684\u3092\u5c0f\u3055\u304f\u6c7a\u3081\u308b<\/h2>\n<p>\u307e\u305a\u30b5\u30f3\u30d7\u30eb\u306e\u70b9\u6570\u309290\u300180\u300180\u300170\u306b\u5909\u3048\u3066\u30013\u5217\u306e\u9806\u4f4d\u304c\u3069\u3046\u5909\u308f\u308b\u304b\u4e88\u6e2c\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002\u305d\u306e\u5f8c\u3001\u90e8\u7f72\u3054\u3068\u306e\u9806\u4f4d\u304c\u5fc5\u8981\u306b\u306a\u3063\u305f\u3089`PARTITION BY`\u3092\u5b66\u3076\u3068\u3001\u540c\u3058\u8003\u3048\u65b9\u3092\u30b0\u30eb\u30fc\u30d7\u5358\u4f4d\u3078\u5e83\u3052\u3089\u308c\u307e\u3059\u3002<\/p>\n<p>\u4e00\u6b21\u8cc7\u6599\uff1a<a href=\"https:\/\/www.postgresql.org\/docs\/18\/functions-window.html\" target=\"_blank\" rel=\"noopener noreferrer\">PostgreSQL\u516c\u5f0f\u300cWindow Functions\u300d<\/a><\/p>\n<p>\u3069\u306e\u9806\u4f4d\u3092\u6b8b\u3057\u305f\u3044\u304b\u3092\u8a00\u8449\u306b\u3057\u3066\u304b\u3089\u3001SQL\u30921\u884c\u305a\u3064\u5909\u3048\u3066\u8a66\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n","protected":false},"featured_media":6550,"template":"","_links":{"self":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route\/6546"}],"collection":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/route"}],"about":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/types\/route"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media\/6550"}],"wp:attachment":[{"href":"https:\/\/route-zero.com\/recruit\/wp-json\/wp\/v2\/media?parent=6546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}