{"id":6070,"date":"2025-09-28T00:51:36","date_gmt":"2025-09-27T15:51:36","guid":{"rendered":"https:\/\/sky-peace.net\/?p=6070"},"modified":"2025-09-28T00:51:36","modified_gmt":"2025-09-27T15:51:36","slug":"sql-lateral-join","status":"publish","type":"post","link":"https:\/\/sky-peace.net\/certified\/sql-lateral-join\/","title":{"rendered":"LATERAL JOIN\u3068APPLY\u53e5\u3092\u5b8c\u5168\u5236\u8987\uff01DB\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u5348\u5f8c\u8a66\u9a13\u306e\u300c\u30c8\u30c3\u30d7N\u554f\u984c\u300dSQL\u653b\u7565\u6cd5"},"content":{"rendered":"<p><!DOCTYPE html><br \/>\n<html lang=\"ja\"><br \/>\n<head><br \/>\n    <meta charset=\"UTF-8\"><br \/>\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><br \/>\n    <title>LATERAL JOIN\u3068APPLY\u53e5\u306e\u89e3\u8aac\u2502\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u5bfe\u7b56<\/title><\/p>\n<style>\n        body { font-family: sans-serif; line-height: 1.6; }\n        h2, h3 { border-left: 4px solid #005ab3; padding-left: 8px; }\n        code { background-color: #f4f4f4; padding: 2px 4px; border-radius: 3px; }\n        pre > code { display: block; background-color: #333; color: #fff; padding: 15px; border-radius: 5px; }\n        table { border-collapse: collapse; width: 80%; margin: 20px 0; }\n        th, td { border: 1px solid #ddd; padding: 8px; text-align: left; }\n        th { background-color: #f2f2f2; }\n        strong { color: #d9534f; }\n    <\/style>\n<p><\/head><br \/>\n<body><\/p>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u3084\u5fdc\u7528\u60c5\u5831\u6280\u8853\u8005\u8a66\u9a13\u306e\u5b66\u7fd2\u3067\u767b\u5834\u3059\u308b\u300cLATERAL JOIN\u300d\u3084\u300cAPPLY \u53e5\u300d\u3002\u53c2\u8003\u66f8\u3092\u8aad\u3093\u3067\u3082\u300c\u5404\u884c\u306b\u95a2\u6570\u306e\u3088\u3046\u306b\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u9069\u7528\u3059\u308b\u300d\u3068\u3044\u3046\u8aac\u660e\u3060\u3051\u3067\u306f\u3001\u5177\u4f53\u7684\u306a\u30a4\u30e1\u30fc\u30b8\u304c\u6e67\u304d\u306b\u304f\u3044\u3068\u611f\u3058\u3066\u3044\u307e\u305b\u3093\u304b\uff1f\u300c\u901a\u5e38\u306e JOIN \u3084\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u3068\u4f55\u304c\u9055\u3046\u306e\uff1f\u300d\u300c\u3069\u3093\u306a\u5834\u9762\u3067\u4f7f\u3046\u3068\u4fbf\u5229\u306a\u306e\uff1f\u300d\u3068\u3044\u3063\u305f\u7591\u554f\u306f\u3001\u591a\u304f\u306e\u5b66\u7fd2\u8005\u304c\u62b1\u3048\u308b\u5171\u901a\u306e\u60a9\u307f\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u3089\u306e\u69cb\u6587\u306f\u3001SQL \u306e\u8868\u73fe\u529b\u3092\u98db\u8e8d\u7684\u306b\u5411\u4e0a\u3055\u305b\u308b\u5f37\u529b\u306a\u6b66\u5668\u3067\u3059\u304c\u3001\u305d\u306e\u6982\u5ff5\u3092\u63b4\u3080\u306b\u306f\u5c11\u3057\u30b3\u30c4\u304c\u3044\u308a\u307e\u3059\u3002\u672c\u8cea\u306f<strong>\u300cFROM \u53e5\u3067\u6307\u5b9a\u3057\u305f\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u3092 1 \u884c\u305a\u3064\u53d6\u308a\u51fa\u3057\u3001\u305d\u306e\u884c\u306e\u5024\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3059\u308b\u300d<\/strong>\u3068\u3044\u3046\u30eb\u30fc\u30d7\u51e6\u7406\u306e\u3088\u3046\u306a\u30a4\u30e1\u30fc\u30b8\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001LATERAL JOIN \u3068 APPLY \u53e5\u304c\u6301\u3064\u3053\u306e\u300c\u30eb\u30fc\u30d7\u51e6\u7406\u300d\u306e\u30a4\u30e1\u30fc\u30b8\u3092\u3001\u8c4a\u5bcc\u306a\u56f3\u89e3\u3068\u5177\u4f53\u7684\u306a SQL \u3092\u4f7f\u3063\u3066\u5fb9\u5e95\u7684\u306b\u53ef\u8996\u5316\u3057\u307e\u3059\u3002\u8aad\u307f\u7d42\u3048\u308b\u9803\u306b\u306f\u3001\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u62bd\u51fa\u8981\u4ef6\u306b\u3082\u30a8\u30ec\u30ac\u30f3\u30c8\u306b\u5bfe\u5fdc\u3067\u304d\u308b SQL \u306e\u65b0\u305f\u306a\u6249\u304c\u958b\u304b\u308c\u3066\u3044\u308b\u306f\u305a\u3067\u3059\u3002\u5348\u5f8c\u8a66\u9a13\u306e\u96e3\u89e3\u306a\u554f\u3044\u5408\u308f\u305b\u306f\u3082\u3061\u308d\u3093\u3001\u5b9f\u52d9\u3067\u306e\u30c7\u30fc\u30bf\u5206\u6790\u3084\u30ec\u30dd\u30fc\u30c6\u30a3\u30f3\u30b0\u696d\u52d9\u306e\u52b9\u7387\u5316\u306b\u3082\u76f4\u7d50\u3059\u308b\u3053\u306e\u5f37\u529b\u306a\u6a5f\u80fd\u3092\u3001\u305c\u3072\u30de\u30b9\u30bf\u30fc\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<hr>\n<h2>LATERAL JOIN \/ APPLY\u53e5\u304c\u5fc5\u8981\u306a\u7406\u7531\u2502\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u9650\u754c\u3092\u8d85\u3048\u308b\u5348\u5f8c\u8a66\u9a13\u5bfe\u7b56<\/h2>\n<p>SQL\u3092\u5b66\u7fd2\u3059\u308b\u3068\u3001<code>INNER JOIN<\/code>\u3084<code>LEFT JOIN<\/code>\u3001\u305d\u3057\u3066<code>GROUP BY<\/code>\u3068\u3044\u3063\u305f\u5f37\u529b\u306a\u9053\u5177\u3092\u624b\u306b\u3057\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u5b9f\u52d9\u3084\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u306e\u5348\u5f8c\u554f\u984c\u3067\u306f\u3001\u3053\u308c\u3089\u3060\u3051\u3067\u306f\u30a8\u30ec\u30ac\u30f3\u30c8\u306b\u89e3\u6c7a\u3067\u304d\u306a\u3044\u8907\u96d1\u306a\u8981\u4ef6\u306b\u906d\u9047\u3057\u307e\u3059\u3002\u305d\u308c\u304c<strong>\u300c\u30b0\u30eb\u30fc\u30d7\u3054\u3068\u306e\u4e0a\u4f4d N \u4ef6\u3092\u53d6\u5f97\u3057\u305f\u3044\u300d<\/strong>\u3068\u3044\u3046\u5178\u578b\u7684\u306a\u30b7\u30ca\u30ea\u30aa\u3067\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u300c\u5404\u90e8\u7f72\u306b\u6240\u5c5e\u3059\u308b\u793e\u54e1\u306e\u3046\u3061\u3001\u7d66\u4e0e\u304c\u9ad8\u3044\u30c8\u30c3\u30d7 3 \u540d\u306e\u60c5\u5831\u3092\u3059\u3079\u3066\u53d6\u5f97\u3057\u305f\u3044\u300d\u3068\u8003\u3048\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>\u901a\u5e38\u306eJOIN\u3068GROUP BY\u306e\u9650\u754c<\/h3>\n<p><code>GROUP BY<\/code>\u3092\u4f7f\u3048\u3070\u3001\u300c\u5404\u90e8\u7f72\u306e\u6700\u9ad8\u7d66\u4e0e\u984d\u300d\u3092\u6c42\u3081\u308b\u3053\u3068\u306f\u7c21\u5358\u3067\u3059\u3002<\/p>\n<pre><code>SELECT\n  department_id,\n  MAX(salary) AS max_salary\nFROM\n  employees\nGROUP BY\n  department_id;<\/code><\/pre>\n<p>\u3057\u304b\u3057\u3001\u3053\u306e\u7d50\u679c\u306f\u300c\u8ab0\u304c\u300d\u305d\u306e\u7d66\u4e0e\u3092\u53d7\u3051\u53d6\u3063\u3066\u3044\u308b\u306e\u304b\u5206\u304b\u308a\u307e\u305b\u3093\u3002<code>employee_id<\/code>\u3084<code>employee_name<\/code>\u3092\u8ffd\u52a0\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001<code>GROUP BY<\/code>\u53e5\u306b\u3082\u8ffd\u52a0\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u3001\u610f\u56f3\u3057\u305f\u7d50\u679c\u3068\u306f\u7570\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<code>JOIN<\/code>\u3092\u99c6\u4f7f\u3057\u3066\u81ea\u5df1\u7d50\u5408\u3092\u7e70\u308a\u8fd4\u3057\u305f\u308a\u3001\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570 (<code>ROW_NUMBER()<\/code>) \u3092\u4f7f\u3063\u305f\u308a\u3059\u308b\u65b9\u6cd5\u3082\u3042\u308a\u307e\u3059\u304c\u3001SQL\u304c\u8907\u96d1\u306b\u306a\u308a\u304c\u3061\u3067\u3059\u3002<\/p>\n<h3>\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u9650\u754c<\/h3>\n<p>\u300c\u3058\u3083\u3042\u3001\u884c\u3054\u3068\u306b\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3059\u308b\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306a\u3089\u3069\u3046\u3060\u308d\u3046\uff1f\u300d\u3068\u8003\u3048\u308b\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u3002\u78ba\u304b\u306b\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306f\u4fbf\u5229\u3067\u3059\u304c\u3001\u6c7a\u5b9a\u7684\u306a\u5236\u7d04\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<ol>\n<li><strong>\u5358\u4e00\u306e\u5024\u3057\u304b\u8fd4\u305b\u306a\u3044\uff08\u30b9\u30ab\u30e9\u30fb\u30b5\u30d6\u30af\u30a8\u30ea\uff09<\/strong>: <code>SELECT<\/code>\u53e5\u306b\u66f8\u304f\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306f\u3001\u7d50\u679c\u304c 1 \u884c 1 \u5217\u306e\u5024\u3067\u306a\u3051\u308c\u3070\u30a8\u30e9\u30fc\u306b\u306a\u308a\u307e\u3059\u3002\u300c\u30c8\u30c3\u30d7 3 \u540d\u300d\u306e\u3088\u3046\u306a\u8907\u6570\u884c\u306e\u7d50\u679c\u3092\u8fd4\u3059\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/li>\n<li><strong>FROM\u53e5\u3067\u4f7f\u3048\u306a\u3044<\/strong>: \u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306f<code>SELECT<\/code>\u53e5\u3084<code>WHERE<\/code>\u53e5\u3067\u4e3b\u554f\u3044\u5408\u308f\u305b\u306e\u5217\u3092\u53c2\u7167\u3057\u307e\u3059\u304c\u3001<code>FROM<\/code>\u53e5\u306e\u4e2d\u3067\u4e3b\u554f\u3044\u5408\u308f\u305b\u306e\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u5217\u3092\u53c2\u7167\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/li>\n<\/ol>\n<p>\u3053\u306e\u3088\u3046\u306b\u3001\u65e2\u5b58\u306e\u6a5f\u80fd\u3060\u3051\u3067\u306f\u300c<strong>\u4e3b\u554f\u3044\u5408\u308f\u305b\u306e\u5404\u884c\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u3001\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u3084\u95a2\u6570\u3092\u547c\u3073\u51fa\u3057\u3001\u305d\u306e\u8907\u6570\u884c\u30fb\u8907\u6570\u5217\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u3092\u7d50\u5408\u3059\u308b<\/strong>\u300d\u3068\u3044\u3046\u51e6\u7406\u3092\u76f4\u611f\u7684\u306b\u66f8\u304f\u3053\u3068\u304c\u56f0\u96e3\u3067\u3057\u305f\u3002<\/p>\n<p>\u3053\u306e\u8ab2\u984c\u3092\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306b\u767b\u5834\u3057\u305f\u306e\u304c\u3001<code>LATERAL JOIN<\/code>\uff08PostgreSQL, Oracle\u306a\u3069\uff09\u3068<code>APPLY<\/code>\u53e5\uff08SQL Server, Oracle\u306a\u3069\uff09\u306a\u306e\u3067\u3059\u3002\u3053\u308c\u3089\u306f\u3001<code>FROM<\/code>\u53e5\u306e\u4e2d\u3067\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u3088\u3046\u306b\u632f\u308b\u821e\u3046\u3053\u3068\u3092\u53ef\u80fd\u306b\u3057\u3001SQL\u306e\u8868\u73fe\u529b\u3092\u5927\u304d\u304f\u5e83\u3052\u307e\u3057\u305f\u3002\u307e\u3055\u306b\u300c\u5404\u884c\u306b\u95a2\u6570\u306e\u3088\u3046\u306b\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u9069\u7528\u3059\u308b\u300d\u305f\u3081\u306e\u6a5f\u80fd\u3068\u8a00\u3048\u308b\u3067\u3057\u3087\u3046\u3002<\/p>\n<hr>\n<h2>LATERAL JOIN\u306e\u4ed5\u7d44\u307f\u3068\u69cb\u6587\u2502\u56f3\u89e3\u3067\u5b66\u3076\u300c\u884c\u3054\u3068\u30eb\u30fc\u30d7\u51e6\u7406\u300d\u306eSQL<\/h2>\n<p><code>LATERAL JOIN<\/code>\u306e\u52d5\u304d\u3092\u7406\u89e3\u3059\u308b\u6700\u826f\u306e\u65b9\u6cd5\u306f\u3001\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u8a00\u8a9e\u306b\u304a\u3051\u308b\u300cFor-Each \u30eb\u30fc\u30d7\u300d\u3092\u30a4\u30e1\u30fc\u30b8\u3059\u308b\u3053\u3068\u3067\u3059\u3002\u3064\u307e\u308a\u3001<strong>\u30c6\u30fc\u30d6\u30eb A \u306e\u5404\u884c\u306b\u5bfe\u3057\u3066\u3001\u305d\u306e\u884c\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u3063\u3066\u30c6\u30fc\u30d6\u30eb B \u306b\u5bfe\u3059\u308b\u554f\u3044\u5408\u308f\u305b\uff08\u30b5\u30d6\u30af\u30a8\u30ea\uff09\u3092\u5b9f\u884c\u3057\u3001\u7d50\u679c\u3092\u7d50\u5408\u3059\u308b<\/strong>\u3068\u3044\u3046\u51e6\u7406\u3092\u7e70\u308a\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<p>\u8a00\u8449\u3060\u3051\u3067\u306f\u5206\u304b\u308a\u306b\u304f\u3044\u306e\u3067\u3001\u5177\u4f53\u7684\u306a\u30c6\u30fc\u30d6\u30eb\u3068 SQL \u3067\u51e6\u7406\u30d5\u30ed\u30fc\u3092\u8ffd\u3063\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>\u30b5\u30f3\u30d7\u30eb\u30c6\u30fc\u30d6\u30eb<\/h3>\n<p>\u3053\u3053\u3067\u306f\u3001\u30b7\u30f3\u30d7\u30eb\u306a\u300c\u90e8\u7f72\u300d\u30c6\u30fc\u30d6\u30eb\u3068\u300c\u5f93\u696d\u54e1\u300d\u30c6\u30fc\u30d6\u30eb\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<p><strong><code>departments<\/code> \u30c6\u30fc\u30d6\u30eb<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>department_id<\/th>\n<th>department_name<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>\u55b6\u696d\u90e8<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u958b\u767a\u90e8<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>\u4eba\u4e8b\u90e8<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><strong><code>employees<\/code> \u30c6\u30fc\u30d6\u30eb<\/strong><\/p>\n<table>\n<thead>\n<tr>\n<th>employee_id<\/th>\n<th>employee_name<\/th>\n<th>department_id<\/th>\n<th>salary<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>101<\/td>\n<td>\u7530\u4e2d<\/td>\n<td>1<\/td>\n<td>800<\/td>\n<\/tr>\n<tr>\n<td>102<\/td>\n<td>\u9234\u6728<\/td>\n<td>1<\/td>\n<td>750<\/td>\n<\/tr>\n<tr>\n<td>103<\/td>\n<td>\u4f50\u85e4<\/td>\n<td>1<\/td>\n<td>780<\/td>\n<\/tr>\n<tr>\n<td>104<\/td>\n<td>\u9ad8\u6a4b<\/td>\n<td>1<\/td>\n<td>700<\/td>\n<\/tr>\n<tr>\n<td>201<\/td>\n<td>\u4f0a\u85e4<\/td>\n<td>2<\/td>\n<td>900<\/td>\n<\/tr>\n<tr>\n<td>202<\/td>\n<td>\u6e21\u8fba<\/td>\n<td>2<\/td>\n<td>850<\/td>\n<\/tr>\n<tr>\n<td>203<\/td>\n<td>\u5c71\u672c<\/td>\n<td>2<\/td>\n<td>920<\/td>\n<\/tr>\n<tr>\n<td>301<\/td>\n<td>\u4e2d\u6751<\/td>\n<td>3<\/td>\n<td>600<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u300c\u5404\u90e8\u7f72\u306e\u7d66\u4e0e\u30c8\u30c3\u30d72\u793e\u54e1\u300d\u3092\u53d6\u5f97\u3059\u308bSQL<\/h3>\n<p>\u3053\u306e\u30c7\u30fc\u30bf\u304b\u3089\u300c\u5404\u90e8\u7f72\u306e\u7d66\u4e0e\u30c8\u30c3\u30d7 2 \u540d\u300d\u3092\u53d6\u5f97\u3059\u308b SQL (PostgreSQL, Oracle \u69cb\u6587) \u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code>SELECT\n  d.department_name,\n  e_top.employee_name,\n  e_top.salary\nFROM\n  departments AS d, -- Oracle\u306e\u5834\u5408\u306f\u30ab\u30f3\u30de\u533a\u5207\u308a\u3082\u53ef\n  LATERAL (\n    SELECT\n      e.employee_name,\n      e.salary\n    FROM\n      employees AS e\n    WHERE\n      e.department_id = d.department_id -- \u2605\u5916\u5074\u306ed.department_id\u3092\u53c2\u7167\n    ORDER BY\n      e.salary DESC\n    LIMIT 2 -- PostgreSQL\u306e\u5834\u5408\u3002Oracle\u3067\u306f FETCH FIRST 2 ROWS ONLY\n  ) AS e_top;<\/code><\/pre>\n<p><em>\u203b\u793e\u54e1\u304c\u4e00\u4eba\u3082\u3044\u306a\u3044\u90e8\u7f72\u3082\u8868\u793a\u3057\u305f\u3044\u5834\u5408\u306f <code>LEFT JOIN LATERAL (...) ON true;<\/code> \u3068\u3057\u307e\u3059\u3002<\/em><\/p>\n<h3>\u56f3\u89e3\uff1aLATERAL JOIN\u306e\u5185\u90e8\u7684\u306a\u51e6\u7406\u30d5\u30ed\u30fc<\/h3>\n<p>\u3053\u306e SQL \u304c\u3069\u306e\u3088\u3046\u306b\u5b9f\u884c\u3055\u308c\u308b\u304b\u3092\u3001\u56f3\u3068\u5171\u306b\u30b9\u30c6\u30c3\u30d7\u30fb\u30d0\u30a4\u30fb\u30b9\u30c6\u30c3\u30d7\u3067\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u3010\u30eb\u30fc\u30d7\u958b\u59cb\u3011<\/strong> \u307e\u305a\u3001\u4e3b\u554f\u3044\u5408\u308f\u305b\u306e <code>FROM departments AS d<\/code> \u304c\u8a55\u4fa1\u3055\u308c\u3001<code>departments<\/code> \u30c6\u30fc\u30d6\u30eb\u306e <strong>1\u884c\u76ee<\/strong>\uff08<code>department_id=1<\/code>, \u55b6\u696d\u90e8\uff09\u304c\u53d6\u308a\u51fa\u3055\u308c\u307e\u3059\u3002<\/li>\n<li><strong>\u3010\u30b5\u30d6\u30af\u30a8\u30ea\u5b9f\u884c\u3011<\/strong> \u6b21\u306b <code>LATERAL<\/code> \u53e5\u306e\u4e2d\u306e\u30b5\u30d6\u30af\u30a8\u30ea\u304c\u5b9f\u884c\u3055\u308c\u307e\u3059\u3002\u3053\u306e\u3068\u304d\u3001<strong>1.\u3067\u53d6\u308a\u51fa\u3055\u308c\u305f <code>d.department_id<\/code> \u306e\u5024\uff08<code>1<\/code>\uff09<\/strong>\u304c\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u5185\u306e <code>WHERE e.department_id = d.department_id<\/code> \u306e <code>d.department_id<\/code> \u306b\u5f15\u304d\u6e21\u3055\u308c\u307e\u3059\u3002<\/li>\n<li><strong>\u3010\u7d50\u679c\u53d6\u5f97\u3011<\/strong> \u30b5\u30d6\u30af\u30a8\u30ea\u306f\u5b9f\u8cea\u7684\u306b\u300c\u55b6\u696d\u90e8 (<code>department_id=1<\/code>) \u306e\u793e\u54e1\u3092\u7d66\u4e0e\u306e\u9ad8\u3044\u9806\u306b 2 \u540d\u53d6\u5f97\u3059\u308b\u300d\u3068\u3044\u3046\u51e6\u7406\u306b\u306a\u308a\u3001<code>\u7530\u4e2d (800)<\/code> \u3068 <code>\u4f50\u85e4 (780)<\/code> \u306e 2 \u884c\u304c\u7d50\u679c\u3068\u3057\u3066\u8fd4\u3055\u308c\u307e\u3059\u3002<\/li>\n<li><strong>\u3010\u7d50\u679c\u306e\u7d50\u5408\u3011<\/strong> <code>departments<\/code> \u306e 1 \u884c\u76ee\uff08\u55b6\u696d\u90e8\uff09\u3068\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u7d50\u679c 2 \u884c\u304c\u7d50\u5408\u3055\u308c\u3001\u6700\u7d42\u7684\u306a\u7d50\u679c\u30bb\u30c3\u30c8\u306b 2 \u884c\u304c\u8ffd\u52a0\u3055\u308c\u307e\u3059\u3002<\/li>\n<li><strong>\u3010\u6b21\u306e\u30eb\u30fc\u30d7\u3078\u3011<\/strong> <code>departments<\/code> \u30c6\u30fc\u30d6\u30eb\u306e <strong>2\u884c\u76ee<\/strong>\uff08<code>department_id=2<\/code>, \u958b\u767a\u90e8\uff09\u304c\u53d6\u308a\u51fa\u3055\u308c\u3001\u518d\u3073 <strong>2.\uff5e4.<\/strong> \u306e\u51e6\u7406\u304c\u7e70\u308a\u8fd4\u3055\u308c\u307e\u3059\u3002\u958b\u767a\u90e8\u306e\u5834\u5408\u306f <code>\u5c71\u672c (920)<\/code> \u3068 <code>\u4f0a\u85e4 (900)<\/code> \u304c\u53d6\u5f97\u30fb\u7d50\u5408\u3055\u308c\u307e\u3059\u3002<\/li>\n<li><strong>\u3010\u30eb\u30fc\u30d7\u7d42\u4e86\u3011<\/strong> <code>departments<\/code> \u30c6\u30fc\u30d6\u30eb\u306e\u3059\u3079\u3066\u306e\u884c\u306b\u3064\u3044\u3066\u3053\u306e\u51e6\u7406\u304c\u5b8c\u4e86\u3059\u308b\u307e\u3067\u7e70\u308a\u8fd4\u3055\u308c\u307e\u3059\u3002<\/li>\n<\/ol>\n<p>\u3053\u306e\u3088\u3046\u306b <code>LATERAL<\/code> \u3068\u3044\u3046\u30ad\u30fc\u30ef\u30fc\u30c9\u306f\u3001\u300c\uff08\u4e3b\u554f\u3044\u5408\u308f\u305b\u306e\uff09\u6a2a\u3067\u3001\u884c\u3054\u3068\u306b\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u90fd\u5ea6\u5b9f\u884c\u3059\u308b\u300d\u3068\u3044\u3046\u30a4\u30e1\u30fc\u30b8\u3092\u6301\u3064\u3068\u3001\u305d\u306e\u5f37\u529b\u306a\u6a5f\u80fd\u304c\u7406\u89e3\u3057\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><\/body><br \/>\n<\/html><\/p>\n<hr>\n<h2>APPLY\u53e5\u306e\u4f7f\u3044\u65b9 (SQL Server)\u2502CROSS APPLY\u3068OUTER APPLY\u3092\u5fb9\u5e95\u89e3\u8aac<\/h2>\n<p>PostgreSQL \u3084 Oracle \u3067 <code>LATERAL JOIN<\/code> \u304c\u5c0e\u5165\u3055\u308c\u308b\u524d\u304b\u3089\u3001SQL Server \u306b\u306f\u540c\u69d8\u306e\u6a5f\u80fd\u3092\u5b9f\u73fe\u3059\u308b <code>APPLY<\/code> \u53e5\u304c\u5b58\u5728\u3057\u307e\u3057\u305f\u3002<code>APPLY<\/code>\u53e5\u306f\u3001<code>LATERAL JOIN<\/code> \u3068\u540c\u3058\u304f\u300c\u884c\u3054\u3068\u306b\u95a2\u6570\u3084\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u9069\u7528\u3059\u308b\u300d\u3068\u3044\u3046\u5f37\u529b\u306a\u6a5f\u80fd\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/p>\n<p><code>APPLY<\/code> \u306b\u306f <code>CROSS APPLY<\/code> \u3068 <code>OUTER APPLY<\/code> \u306e 2 \u7a2e\u985e\u304c\u3042\u308a\u3001\u305d\u308c\u305e\u308c\u901a\u5e38\u306e <code>JOIN<\/code> \u306b\u304a\u3051\u308b <code>INNER JOIN<\/code> \u3068 <code>LEFT OUTER JOIN<\/code> \u306e\u95a2\u4fc2\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002<\/p>\n<h3>CROSS APPLY\uff1aINNER JOIN\u306e\u3088\u3046\u306b\u632f\u308b\u821e\u3046<\/h3>\n<p><code>CROSS APPLY<\/code> \u306f\u3001<code>INNER JOIN<\/code> \u3084 <code>JOIN LATERAL<\/code> \u3068\u540c\u3058\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u5de6\u5074\uff08\u4e3b\uff09\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u5404\u884c\u306b\u5bfe\u3057\u3066\u53f3\u5074\u306e\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u8a55\u4fa1\u3057\u3001<strong>\u30b5\u30d6\u30af\u30a8\u30ea\u304c 1 \u884c\u4ee5\u4e0a\u306e\u7d50\u679c\u3092\u8fd4\u3057\u305f\u5834\u5408\u306b\u306e\u307f<\/strong>\u3001\u305d\u306e\u884c\u3092\u7d50\u679c\u30bb\u30c3\u30c8\u306b\u542b\u3081\u307e\u3059\u3002<\/p>\n<p>\u5148\u306e\u300c\u5404\u90e8\u7f72\u306e\u7d66\u4e0e\u30c8\u30c3\u30d72\u793e\u54e1\u300d\u306e\u4f8b\u3092 SQL Server \u306e\u69cb\u6587\u3067\u66f8\u304f\u3068\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><code>SELECT\n  d.department_name,\n  e_top.employee_name,\n  e_top.salary\nFROM\n  departments AS d\nCROSS APPLY (\n  SELECT\n    e.employee_name,\n    e.salary\n  FROM\n    employees AS e\n  WHERE\n    e.department_id = d.department_id\n  ORDER BY\n    e.salary DESC\n  OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY -- SQL Server 2012\u4ee5\u964d\n) AS e_top;<\/code><\/pre>\n<p>\u3082\u3057\u3001\u6240\u5c5e\u3059\u308b\u793e\u54e1\u304c\u4e00\u4eba\u3082\u3044\u306a\u3044\u90e8\u7f72\uff08\u4f8b\u3048\u3070\u65b0\u8a2d\u3055\u308c\u305f\u300c\u7dcf\u52d9\u90e8\u300d\uff09\u304c\u3042\u3063\u305f\u5834\u5408\u3001\u305d\u306e\u90e8\u7f72\u306f\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u7d50\u679c\u304c 0 \u884c\u3068\u306a\u308b\u305f\u3081\u3001<code>CROSS APPLY<\/code> \u3067\u306f\u6700\u7d42\u7684\u306a\u7d50\u679c\u304b\u3089\u9664\u5916\u3055\u308c\u307e\u3059\u3002<\/p>\n<\/p>\n<h3>OUTER APPLY\uff1aLEFT JOIN\u306e\u3088\u3046\u306b\u632f\u308b\u821e\u3046<\/h3>\n<p>\u4e00\u65b9\u3001<code>OUTER APPLY<\/code> \u306f <code>LEFT OUTER JOIN<\/code> \u3084 <code>LEFT JOIN LATERAL<\/code> \u3068\u540c\u3058\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002<code>CROSS APPLY<\/code> \u3068\u306e\u6c7a\u5b9a\u7684\u306a\u9055\u3044\u306f\u3001<strong>\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u7d50\u679c\u304c 0 \u884c\u3060\u3063\u305f\u3068\u3057\u3066\u3082\u3001\u5de6\u5074\uff08\u4e3b\uff09\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u306f\u7d50\u679c\u30bb\u30c3\u30c8\u306b\u6b8b\u308b<\/strong>\u70b9\u3067\u3059\u3002\u305d\u306e\u969b\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u304b\u3089\u8fd4\u3055\u308c\u308b\u306f\u305a\u3060\u3063\u305f\u5217\u306f <code>NULL<\/code> \u3067\u88dc\u5b8c\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u5148\u307b\u3069\u306e\u30af\u30a8\u30ea\u3092 <code>OUTER APPLY<\/code> \u306b\u5909\u66f4\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code>SELECT\n  d.department_name,\n  e_top.employee_name,\n  e_top.salary\nFROM\n  departments AS d\nOUTER APPLY ( -- \u3053\u3053\u3092\u5909\u66f4\n  SELECT\n    e.employee_name,\n    e.salary\n  FROM\n    employees AS e\n  WHERE\n    e.department_id = d.department_id\n  ORDER BY\n    e.salary DESC\n  OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY\n) AS e_top;<\/code><\/pre>\n<p>\u3053\u306e\u30af\u30a8\u30ea\u3067\u3042\u308c\u3070\u3001\u793e\u54e1\u304c\u4e00\u4eba\u3082\u3044\u306a\u3044\u300c\u7dcf\u52d9\u90e8\u300d\u3082\u7d50\u679c\u306b\u8868\u793a\u3055\u308c\u3001<code>employee_name<\/code> \u3068 <code>salary<\/code> \u306e\u5217\u306b\u306f <code>NULL<\/code> \u304c\u5165\u308a\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u300c\u5168\u3007\u3007\u306e\u4e00\u89a7\u3092\u51fa\u3057\u3064\u3064\u3001\u95a2\u9023\u3059\u308b\u4e0a\u4f4d N \u4ef6\u306e\u60c5\u5831\u3082\u3042\u308c\u3070\u8868\u793a\u3057\u305f\u3044\u300d\u3068\u3044\u3063\u305f\u8981\u4ef6\u3067\u975e\u5e38\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n<h3>APPLY \u3068 LATERAL JOIN \u306e\u95a2\u4fc2\u307e\u3068\u3081<\/h3>\n<p>\u3053\u308c\u3089\u306e\u95a2\u4fc2\u6027\u3092\u8868\u306b\u307e\u3068\u3081\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u69cb\u6587<\/th>\n<th>\u52d5\u4f5c\u306e\u30a4\u30e1\u30fc\u30b8<\/th>\n<th>\u30b5\u30d6\u30af\u30a8\u30ea\u304b\u30890\u4ef6\u306e\u5834\u5408\u306e\u6319\u52d5<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>JOIN LATERAL<\/code><\/td>\n<td><code>INNER JOIN<\/code> \u76f8\u5f53<\/td>\n<td>\u4e3b\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u3082\u7d50\u679c\u304b\u3089<strong>\u9664\u5916\u3055\u308c\u308b<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>LEFT JOIN LATERAL<\/code><\/td>\n<td><code>LEFT JOIN<\/code> \u76f8\u5f53<\/td>\n<td>\u4e3b\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u306f <code>NULL<\/code> \u57cb\u3081\u3067<strong>\u6b8b\u308b<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>CROSS APPLY<\/code><\/td>\n<td><code>INNER JOIN<\/code> \u76f8\u5f53<\/td>\n<td>\u4e3b\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u3082\u7d50\u679c\u304b\u3089<strong>\u9664\u5916\u3055\u308c\u308b<\/strong><\/td>\n<\/tr>\n<tr>\n<td><code>OUTER APPLY<\/code><\/td>\n<td><code>LEFT JOIN<\/code> \u76f8\u5f53<\/td>\n<td>\u4e3b\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u306f <code>NULL<\/code> \u57cb\u3081\u3067<strong>\u6b8b\u308b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u57fa\u672c\u7684\u306b\u306f\u3001\u6a19\u6e96SQL\u6e96\u62e0\u306e <code>LATERAL JOIN<\/code> \u3068\u3001SQL Server\u72ec\u81ea\u306e <code>APPLY<\/code> \u306f\u307b\u307c\u540c\u3058\u6a5f\u80fd\u3092\u63d0\u4f9b\u3057\u3066\u3044\u308b\u3068\u7406\u89e3\u3057\u3066\u554f\u984c\u3042\u308a\u307e\u305b\u3093\u3002\u3069\u3061\u3089\u306e\u69cb\u6587\u306b\u89e6\u308c\u308b\u6a5f\u4f1a\u304c\u591a\u3044\u304b\u306f\u3001\u666e\u6bb5\u5229\u7528\u3057\u3066\u3044\u308b\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u88fd\u54c1\u306b\u3088\u308a\u307e\u3059\u3002<\/p>\n<hr>\n<h2>\u5b9f\u8df5\u7684\u306a\u6d3b\u7528\u30b7\u30ca\u30ea\u30aa\u3068DB\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u5348\u5f8c\u8a66\u9a13\u5bfe\u7b56<\/h2>\n<p><code>LATERAL JOIN<\/code> \u3084 <code>APPLY<\/code> \u306f\u3001\u5358\u306a\u308bSQL\u306e\u30c6\u30af\u30cb\u30c3\u30af\u306b\u7559\u307e\u3089\u305a\u3001\u5b9f\u969b\u306e\u696d\u52d9\u30b7\u30b9\u30c6\u30e0\u3084\u8a66\u9a13\u554f\u984c\u3067\u983b\u51fa\u3059\u308b\u300c\u30c8\u30c3\u30d7N\u554f\u984c\u300d\u3092\u30a8\u30ec\u30ac\u30f3\u30c8\u306b\u89e3\u6c7a\u3059\u308b\u305f\u3081\u306e\u5f37\u529b\u306a\u601d\u8003\u30c4\u30fc\u30eb\u3067\u3059\u3002\u3053\u3053\u3067\u306f\u3001\u3088\u308a\u5177\u4f53\u7684\u306a\u30b7\u30ca\u30ea\u30aa\u3092\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>\u30b7\u30ca\u30ea\u30aa1\uff1aEC\u30b5\u30a4\u30c8 &#8211; \u30ab\u30c6\u30b4\u30ea\u3054\u3068\u306e\u6700\u65b0\u30ec\u30d3\u30e5\u30fc\u30923\u4ef6\u53d6\u5f97<\/h3>\n<p><strong>\u8981\u4ef6<\/strong>: Web\u30b5\u30a4\u30c8\u306b\u5546\u54c1\u30ab\u30c6\u30b4\u30ea\u306e\u4e00\u89a7\u3092\u8868\u793a\u3057\u3001\u5404\u30ab\u30c6\u30b4\u30ea\u306b\u3064\u3044\u3066\u30e6\u30fc\u30b6\u30fc\u304b\u3089\u6295\u7a3f\u3055\u308c\u305f\u6700\u65b0\u306e\u30ec\u30d3\u30e5\u30fc\u30923\u4ef6\u305a\u3064\u8868\u793a\u3057\u305f\u3044\u3002<\/p>\n<p>\u3053\u306e\u8981\u4ef6\u3067\u306f\u3001\u30ec\u30d3\u30e5\u30fc\u304c\u307e\u3060\u6295\u7a3f\u3055\u308c\u3066\u3044\u306a\u3044\u30ab\u30c6\u30b4\u30ea\u3082\u8868\u793a\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u305f\u3081\u3001<code>LEFT JOIN LATERAL<\/code> \u3084 <code>OUTER APPLY<\/code> \u304c\u6700\u9069\u3067\u3059\u3002<\/p>\n<pre><code>-- PostgreSQL \/ Oracle\nSELECT\n  c.category_name,\n  r_latest.review_text,\n  r_latest.created_at\nFROM\n  product_categories AS c\nLEFT JOIN LATERAL (\n  SELECT\n    r.review_text,\n    r.created_at\n  FROM\n    reviews AS r\n  WHERE\n    r.category_id = c.category_id\n  ORDER BY\n    r.created_at DESC\n  FETCH FIRST 3 ROWS ONLY -- Oracle\u69cb\u6587\n) AS r_latest ON true;<\/code><\/pre>\n<p>\u3053\u306eSQL\u306b\u3088\u308a\u3001\u30ab\u30c6\u30b4\u30ea\u3054\u3068\u306b\u6700\u65b0\u306e\u30ec\u30d3\u30e5\u30fc\u3092\u53d6\u5f97\u3057\u3064\u3064\u3001\u30ec\u30d3\u30e5\u30fc\u304c0\u4ef6\u306e\u30ab\u30c6\u30b4\u30ea\u3082\u30ea\u30b9\u30c8\u304b\u3089\u6f0f\u308c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<h3>\u30b7\u30ca\u30ea\u30aa2\uff1a\u91d1\u878d\u30b7\u30b9\u30c6\u30e0 &#8211; \u5404\u9867\u5ba2\u306e\u76f4\u8fd1\u306e\u53d6\u5f15\u5c65\u6b74\u30925\u4ef6\u53d6\u5f97<\/h3>\n<p><strong>\u8981\u4ef6<\/strong>: \u9867\u5ba2\u7ba1\u7406\u753b\u9762\u3067\u3001\u7279\u5b9a\u306e\u9867\u5ba2\u60c5\u5831\u3068\u5171\u306b\u3001\u305d\u306e\u9867\u5ba2\u304c\u884c\u3063\u305f\u76f4\u8fd15\u56de\u306e\u53d6\u5f15\u5c65\u6b74\uff08\u5165\u51fa\u91d1\u306a\u3069\uff09\u3092\u7d20\u65e9\u304f\u8868\u793a\u3057\u305f\u3044\u3002<\/p>\n<p>\u3053\u306e\u5834\u5408\u3082\u3001\u65b0\u898f\u767b\u9332\u3057\u305f\u3070\u304b\u308a\u3067\u53d6\u5f15\u5c65\u6b74\u304c\u307e\u3060\u306a\u3044\u9867\u5ba2\u3092\u8003\u616e\u3057\u3066 <code>OUTER APPLY<\/code> \u3092\u4f7f\u3046\u306e\u304c\u5805\u7262\u306a\u8a2d\u8a08\u3068\u8a00\u3048\u307e\u3059\u3002<\/p>\n<pre><code>-- SQL Server\nSELECT\n  c.customer_name,\n  t_recent.transaction_date,\n  t_recent.amount\nFROM\n  customers AS c\nOUTER APPLY (\n  SELECT\n    t.transaction_date,\n    t.amount\n  FROM\n    transactions AS t\n  WHERE\n    t.customer_id = c.customer_id\n  ORDER BY\n    t.transaction_date DESC\n  OFFSET 0 ROWS FETCH NEXT 5 ROWS ONLY\n) AS t_recent;<\/code><\/pre>\n<h3>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u5348\u5f8c\u8a66\u9a13\u3067\u306e\u51fa\u984c\u30a4\u30e1\u30fc\u30b8<\/h3>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u3001\u7279\u306b\u5348\u5f8c\u2160\u30fb\u5348\u5f8c\u2161\u3067\u306f\u3001<code>LATERAL JOIN<\/code> \u3084 <code>APPLY<\/code> \u306e\u77e5\u8b58\u304c\u76f4\u63a5\u554f\u308f\u308c\u308bSQL\u8a18\u8ff0\u554f\u984c\u304c\u983b\u51fa\u3057\u307e\u3059\u3002<\/p>\n<p><strong>\u8a2d\u554f\u4f8b<\/strong>:<\/p>\n<p>\u300c\u5404\u652f\u5e97\uff08\u652f\u5e97\u30de\u30b9\u30bf\uff09\u306b\u3064\u3044\u3066\u3001\u305d\u306e\u652f\u5e97\u3067\u53d7\u6ce8\u3057\u305f\u6700\u65b03\u4ef6\u306e\u53d7\u6ce8\u60c5\u5831\uff08\u53d7\u6ce8\u4f1d\u7968\uff09\u3092\u3001\u652f\u5e97\u540d\u3001\u53d7\u6ce8\u65e5\u3001\u53d7\u6ce8\u91d1\u984d\u306e\u5f62\u5f0f\u3067\u53d6\u5f97\u3059\u308bSELECT\u6587\u3092\u7b54\u3048\u3088\u3002\u306a\u304a\u3001\u53d7\u6ce8\u304c1\u4ef6\u3082\u306a\u3044\u652f\u5e97\u3082\u652f\u5e97\u540d\u3092\u8868\u793a\u3059\u308b\u3053\u3068\u3002\u300d<\/p>\n<p><strong>\u89e3\u7b54\u3078\u306e\u601d\u8003\u30d7\u30ed\u30bb\u30b9<\/strong>:<\/p>\n<ol>\n<li><strong>\u4e3b\u30c6\u30fc\u30d6\u30eb\u306e\u7279\u5b9a<\/strong>: \u307e\u305a\u3001\u300c\u5404\u652f\u5e97\u306b\u3064\u3044\u3066\u300d\u3068\u3042\u308b\u306e\u3067\u3001<code>FROM<\/code>\u53e5\u306e\u4e3b\u5f79\u306f<code>\u652f\u5e97\u30de\u30b9\u30bf<\/code>\u3060\u3068\u5224\u65ad\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u884c\u3054\u3068\u51e6\u7406\u306e\u767a\u60f3<\/strong>: \u300c\u5404\u652f\u5e97\u3054\u3068\u306b\u300d\u300c\u6700\u65b03\u4ef6\u300d\u3068\u3044\u3046\u30ad\u30fc\u30ef\u30fc\u30c9\u304b\u3089\u3001<code>GROUP BY<\/code>\u3067\u306f\u89e3\u3051\u306a\u3044\u30c8\u30c3\u30d7N\u554f\u984c\u3067\u3042\u308b\u3068\u898b\u629c\u304d\u307e\u3059\u3002\u3053\u3053\u3067\u884c\u3054\u3068\u306e\u30eb\u30fc\u30d7\u51e6\u7406\u304c\u53ef\u80fd\u306a <code>LATERAL JOIN<\/code> \/ <code>APPLY<\/code> \u3092\u4f7f\u3046\u65b9\u91dd\u3092\u7acb\u3066\u307e\u3059\u3002<\/li>\n<li><strong>\u5916\u90e8\u7d50\u5408\u306e\u9078\u629e<\/strong>: \u300c\u53d7\u6ce8\u304c1\u4ef6\u3082\u306a\u3044\u652f\u5e97\u3082\u8868\u793a\u300d\u3068\u3044\u3046\u8981\u4ef6\u304b\u3089\u3001\u5185\u90e8\u7d50\u5408\u7cfb\u306e <code>CROSS APPLY<\/code> \u3084 <code>JOIN LATERAL<\/code> \u3067\u306f\u306a\u304f\u3001\u5916\u90e8\u7d50\u5408\u7cfb\u306e <code>OUTER APPLY<\/code> \u307e\u305f\u306f <code>LEFT JOIN LATERAL<\/code> \u3092\u9078\u629e\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u69cb\u7bc9<\/strong>: <code>APPLY<\/code>\u53e5\uff08\u307e\u305f\u306f<code>LATERAL<\/code>\u53e5\uff09\u306e\u4e2d\u3067\u3001<code>\u53d7\u6ce8\u4f1d\u7968<\/code>\u30c6\u30fc\u30d6\u30eb\u3092\u5bfe\u8c61\u3068\u3057\u3001<code>WHERE<\/code>\u53e5\u3067<code>\u652f\u5e97\u30de\u30b9\u30bf.\u652f\u5e97\u30b3\u30fc\u30c9 = \u53d7\u6ce8\u4f1d\u7968.\u652f\u5e97\u30b3\u30fc\u30c9<\/code>\u3068\u3044\u3046\u76f8\u95a2\u6761\u4ef6\u3092\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u9806\u5e8f\u3068\u4ef6\u6570<\/strong>: <code>ORDER BY \u53d7\u6ce8\u65e5 DESC<\/code> \u3067\u65b0\u3057\u3044\u9806\u306b\u4e26\u3079\u66ff\u3048\u3001<code>FETCH FIRST 3 ROWS ONLY<\/code> \u306a\u3069\u3067\u4ef6\u6570\u3092\u7d5e\u308a\u8fbc\u307f\u307e\u3059\u3002<\/li>\n<\/ol>\n<p>\u3053\u306e\u601d\u8003\u30d7\u30ed\u30bb\u30b9\u3092\u8eab\u306b\u3064\u3051\u308b\u3053\u3068\u3067\u3001\u8907\u96d1\u306b\u898b\u3048\u308b\u554f\u984c\u6587\u3082\u69cb\u9020\u7684\u306b\u5206\u89e3\u3057\u3001\u6b63\u89e3\u306eSQL\u3092\u30b9\u30e0\u30fc\u30ba\u306b\u5c0e\u304d\u51fa\u305b\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<hr>\n<h2>\u307e\u3068\u3081\u2502\u300c\u884c\u3054\u3068\u306e\u30eb\u30fc\u30d7\u51e6\u7406\u300d\u3068\u3044\u3046\u6b66\u5668\u3092\u624b\u306b\u5165\u308c\u308b<\/h2>\n<p>\u672c\u8a18\u4e8b\u3067\u306f\u3001<code>LATERAL JOIN<\/code> \u3068 <code>APPLY<\/code> \u53e5\u306b\u3064\u3044\u3066\u3001\u305d\u306e\u5fc5\u8981\u6027\u304b\u3089\u5177\u4f53\u7684\u306a\u51e6\u7406\u30d5\u30ed\u30fc\u3001\u305d\u3057\u3066\u5b9f\u8df5\u7684\u306a\u6d3b\u7528\u30b7\u30ca\u30ea\u30aa\u307e\u3067\u3092\u89e3\u8aac\u3057\u307e\u3057\u305f\u3002\u6700\u5f8c\u306b\u3001\u91cd\u8981\u306a\u30dd\u30a4\u30f3\u30c8\u3092\u632f\u308a\u8fd4\u308a\u307e\u3057\u3087\u3046\u3002<\/p>\n<ul>\n<li><strong>\u6838\u5fc3\u7684\u306a\u30a4\u30e1\u30fc\u30b8<\/strong>: <code>LATERAL JOIN<\/code> \u3068 <code>APPLY<\/code> \u306e\u672c\u8cea\u306f\u3001<strong>\u300c\u4e3b\u30c6\u30fc\u30d6\u30eb\u306e\u5404\u884c\u3092\u30eb\u30fc\u30d7\u306e\u8d77\u70b9\u3068\u3057\u3001\u305d\u306e\u884c\u306e\u5024\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u5b9f\u884c\u3059\u308b\u300d<\/strong>\u3068\u3044\u3046\u8003\u3048\u65b9\u3067\u3059\u3002\u3053\u306e\u300c\u884c\u3054\u3068\u306e\u30eb\u30fc\u30d7\u51e6\u7406\u300d\u306e\u30a4\u30e1\u30fc\u30b8\u304c\u7406\u89e3\u306e\u9375\u3068\u306a\u308a\u307e\u3059\u3002<\/li>\n<li><strong>\u5b58\u5728\u7406\u7531<\/strong>: \u5f93\u6765\u306e<code>JOIN<\/code>\u3084<code>GROUP BY<\/code>\u3001\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u3067\u306f\u56f0\u96e3\u3060\u3063\u305f<strong>\u300c\u30b0\u30eb\u30fc\u30d7\u3054\u3068\u306e\u30c8\u30c3\u30d7N\u4ef6\u300d<\/strong>\u306e\u3088\u3046\u306a\u8907\u96d1\u306a\u554f\u3044\u5408\u308f\u305b\u3092\u3001\u76f4\u611f\u7684\u304b\u3064\u30b7\u30f3\u30d7\u30eb\u306b\u8a18\u8ff0\u3059\u308b\u305f\u3081\u306b\u751f\u307e\u308c\u307e\u3057\u305f\u3002<\/li>\n<li><strong>\u69cb\u6587\u306e\u4f7f\u3044\u5206\u3051<\/strong>:\n<ul>\n<li><strong><code>LATERAL JOIN<\/code> (PostgreSQL, Oracle\u7b49)<\/strong>: \u6a19\u6e96SQL\u306b\u8fd1\u3044\u69cb\u6587\u3067\u3059\u3002<\/li>\n<li><strong><code>APPLY<\/code> (SQL Server\u7b49)<\/strong>: <code>CROSS APPLY<\/code> \u3068 <code>OUTER APPLY<\/code> \u304c\u3042\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u7d50\u5408\u65b9\u6cd5\u306e\u9078\u629e<\/strong>:\n<ul>\n<li>\u30b5\u30d6\u30af\u30a8\u30ea\u304b\u3089\u5bfe\u5fdc\u3059\u308b\u30c7\u30fc\u30bf\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u884c\u3092<strong>\u9664\u5916\u3057\u305f\u3044<\/strong>\u5834\u5408 \u2192 <code>JOIN LATERAL<\/code> \u307e\u305f\u306f <code>CROSS APPLY<\/code> (<code>INNER JOIN<\/code>\u76f8\u5f53)<\/li>\n<li>\u30c7\u30fc\u30bf\u304c\u898b\u3064\u304b\u3089\u306a\u304f\u3066\u3082\u4e3b\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u306f<strong>\u6b8b\u3057\u305f\u3044<\/strong>\u5834\u5408 \u2192 <code>LEFT JOIN LATERAL<\/code> \u307e\u305f\u306f <code>OUTER APPLY<\/code> (<code>LEFT JOIN<\/code>\u76f8\u5f53)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u306e\u5348\u5f8c\u554f\u984c\u3084\u3001\u5b9f\u52d9\u3067\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u3092\u6271\u3046\u969b\u3001\u300c\u3007\u3007\u3054\u3068\u306b\u6700\u65b0\u306e\u25b3\u25b3\u3092 N \u4ef6\u2026\u300d\u3068\u3044\u3063\u305f\u8981\u4ef6\u304c\u51fa\u3066\u304d\u305f\u3089\u3001\u771f\u3063\u5148\u306b <code>LATERAL JOIN<\/code> \u3084 <code>APPLY<\/code> \u3092\u601d\u3044\u51fa\u305b\u308b\u3088\u3046\u306b\u8a13\u7df4\u3057\u3066\u304a\u304f\u3053\u3068\u304c\u3001\u5408\u683c\u3078\u306e\u3001\u305d\u3057\u3066\u30b9\u30ad\u30eb\u30a2\u30c3\u30d7\u3078\u306e\u8fd1\u9053\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u5f37\u529b\u306a\u6a5f\u80fd\u3092\u4f7f\u3044\u3053\u306a\u3057\u3001SQL\u306b\u3088\u308b\u30c7\u30fc\u30bf\u64cd\u4f5c\u306e\u53ef\u80fd\u6027\u3092\u3055\u3089\u306b\u5e83\u3052\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LATERAL JOIN\u3068APPLY\u53e5\u306e\u89e3\u8aac\u2502\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u5bfe\u7b56 \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u3084\u5fdc\u7528\u60c5\u5831\u6280\u8853\u8005\u8a66\u9a13\u306e\u5b66\u7fd2\u3067\u767b\u5834\u3059\u308b\u300cLATERAL JOIN\u300d\u3084\u300cAPPLY \u53e5\u300d\u3002\u53c2 &#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","columnck":"","stmeta_robots":"index, follow","st_titlewords":"","st_title_extra":"","st_keywords":"","st_description":"","st_display_ad_mark":"","st_display_ad_mark_hide":"","textcopyck":"","st_copyright":"","st_copyurl":"","eyecatch_set":"","st_youtube_url":"","st_youtube_eyecatch":"","headerwidget_set":"","updatewidget_set":"","kanrenwidget_set":"","ikkatuwidget_set":"","koukoku_set":"","auto_adsense_koukoku_set":"","st_redirect":"","st_redirect_url_as_canonical":"","st_redirect_pvm_do_not_track":"","post_data_updatewidget_set":"","st_post_header_under_bg":"","st_twitter_tag":"","affinger_note":"","rankdisplayck":"","st_head_code":"","st_footer_code":"","st_post_header_under_code":"","_st_toc_override":false,"_st_toc_settings":[]},"categories":[3],"tags":[6],"class_list":["post-6070","post","type-post","status-publish","format-standard","hentry","category-ipa","tag-6"],"_links":{"self":[{"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/posts\/6070","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/comments?post=6070"}],"version-history":[{"count":0,"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/posts\/6070\/revisions"}],"wp:attachment":[{"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/media?parent=6070"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/categories?post=6070"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/tags?post=6070"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}