{"id":6062,"date":"2025-09-27T22:49:47","date_gmt":"2025-09-27T13:49:47","guid":{"rendered":"https:\/\/sky-peace.net\/?p=6062"},"modified":"2025-09-27T22:49:47","modified_gmt":"2025-09-27T13:49:47","slug":"db-ura-1","status":"publish","type":"post","link":"https:\/\/sky-peace.net\/certified\/db-ura-1\/","title":{"rendered":"INSERT\u2026SELECT\u304b\u3089MERGE\u3001EXISTS\u307e\u3067\uff1a\u5fdc\u7528SQL\u5927\u5168\uff08\u5fdc\u7528\u60c5\u5831\u30fbDB\u30b9\u30da\u5bfe\u5fdc\uff09"},"content":{"rendered":"<p><code>SELECT * FROM ...<\/code> \u306f\u3082\u3046\u5352\u696d\u3002\u3042\u306a\u305f\u306f\u3001\u5b9f\u52d9\u306e\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u8981\u6c42\u3084\u3001\u5fdc\u7528\u60c5\u5831\u30fb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da-\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u306e\u5348\u5f8c\u554f\u984c\u3067\u51fa\u984c\u3055\u308c\u308b\u3088\u3046\u306a\u3001\u4e00\u7b4b\u7e04\u3067\u306f\u3044\u304b\u306a\u3044SQL\u306b\u982d\u3092\u60a9\u307e\u305b\u3066\u3044\u307e\u305b\u3093\u304b\uff1f\u57fa\u672c\u7684\u306aCRUD\u64cd\u4f5c\u306f\u3067\u304d\u3066\u3082\u3001<code>GROUP BY<\/code>\u3068<code>LEFT JOIN<\/code>\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3042\u305f\u308a\u304b\u3089\u3001SQL\u306e\u8907\u96d1\u3055\u306b\u58c1\u3092\u611f\u3058\u308b\u65b9\u306f\u5c11\u306a\u304f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u300c\u30b5\u30d6\u30af\u30a8\u30ea\u304c\u4f55\u91cd\u306b\u3082\u306a\u3063\u3066\u53ef\u8aad\u6027\u304c\u6700\u60aa\u2026\u300d\u300c\u3082\u3063\u3068\u52b9\u7387\u7684\u306b\u30c7\u30fc\u30bf\u3092\u66f4\u65b0\u3059\u308b\u65b9\u6cd5\u306f\u306a\u3044\u306e\u304b\uff1f\u300d\u300c\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u3063\u3066\u805e\u3044\u305f\u3053\u3068\u306f\u3042\u308b\u3051\u3069\u3001\u3069\u3046\u4f7f\u3048\u3070\u3044\u3044\u304b\u5206\u304b\u3089\u306a\u3044\u300d\u3002\u305d\u3093\u306a\u60a9\u307f\u306f\u3001\u591a\u304f\u306e\u30c7\u30fc\u30bf\u6d3b\u7528\u8005\u304c\u901a\u308b\u9053\u3067\u3059\u3002<\/p>\n<p>\u672c\u8a18\u4e8b\u3067\u306f\u3001\u57fa\u672c\u7684\u306aSQL\u304b\u3089\u4e00\u6b69\u9032\u307f\u3001\u3042\u306a\u305f\u306e<strong>\u300cSQL\u306e\u5f15\u304d\u51fa\u3057\u300d<\/strong>\u3092\u5287\u7684\u306b\u5897\u3084\u3059\u305f\u3081\u306e\u5fdc\u7528\u7684\u306a\u69cb\u6587\u3092\u7db2\u7f85\u7684\u306b\u89e3\u8aac\u3057\u307e\u3059\u3002\u5358\u306a\u308b\u69cb\u6587\u306e\u7d39\u4ecb\u3060\u3051\u3067\u306a\u304f\u3001\u306a\u305c\u305d\u308c\u304c\u5fc5\u8981\u306a\u306e\u304b\uff08What\/Why\uff09\u3001\u3069\u306e\u3088\u3046\u306a\u5834\u9762\u3067\u5f79\u7acb\u3064\u306e\u304b\uff08When\/Where\uff09\u3001\u305d\u3057\u3066\u5177\u4f53\u7684\u306a\u66f8\u304d\u65b9\uff08How\uff09\u307e\u3067\u3092\u3001\u8c4a\u5bcc\u306a\u30b3\u30fc\u30c9\u4f8b\u3084\u56f3\u89e3\u3092\u4ea4\u3048\u306a\u304c\u3089\u4f53\u7cfb\u7684\u306b\u6398\u308a\u4e0b\u3052\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u30c7\u30fc\u30bf\u306e\u633f\u5165\u30fb\u66f4\u65b0\u3092\u52b9\u7387\u5316\u3059\u308b\u30c6\u30af\u30cb\u30c3\u30af\u304b\u3089\u3001<code>WITH<\/code>\u53e5\uff08CTE\uff09\u306b\u3088\u308b\u53ef\u8aad\u6027\u306e\u5411\u4e0a\u3001<code>ROW_NUMBER()<\/code>\u3092\u306f\u3058\u3081\u3068\u3059\u308b\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u3067\u306e\u9ad8\u5ea6\u306a\u96c6\u8a08\u3001<code>MERGE<\/code>\u6587\u306b\u3088\u308b\u6761\u4ef6\u5206\u5c90\u51e6\u7406\u3001\u305d\u3057\u3066\u8a66\u9a13\u3067\u72d9\u308f\u308c\u3084\u3059\u3044<code>EXISTS<\/code>\u53e5\u3084<code>HAVING<\/code>\u53e5\u306e\u4f7f\u3044\u5206\u3051\u307e\u3067\u3002\u5b9f\u52d9\u3067\u983b\u51fa\u306e\u30b1\u30fc\u30b9\u3084\u3001\u8a66\u9a13\u306e\u300c\u3072\u3063\u304b\u3051\u554f\u984c\u300d\u5bfe\u7b56\u306b\u7126\u70b9\u3092\u5f53\u3066\u3001\u4e00\u3064\u3072\u3068\u3064\u4e01\u5be7\u306b\u30de\u30b9\u30bf\u30fc\u3057\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<p>\u3053\u306e\u8a18\u4e8b\u3092\u8aad\u307f\u7d42\u3048\u308b\u9803\u306b\u306f\u3001\u3042\u306a\u305f\u306f\u8907\u96d1\u306aSQL\u3092\u81ea\u4fe1\u3092\u6301\u3063\u3066\u7d44\u307f\u7acb\u3066\u3089\u308c\u308b\u3088\u3046\u306b\u306a\u308a\u3001\u30c7\u30fc\u30bf\u6d3b\u7528\u306e\u30ec\u30d9\u30eb\u304c\u683c\u6bb5\u306b\u5411\u4e0a\u3057\u3066\u3044\u308b\u306f\u305a\u3067\u3059\u3002\u305d\u308c\u3067\u306f\u3001\u5fdc\u7528SQL\u306e\u5965\u6df1\u3044\u4e16\u754c\u3078\u4e00\u7dd2\u306b\u65c5\u7acb\u3061\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2>INSERT\u306e\u5fdc\u7528\u30c6\u30af\u30cb\u30c3\u30af\u2502\u30af\u30a8\u30ea\u7d50\u679c\u306e\u633f\u5165\u304b\u3089ID\u306e\u5373\u6642\u53d6\u5f97\u307e\u3067<\/h2>\n<p>\u65e5\u3005\u306e\u696d\u52d9\u3067\u4f55\u6c17\u306a\u304f\u4f7f\u3063\u3066\u3044\u308b <code>INSERT<\/code> \u6587\u3002<code>INSERT INTO table (column1, column2) VALUES (value1, value2);<\/code> \u3068\u3044\u3046\u57fa\u672c\u5f62\u306f\u3001SQL\u3092\u5b66\u3073\u59cb\u3081\u305f\u6700\u521d\u306b\u899a\u3048\u308b\u69cb\u6587\u306e\u4e00\u3064\u3067\u3057\u3087\u3046\u3002\u3057\u304b\u3057\u3001\u5b9f\u52d9\u3084\u8a66\u9a13\u3067\u306f\u3001\u3053\u306e\u57fa\u672c\u5f62\u3060\u3051\u3067\u306f\u5bfe\u5fdc\u3067\u304d\u306a\u3044\u30b7\u30fc\u30f3\u304c\u6570\u591a\u304f\u767b\u5834\u3057\u307e\u3059\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u300c<strong>\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u96c6\u8a08\u7d50\u679c\u3092\u4e38\u3054\u3068\u65b0\u3057\u3044\u30c6\u30fc\u30d6\u30eb\u306b\u79fb\u3057\u305f\u3044<\/strong>\u300d\u300c<strong>\u5927\u91cf\u306e\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u3092\u4e00\u62ec\u3067\u4f5c\u6210\u3057\u305f\u3044<\/strong>\u300d\u300c<strong>\u30c7\u30fc\u30bf\u3092\u767b\u9332\u3057\u305f\u77ac\u9593\u306b\u3001\u81ea\u52d5\u63a1\u756a\u3055\u308c\u305fID\u3092\u3059\u3050\u3055\u307e\u53d6\u5f97\u3057\u305f\u3044<\/strong>\u300d\u3068\u3044\u3063\u305f\u8981\u6c42\u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u3089\u306e\u8ab2\u984c\u306f\u3001\u3053\u308c\u304b\u3089\u7d39\u4ecb\u3059\u308b3\u3064\u306e\u5fdc\u7528\u30c6\u30af\u30cb\u30c3\u30af\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u30b9\u30de\u30fc\u30c8\u304b\u3064\u52b9\u7387\u7684\u306b\u89e3\u6c7a\u3067\u304d\u307e\u3059\u3002<\/p>\n<ol>\n<li><code>INSERT ... SELECT<\/code>\uff1a\u30af\u30a8\u30ea\u306e\u7d50\u679c\u3092\u305d\u306e\u307e\u307e\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u633f\u5165\u3059\u308b<\/li>\n<li><code>CTAS \/ SELECT INTO<\/code>\uff1a\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u3068\u30c7\u30fc\u30bf\u633f\u5165\u3092\u4e00\u5ea6\u306b\u884c\u3046<\/li>\n<li><code>INSERT ... RETURNING \/ OUTPUT<\/code>\uff1a\u633f\u5165\u3057\u305f\u30ec\u30b3\u30fc\u30c9\u306e\u60c5\u5831\u3092\u5373\u5ea7\u306b\u53d7\u3051\u53d6\u308b<\/li>\n<\/ol>\n<p>\u4e00\u3064\u305a\u3064\u3001\u5177\u4f53\u7684\u306a\u30b3\u30fc\u30c9\u4f8b\u3068\u696d\u52d9\u30b7\u30fc\u30f3\u3092\u4ea4\u3048\u3066\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3>\u30af\u30a8\u30ea\u7d50\u679c\u3092\u4e00\u62ec\u767b\u9332\u3059\u308b <code>INSERT ... SELECT<\/code><\/h3>\n<p><code>INSERT ... SELECT<\/code> \u306f\u3001<code>SELECT<\/code> \u6587\u3067\u53d6\u5f97\u3057\u305f\u7d50\u679c\u30bb\u30c3\u30c8\u3092\u3001\u305d\u306e\u307e\u307e\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u4e00\u62ec\u3067\u633f\u5165\u3059\u308b\u305f\u3081\u306e\u69cb\u6587\u3067\u3059\u3002\u30eb\u30fc\u30d7\u51e6\u7406\u306a\u3069\u30671\u4ef6\u305a\u3064 <code>INSERT<\/code> \u3092\u7e70\u308a\u8fd4\u3059\u306e\u306b\u6bd4\u3079\u3001<strong>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u306b\u512a\u308c\u3001\u30b3\u30fc\u30c9\u3082\u7c21\u6f54\u306b\u306a\u308b<\/strong>\u3068\u3044\u3046\u5927\u304d\u306a\u30e1\u30ea\u30c3\u30c8\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h4>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u6708\u6b21\u306e\u58f2\u4e0a\u30b5\u30de\u30ea\u30fc\u3092\u4f5c\u6210\u3059\u308b<\/h4>\n<p>\u65e5\u3005\u306e\u58f2\u4e0a\u30c7\u30fc\u30bf\u304c\u683c\u7d0d\u3055\u308c\u305f <code>sales<\/code> \u30c6\u30fc\u30d6\u30eb\u304c\u3042\u308b\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre><code>-- \u633f\u5165\u5148\u306e\u30c6\u30fc\u30d6\u30eb\uff08\u3042\u3089\u304b\u3058\u3081\u4f5c\u6210\u3057\u3066\u304a\u304f\uff09\nCREATE TABLE monthly_summary (\n    target_month  VARCHAR(7),\n    customer_id   INT,\n    total_amount  BIGINT\n);\n\n-- 2025\u5e749\u6708\u5206\u306e\u30c7\u30fc\u30bf\u3092\u96c6\u8a08\u3057\u3001\u30b5\u30de\u30ea\u30fc\u30c6\u30fc\u30d6\u30eb\u306b\u633f\u5165\nINSERT INTO monthly_summary (target_month, customer_id, total_amount)\nSELECT\n    '2025-09' AS target_month,\n    customer_id,\n    SUM(amount) AS total_amount\nFROM\n    sales\nWHERE\n    sale_date &gt;= '2025-09-01' AND sale_date &lt; '2025-10-01'\nGROUP BY\n    customer_id;\n<\/code><\/pre>\n<h4>&#x1f3e2; \u3053\u3093\u306a\u4ed5\u4e8b\u3067\u5f79\u7acb\u3064\uff01<\/h4>\n<ul>\n<li><strong>\u591c\u9593\u30d0\u30c3\u30c1\u51e6\u7406<\/strong>: \u65e5\u6b21\u30c7\u30fc\u30bf\u3092\u96c6\u8a08\u3057\u3066\u3001\u6708\u6b21\u30b5\u30de\u30ea\u30fc\u30c6\u30fc\u30d6\u30eb\u3084\u5206\u6790\u7528DWH\uff08\u30c7\u30fc\u30bf\u30a6\u30a7\u30a2\u30cf\u30a6\u30b9\uff09\u306b\u30c7\u30fc\u30bf\u3092\u8ee2\u9001\u3059\u308b\u51e6\u7406\u3002<\/li>\n<li><strong>\u30c7\u30fc\u30bf\u79fb\u884c<\/strong>: \u53e4\u3044\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u65b0\u3057\u3044\u30b9\u30ad\u30fc\u30de\u306e\u30c6\u30fc\u30d6\u30eb\u3078\u3001\u30c7\u30fc\u30bf\u3092\u5909\u63db\u3057\u306a\u304c\u3089\u4e00\u62ec\u3067\u79fb\u884c\u3059\u308b\u4f5c\u696d\u3002<\/li>\n<li><strong>\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u4f5c\u6210<\/strong>: \u65e2\u5b58\u306e\u9867\u5ba2\u30c7\u30fc\u30bf\u3092\u5143\u306b\u3001\u500b\u4eba\u60c5\u5831\u306a\u3069\u3092\u30de\u30b9\u30af\u51e6\u7406\u3057\u305f\u30c6\u30b9\u30c8\u7528\u30c7\u30fc\u30bf\u3092\u5927\u91cf\u306b\u4f5c\u6210\u3059\u308b\u5834\u9762\u3002<\/li>\n<\/ul>\n<h3>\u30c6\u30fc\u30d6\u30eb\u4f5c\u6210\u3068\u30c7\u30fc\u30bf\u633f\u5165\u3092\u540c\u6642\u306b\u884c\u3046 <code>CTAS \/ SELECT INTO<\/code><\/h3>\n<p><code>CTAS (Create Table As Select)<\/code> \u306f\u3001\u305d\u306e\u540d\u306e\u901a\u308a\u3001<code>SELECT<\/code> \u6587\u306e\u7d50\u679c\u3092\u5143\u306b\u3057\u3066<strong>\u65b0\u3057\u3044\u30c6\u30fc\u30d6\u30eb\u306e\u4f5c\u6210\u3068\u30c7\u30fc\u30bf\u306e\u633f\u5165\u3092\u540c\u6642\u306b<\/strong>\u884c\u3044\u307e\u3059\u3002<code>CREATE TABLE<\/code> \u3057\u3066 <code>INSERT ... SELECT<\/code> \u3092\u884c\u30462\u30b9\u30c6\u30c3\u30d7\u3092\u30011\u3064\u306e\u30af\u30a8\u30ea\u3067\u5b8c\u7d50\u3055\u305b\u3089\u308c\u308b\u4fbf\u5229\u306a\u69cb\u6587\u3067\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u88fd\u54c1\u306b\u3088\u3063\u3066\u69cb\u6587\u304c\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n<table border=\"1\">\n<thead>\n<tr>\n<th>\u69cb\u6587<\/th>\n<th>\u5bfe\u5fdc\u3059\u308b\u4e3b\u306aDBMS<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>CREATE TABLE ... AS SELECT<\/code><\/td>\n<td>Oracle, PostgreSQL, MySQL, BigQuery<\/td>\n<\/tr>\n<tr>\n<td><code>SELECT ... INTO ... FROM<\/code><\/td>\n<td>SQL Server<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u5206\u6790\u7528\u306e\u4e00\u6642\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b<\/h4>\n<p><strong>PostgreSQL \u3084 Oracle \u306e\u5834\u5408 (CTAS)<\/strong><\/p>\n<pre><code>CREATE TABLE premium_customers AS\nSELECT\n    customer_id,\n    COUNT(*) AS purchase_count,\n    SUM(amount) AS total_amount\nFROM\n    sales\nGROUP BY\n    customer_id\nHAVING\n    SUM(amount) &gt;= 100000;\n<\/code><\/pre>\n<p><strong>SQL Server \u306e\u5834\u5408 (SELECT INTO)<\/strong><\/p>\n<pre><code>SELECT\n    customer_id,\n    COUNT(*) AS purchase_count,\n    SUM(amount) AS total_amount\nINTO\n    premium_customers\nFROM\n    sales\nGROUP BY\n    customer_id\nHAVING\n    SUM(amount) &gt;= 100000;\n<\/code><\/pre>\n<h4>&#x1f3e2; \u3053\u3093\u306a\u4ed5\u4e8b\u3067\u5f79\u7acb\u3064\uff01<\/h4>\n<ul>\n<li><strong>\u30c7\u30fc\u30bf\u5206\u6790<\/strong>: \u5143\u306e\u5927\u898f\u6a21\u30c6\u30fc\u30d6\u30eb\u306b\u5f71\u97ff\u3092\u4e0e\u3048\u305a\u3001\u5206\u6790\u306b\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u3060\u3051\u3092\u62bd\u51fa\u3057\u305f\u81ea\u5206\u5c02\u7528\u306e\u300c\u30b5\u30f3\u30c9\u30dc\u30c3\u30af\u30b9\uff08\u7802\u5834\uff09\u300d\u30c6\u30fc\u30d6\u30eb\u3092\u4f5c\u6210\u3059\u308b\u6642\u3002<\/li>\n<li><strong>\u30d0\u30c3\u30af\u30a2\u30c3\u30d7<\/strong>: \u30c6\u30fc\u30d6\u30eb\u3092\u66f4\u65b0\u3059\u308b\u524d\u306b\u3001\u5ff5\u306e\u305f\u3081\u7279\u5b9a\u306e\u6761\u4ef6\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u5225\u30c6\u30fc\u30d6\u30eb\u306b\u9000\u907f\u3055\u305b\u3066\u304a\u304d\u305f\u3044\u6642\u3002<\/li>\n<\/ul>\n<h3>\u767b\u9332\u3057\u305fID\u3092\u5373\u5ea7\u306b\u53d7\u3051\u53d6\u308b <code>INSERT ... RETURNING \/ OUTPUT<\/code><\/h3>\n<p>Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306a\u3069\u3067\u30c7\u30fc\u30bf\u3092\u65b0\u898f\u767b\u9332\u3059\u308b\u969b\u3001\u81ea\u52d5\u63a1\u756a\uff08<code>SERIAL<\/code> \u3084 <code>IDENTITY<\/code>\uff09\u3067\u751f\u6210\u3055\u308c\u305f\u4e3b\u30ad\u30fc\uff08ID\uff09\u3092\u3059\u3050\u306b\u77e5\u308a\u305f\u3044\u30b1\u30fc\u30b9\u306f\u975e\u5e38\u306b\u591a\u3044\u3067\u3059\u3002<code>RETURNING<\/code>\u53e5 (PostgreSQL, Oracle) \u3084 <code>OUTPUT<\/code>\u53e5 (SQL Server) \u3092\u4f7f\u3048\u3070\u3001<strong><code>INSERT<\/code> \u3092\u5b9f\u884c\u3057\u305f\u305d\u306e\u5834\u3067\u3001\u633f\u5165\u3055\u308c\u305f\u884c\u306e\u60c5\u5831\u3092\u8fd4\u3059<\/strong>\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h4>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u65b0\u898f\u30e6\u30fc\u30b6\u30fc\u767b\u9332\u3067ID\u3092\u53d6\u5f97\u3059\u308b<\/h4>\n<p><strong>PostgreSQL \u3084 Oracle \u306e\u5834\u5408 (RETURNING)<\/strong><\/p>\n<pre><code>INSERT INTO users (name, email)\nVALUES ('Taro Yamada', 'taro@example.com')\nRETURNING id;\n<\/code><\/pre>\n<p><strong>SQL Server \u306e\u5834\u5408 (OUTPUT)<\/strong><\/p>\n<pre><code>INSERT INTO users (name, email)\nOUTPUT inserted.id\nVALUES ('Jiro Sato', 'jiro@example.com');\n<\/code><\/pre>\n<h4>&#x1f3e2; \u3053\u3093\u306a\u4ed5\u4e8b\u3067\u5f79\u7acb\u3064\uff01<\/h4>\n<ul>\n<li><strong>Web\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3<\/strong>: \u30e6\u30fc\u30b6\u30fc\u306e\u65b0\u898f\u767b\u9332\u3001\u6ce8\u6587\u306e\u53d7\u4ed8\u3001\u6295\u7a3f\u306e\u4fdd\u5b58\u306a\u3069\u3001\u767b\u9332\u51e6\u7406\u3068\u540c\u6642\u306b\u751f\u6210\u3055\u308c\u305fID\u3092\u4f7f\u3063\u3066\u6b21\u306e\u51e6\u7406\u3078\u7e4b\u3052\u305f\u3044\u3042\u3089\u3086\u308b\u5834\u9762\u3002<\/li>\n<li><strong>\u89aa\u5b50\u95a2\u4fc2\u306e\u3042\u308b\u30c7\u30fc\u30bf\u767b\u9332<\/strong>: \u89aa\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u3092 <code>INSERT<\/code> \u3057\u3066ID\u3092\u53d6\u5f97\u3057\u3001\u5373\u5ea7\u306b\u305d\u306eID\u3092\u4f7f\u3063\u3066\u5b50\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u3092\u767b\u9332\u3059\u308b\u51e6\u7406\u3002<\/li>\n<\/ul>\n<h2>UPDATE\uff0fDELETE\u306e\u9ad8\u5ea6\u306a\u4f7f\u3044\u65b9\u2502JOIN\u3084\u30b5\u30d6\u30af\u30a8\u30ea\u3067\u5bfe\u8c61\u3092\u4e00\u62ec\u6307\u5b9a<\/h2>\n<p>\u30c7\u30fc\u30bf\u306e\u66f4\u65b0\u3084\u524a\u9664\u3092\u884c\u3046<code>UPDATE<\/code>\u3068<code>DELETE<\/code>\u3002<code>WHERE id = 1<\/code> \u306e\u3088\u3046\u306b\u5358\u4e00\u306e\u30ec\u30b3\u30fc\u30c9\u3092\u5bfe\u8c61\u3068\u3059\u308b\u51e6\u7406\u306f\u7c21\u5358\u3067\u3059\u304c\u3001\u5b9f\u52d9\u3067\u306f\u3082\u3063\u3068\u8907\u96d1\u306a\u6761\u4ef6\u6307\u5b9a\u304c\u6c42\u3081\u3089\u308c\u307e\u3059\u3002\u300c<strong>\u9867\u5ba2\u30de\u30b9\u30bf\u3092\u53c2\u7167\u3057\u3066\u3001\u7279\u5b9a\u306e\u4f1a\u54e1\u30e9\u30f3\u30af\u306e\u30e6\u30fc\u30b6\u30fc\u306b\u95a2\u9023\u3059\u308b\u58f2\u4e0a\u30c7\u30fc\u30bf\u306e\u30d5\u30e9\u30b0\u3092\u4e00\u62ec\u3067\u66f4\u65b0\u3057\u305f\u3044<\/strong>\u300d\u300c<strong>\u5546\u54c1\u30ab\u30c6\u30b4\u30ea\u304c\u300e\u5ec3\u756a\u300f\u306b\u306a\u3063\u305f\u5546\u54c1\u306b\u95a2\u9023\u3059\u308b\u3001\u3059\u3079\u3066\u306e\u5728\u5eab\u30ec\u30b3\u30fc\u30c9\u3092\u524a\u9664\u3057\u305f\u3044<\/strong>\u300d\u3068\u3044\u3063\u305f\u30b1\u30fc\u30b9\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u3088\u3046\u306a\u300c\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u60c5\u5831\u300d\u3092\u6761\u4ef6\u306b\u542b\u3081\u308b\u5834\u5408\u3001<code>JOIN<\/code> \u3084\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u6d3b\u7528\u3059\u308b\u3053\u3068\u3067\u3001\u8907\u6570\u306e\u30af\u30a8\u30ea\u3092\u767a\u884c\u3059\u308b\u3053\u3068\u306a\u304f\u3001\u4e00\u5ea6\u306e\u64cd\u4f5c\u3067\u5b89\u5168\u304b\u3064\u52b9\u7387\u7684\u306b\u51e6\u7406\u3092\u5b8c\u7d50\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>\u5225\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u6761\u4ef6\u306b\u3059\u308b <code>JOIN<\/code> \u3092\u4f34\u3046UPDATE\uff0fDELETE<\/h3>\n<p><code>UPDATE<\/code>\u6587\u3084<code>DELETE<\/code>\u6587\u306e\u53e5\u306e\u4e2d\u3067<code>JOIN<\/code>\u3092\u4f7f\u3044\u3001\u4ed6\u306e\u30c6\u30fc\u30d6\u30eb\u3068\u7d50\u5408\u3059\u308b\u3053\u3068\u3067\u3001\u305d\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u30ab\u30e9\u30e0\u3092\u6761\u4ef6\u306b\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u5074\u3067<code>SELECT<\/code>\u3057\u3066\u304b\u3089<code>UPDATE<\/code>\u3059\u308b\u3001\u3068\u3044\u3063\u305f\u9762\u5012\u306a\u624b\u7d9a\u304d\u304c\u4e0d\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u4f1a\u54e1\u30e9\u30f3\u30af\u306b\u5fdc\u3058\u3066\u58f2\u4e0a\u30c7\u30fc\u30bf\u3092\u66f4\u65b0\u3059\u308b<\/h4>\n<p>\u300c\u30d6\u30ed\u30f3\u30ba\u4f1a\u54e1\uff08<code>rank = 'Bronze'<\/code>\uff09\u306e\u58f2\u4e0a\u30ec\u30b3\u30fc\u30c9\u5168\u3066\u306b\u3001\u5099\u8003\u6b04(<code>notes<\/code>)\u3078\u300e\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u5272\u5f15\u5bfe\u8c61\u5916\u300f\u3068\u8ffd\u8a18\u3059\u308b\u300d\u3068\u3044\u3046\u8981\u4ef6\u306b\u5bfe\u5fdc\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><strong>PostgreSQL \u306e\u5834\u5408 (<code>UPDATE ... FROM ...<\/code>)<\/strong><\/p>\n<pre><code>UPDATE sales s\nSET\n    notes = '\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u5272\u5f15\u5bfe\u8c61\u5916'\nFROM\n    customers c\nWHERE\n    s.customer_id = c.customer_id AND c.rank = 'Bronze';\n<\/code><\/pre>\n<p><strong>MySQL \u306e\u5834\u5408 (<code>UPDATE ... JOIN ...<\/code>)<\/strong><\/p>\n<pre><code>UPDATE sales s\nJOIN\n    customers c ON s.customer_id = c.customer_id\nSET\n    s.notes = '\u30ad\u30e3\u30f3\u30da\u30fc\u30f3\u5272\u5f15\u5bfe\u8c61\u5916'\nWHERE\n    c.rank = 'Bronze';\n<\/code><\/pre>\n<h4>&#x1f3e2; \u3053\u3093\u306a\u4ed5\u4e8b\u3067\u5f79\u7acb\u3064\uff01<\/h4>\n<ul>\n<li><strong>\u30de\u30b9\u30bf\u30c7\u30fc\u30bf\u9023\u643a<\/strong>: \u5546\u54c1\u30de\u30b9\u30bf\u3067\u300c\u8ca9\u58f2\u7d42\u4e86\u300d\u306b\u306a\u3063\u305f\u5546\u54c1\u306b\u3064\u3044\u3066\u3001\u95a2\u9023\u3059\u308b\u5728\u5eab\u30c6\u30fc\u30d6\u30eb\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u300c\u8ca9\u58f2\u4e0d\u53ef\u300d\u306b\u4e00\u62ec\u66f4\u65b0\u3059\u308b\u3002<\/li>\n<li><strong>\u30c7\u30fc\u30bf\u30af\u30ec\u30f3\u30b8\u30f3\u30b0<\/strong>: \u9867\u5ba2\u30de\u30b9\u30bf\u4e0a\u3067\u91cd\u8907\u30d5\u30e9\u30b0\u304c\u7acb\u3066\u3089\u308c\u305f\u9867\u5ba2\u306e\u3001\u53e4\u3044\u65b9\u306e\u884c\u52d5\u30ed\u30b0\u3092\u4e00\u62ec\u3067\u524a\u9664\u3059\u308b\u3002<\/li>\n<\/ul>\n<h4>&#x1f4dd; DBMS\u5225 \u69cb\u6587\u306e\u9055\u3044<\/h4>\n<table border=\"1\">\n<thead>\n<tr>\n<th>DBMS<\/th>\n<th>UPDATE\u69cb\u6587<\/th>\n<th>DELETE\u69cb\u6587<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>PostgreSQL<\/strong><\/td>\n<td><code>UPDATE T1 SET ... FROM T2 WHERE ...<\/code><\/td>\n<td><code>DELETE FROM T1 USING T2 WHERE ...<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>SQL Server<\/strong><\/td>\n<td><code>UPDATE T1 SET ... FROM T1 JOIN T2 ON ...<\/code><\/td>\n<td><code>DELETE T1 FROM T1 JOIN T2 ON ...<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>MySQL<\/strong><\/td>\n<td><code>UPDATE T1 JOIN T2 ON ... SET ...<\/code><\/td>\n<td><code>DELETE T1 FROM T1 JOIN T2 ON ...<\/code><\/td>\n<\/tr>\n<tr>\n<td><strong>Oracle<\/strong><\/td>\n<td><code>MERGE<\/code>\u6587\u3084 <code>UPDATE (SELECT ...)<\/code> \u3092\u4f7f\u7528<\/td>\n<td>\u30b5\u30d6\u30af\u30a8\u30ea (<code>IN<\/code> \/ <code>EXISTS<\/code>) \u3092\u4f7f\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u5229\u7528\u3057\u305f\u6761\u4ef6\u66f4\u65b0\uff0f\u524a\u9664<\/h3>\n<p><code>WHERE<\/code>\u53e5\u306e\u4e2d\u306b<code>SELECT<\/code>\u6587\uff08\u30b5\u30d6\u30af\u30a8\u30ea\uff09\u3092\u8a18\u8ff0\u3057\u3066\u3001\u66f4\u65b0\u30fb\u524a\u9664\u306e\u5bfe\u8c61\u3092\u7d5e\u308a\u8fbc\u3080\u65b9\u6cd5\u3067\u3059\u3002<code>IN<\/code> \u3084 <code>EXISTS<\/code> \u3092\u4f7f\u3046\u306e\u304c\u4e00\u822c\u7684\u3067\u3001JOIN\u306b\u6bd4\u3079\u3066\u69cb\u6587\u304c\u76f4\u611f\u7684\u3067\u5206\u304b\u308a\u3084\u3059\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h4>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u8cfc\u5165\u5c65\u6b74\u306e\u306a\u3044\u9867\u5ba2\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u66f4\u65b0\u3059\u308b<\/h4>\n<p>\u300c\u76f4\u8fd11\u5e74\u9593\u306b\u4e00\u5ea6\u3082\u8cfc\u5165\u5c65\u6b74\u306e\u306a\u3044\u9867\u5ba2\u300d\u306e\u30b9\u30c6\u30fc\u30bf\u30b9\u3092\u3001\u300c\u4f11\u7720\uff08<code>dormant<\/code>\uff09\u300d\u306b\u66f4\u65b0\u3059\u308b\u30b1\u30fc\u30b9\u3092\u8003\u3048\u307e\u3059\u3002<code>NOT EXISTS<\/code>\u3092\u4f7f\u3046\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8868\u73fe\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code>UPDATE customers c\nSET\n    status = 'dormant'\nWHERE\n    NOT EXISTS (\n        SELECT 1\n        FROM sales s\n        WHERE\n            s.customer_id = c.customer_id\n            AND s.sale_date &gt;= CURRENT_DATE - INTERVAL '1 year'\n    );\n<\/code><\/pre>\n<h4>&#x1f3e2; \u3053\u3093\u306a\u4ed5\u4e8b\u3067\u5f79\u7acb\u3064\uff01<\/h4>\n<ul>\n<li><strong>\u30de\u30b9\u30bf\u306e\u68da\u5378\u3057<\/strong>: \u5728\u5eab\u6570\u304c0\u3067\u3001\u304b\u3064\u904e\u53bb3\u5e74\u9593\u4e00\u5ea6\u3082\u6ce8\u6587\u3055\u308c\u3066\u3044\u306a\u3044\u5546\u54c1\u3092\u5546\u54c1\u30de\u30b9\u30bf\u304b\u3089<code>DELETE<\/code>\u3059\u308b\u3002<\/li>\n<li><strong>\u30d5\u30e9\u30b0\u4ed8\u3051<\/strong>: \u5168\u793e\u54e1\u306e\u5e73\u5747\u52e4\u7d9a\u5e74\u6570\u3092\u30b5\u30d6\u30af\u30a8\u30ea\u3067\u7b97\u51fa\u3057\u3001\u305d\u306e\u5e73\u5747\u5024\u3088\u308a\u3082\u52e4\u7d9a\u5e74\u6570\u304c\u9577\u3044\u793e\u54e1\u306e\u30ec\u30b3\u30fc\u30c9\u306b\u300c\u30d9\u30c6\u30e9\u30f3\u300d\u30d5\u30e9\u30b0\u3092<code>UPDATE<\/code>\u3067\u4ed8\u4e0e\u3059\u308b\u3002<\/li>\n<\/ul>\n<h2>UPSERT\uff0f\u91cd\u8907\u6642\u306e\u51e6\u7406\u2502MERGE\u6587\u3067\u633f\u5165\u3068\u66f4\u65b0\u3092\u4e00\u5ea6\u306b<\/h2>\n<p>\u30c7\u30fc\u30bf\u3092\u767b\u9332\u3057\u3088\u3046\u3068\u3057\u305f\u3089\u3001\u4e3b\u30ad\u30fc\u3084\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u306e\u91cd\u8907\u3067\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3057\u305f\u3001\u3068\u3044\u3046\u7d4c\u9a13\u306f\u8ab0\u306b\u3067\u3082\u3042\u308b\u3067\u3057\u3087\u3046\u3002\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u53e4\u5178\u7684\u306a\u65b9\u6cd5\u306f\u3001\u6b21\u306e\u3088\u3046\u306a\u624b\u9806\u3092\u8e0f\u3080\u3053\u3068\u3067\u3057\u305f\u3002<\/p>\n<ol>\n<li><code>SELECT<\/code> \u6587\u3067\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u78ba\u8a8d\u3059\u308b\u3002<\/li>\n<li>\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30b3\u30fc\u30c9\u3067\u3001\u5b58\u5728\u3057\u306a\u3044 (<code>NOT EXISTS<\/code>) \u306a\u3089 <code>INSERT<\/code> \u3092\u5b9f\u884c\u3002<\/li>\n<li>\u5b58\u5728\u3059\u308b\u5834\u5408 (<code>EXISTS<\/code>) \u306f <code>UPDATE<\/code> \u3092\u5b9f\u884c\u3002<\/li>\n<\/ol>\n<p>\u3057\u304b\u3057\u3053\u306e\u65b9\u6cd5\u306b\u306f\u3001<strong>\u2460\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3068\u306e\u901a\u4fe1\u304c2\u56de\u767a\u751f\u3057\u975e\u52b9\u7387<\/strong>\u3001<strong>\u2461\u78ba\u8a8d\u3068\u5b9f\u884c\u306e\u9593\u306b\u5225\u306e\u51e6\u7406\u304c\u5272\u308a\u8fbc\u3080\u300c\u7af6\u5408\u72b6\u614b\uff08Race Condition\uff09\u300d\u306e\u30ea\u30b9\u30af\u304c\u3042\u308b<\/strong>\u3001\u3068\u3044\u3046\u5f31\u70b9\u304c\u3042\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u3053\u306e\u300c<strong>\u3082\u3057\u5b58\u5728\u3059\u308b\u306a\u3089\u66f4\u65b0\u3001\u5b58\u5728\u3057\u306a\u3044\u306a\u3089\u633f\u5165<\/strong>\u300d\u3092\u3001\u4e00\u5ea6\u306e\u547d\u4ee4\u3067\u5b89\u5168\uff08\u30a2\u30c8\u30df\u30c3\u30af\uff09\u306b\u5b9f\u884c\u3059\u308b\u64cd\u4f5c\u3092 <strong>UPSERT<\/strong> (UPdate + inSERT) \u3068\u547c\u3073\u307e\u3059\u3002UPSERT\u3092\u5b9f\u73fe\u3059\u308b\u69cb\u6587\u306fDBMS\u306b\u3088\u3063\u3066\u7570\u306a\u308a\u307e\u3059\u304c\u3001\u3053\u3053\u3067\u306f\u4ee3\u8868\u7684\u306a3\u3064\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li><code>MERGE<\/code>\u6587\uff1aSQL\u306e\u6a19\u6e96\u898f\u683c\u3067\u5b9a\u3081\u3089\u308c\u3066\u3044\u308b\u591a\u6a5f\u80fd\u306a\u69cb\u6587<\/li>\n<li><code>ON CONFLICT DO UPDATE<\/code>\uff1aPostgreSQL\u306a\u3069\u3067\u4f7f\u308f\u308c\u308b\u3001<code>INSERT<\/code>\u6587\u306e\u62e1\u5f35\u69cb\u6587<\/li>\n<li><code>ON DUPLICATE KEY UPDATE<\/code>\uff1aMySQL\u3067\u4f7f\u308f\u308c\u308b\u3001\u3053\u3061\u3089\u3082<code>INSERT<\/code>\u6587\u306e\u62e1\u5f35\u69cb\u6587<\/li>\n<\/ul>\n<h3>SQL\u6a19\u6e96\u306e <code>MERGE<\/code> \u6587<\/h3>\n<p><code>MERGE<\/code>\u6587\u306f\u3001\u66f4\u65b0\u5143\u30c7\u30fc\u30bf\uff08\u30bd\u30fc\u30b9\uff09\u3068\u66f4\u65b0\u5148\u30c6\u30fc\u30d6\u30eb\uff08\u30bf\u30fc\u30b2\u30c3\u30c8\uff09\u3092\u7d50\u5408\u6761\u4ef6\u3067\u6bd4\u8f03\u3057\u3001\u4e00\u81f4\u3057\u305f\uff08<code>MATCHED<\/code>\uff09\u5834\u5408\u3068\u3001\u4e00\u81f4\u3057\u306a\u304b\u3063\u305f\uff08<code>NOT MATCHED<\/code>\uff09\u5834\u5408\u3067\u3001\u305d\u308c\u305e\u308c\u5b9f\u884c\u3059\u308b\u51e6\u7406\uff08<code>UPDATE<\/code>, <code>INSERT<\/code>, <code>DELETE<\/code>\uff09\u3092\u5b9a\u7fa9\u3067\u304d\u308b\u975e\u5e38\u306b\u5f37\u529b\u306a\u69cb\u6587\u3067\u3059\u3002<\/p>\n<h4>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u65e5\u3005\u306e\u5165\u8377\u30c7\u30fc\u30bf\u3067\u5728\u5eab\u30de\u30b9\u30bf\u3092\u66f4\u65b0\u3059\u308b<\/h4>\n<p><code>product_id<\/code> \u304c <code>P002<\/code> \u306e\u300c\u307f\u304b\u3093\u300d\u306f\u5728\u5eab\u304c\u3059\u3067\u306b\u3042\u308b\u306e\u3067<strong>\u66f4\u65b0 (UPDATE)<\/strong>\u3001<code>P003<\/code> \u306e\u65b0\u5546\u54c1\u306f\u5728\u5eab\u304c\u306a\u3044\u306e\u3067<strong>\u65b0\u898f\u767b\u9332 (INSERT)<\/strong> \u3057\u305f\u3044\u3067\u3059\u3002<code>MERGE<\/code>\u6587\u3092\u4f7f\u3046\u3068\u3001\u3053\u306e\u51e6\u7406\u30921\u30af\u30a8\u30ea\u3067\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code>-- SQL Server, Oracle \u306a\u3069\u3067\u5229\u7528\u53ef\u80fd\nMERGE INTO product_stock AS target\nUSING daily_arrivals AS source\nON (target.product_id = source.product_id)\nWHEN MATCHED THEN\n    -- \u4e00\u81f4\u3057\u305f\u5834\u5408\uff1a\u5728\u5eab\u6570\u3092\u52a0\u7b97\u3057\u3066\u66f4\u65b0\n    UPDATE SET target.quantity = target.quantity + source.arrival_quantity\nWHEN NOT MATCHED THEN\n    -- \u4e00\u81f4\u3057\u306a\u304b\u3063\u305f\u5834\u5408\uff1a\u65b0\u3057\u3044\u5546\u54c1\u3068\u3057\u3066\u633f\u5165\n    INSERT (product_id, product_name, quantity)\n    VALUES (source.product_id, '\uff08\u5546\u54c1\u540d\u672a\u767b\u9332\uff09', source.arrival_quantity);\n<\/code><\/pre>\n<h4>&#x1f3e2; \u3053\u3093\u306a\u4ed5\u4e8b\u3067\u5f79\u7acb\u3064\uff01<\/h4>\n<ul>\n<li><strong>ETL\/\u30d0\u30c3\u30c1\u51e6\u7406<\/strong>: \u591c\u9593\u30d0\u30c3\u30c1\u3067\u3001\u57fa\u5e79\u30b7\u30b9\u30c6\u30e0\u304b\u3089\u62bd\u51fa\u3057\u305f\u5dee\u5206\u30c7\u30fc\u30bf\u3092DWH\uff08\u30c7\u30fc\u30bf\u30a6\u30a7\u30a2\u30cf\u30a6\u30b9\uff09\u306b\u53cd\u6620\u3055\u305b\u308b\u51e6\u7406\u3002<\/li>\n<li><strong>\u30c7\u30fc\u30bf\u540c\u671f<\/strong>: \u8907\u6570\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u9593\u3067\u30de\u30b9\u30bf\u30c7\u30fc\u30bf\u3092\u540c\u671f\u3059\u308b\u5834\u9762\u3002<\/li>\n<\/ul>\n<h3>PostgreSQL\u306e <code>ON CONFLICT DO UPDATE<\/code><\/h3>\n<p>PostgreSQL\u3067\u306f\u3001<code>INSERT<\/code>\u6587\u306b<code>ON CONFLICT<\/code>\u53e5\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u3088\u308a\u76f4\u611f\u7684\u306bUPSERT\u3092\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><code>-- PostgreSQL, SQLite \u306a\u3069\u3067\u5229\u7528\u53ef\u80fd\nINSERT INTO product_stock (product_id, quantity)\nVALUES ('P002', 50), ('P003', 80)\nON CONFLICT (product_id) DO UPDATE SET\n    quantity = product_stock.quantity + excluded.arrival_quantity;\n<\/code><\/pre>\n<h3>MySQL\u306e <code>ON DUPLICATE KEY UPDATE<\/code><\/h3>\n<p>MySQL\u306b\u3082\u540c\u69d8\u306e\u69cb\u6587\u304c\u3042\u308a\u307e\u3059\u3002\u4e3b\u30ad\u30fc\u307e\u305f\u306f\u30e6\u30cb\u30fc\u30af\u30ad\u30fc\u304c\u91cd\u8907\u3057\u305f\u5834\u5408\u306e\u52d5\u4f5c\u3092<code>ON DUPLICATE KEY UPDATE<\/code>\u53e5\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre><code>-- MySQL, MariaDB \u306a\u3069\u3067\u5229\u7528\u53ef\u80fd\nINSERT INTO product_stock (product_id, quantity)\nVALUES ('P002', 50), ('P003', 80)\nON DUPLICATE KEY UPDATE\n    quantity = quantity + VALUES(quantity);\n<\/code><\/pre>\n<h4>&#x1f4dd; DBMS\u5225 UPSERT\u69cb\u6587\u306e\u307e\u3068\u3081<\/h4>\n<table border=\"1\">\n<thead>\n<tr>\n<th>\u69cb\u6587<\/th>\n<th>\u5bfe\u5fdcDBMS<\/th>\n<th>\u7279\u5fb4<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong><code>MERGE<\/code><\/strong><\/td>\n<td>SQL Server, Oracle, BigQuery<\/td>\n<td>SQL\u6a19\u6e96\u3002\u69cb\u6587\u306f\u3084\u3084\u8907\u96d1\u3060\u304c\u3001<code>DELETE<\/code>\u3082\u6307\u5b9a\u3067\u304d\u308b\u306a\u3069\u591a\u6a5f\u80fd\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong><code>ON CONFLICT DO UPDATE<\/code><\/strong><\/td>\n<td>PostgreSQL, SQLite<\/td>\n<td><code>INSERT<\/code>\u6587\u306e\u62e1\u5f35\u3067\u76f4\u611f\u7684\u3002\u30e6\u30cb\u30fc\u30af\u5236\u7d04\u9055\u53cd\u304c\u30c8\u30ea\u30ac\u30fc\u3068\u306a\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong><code>ON DUPLICATE KEY UPDATE<\/code><\/strong><\/td>\n<td>MySQL, MariaDB<\/td>\n<td>\u3053\u3061\u3089\u3082<code>INSERT<\/code>\u6587\u306e\u62e1\u5f35\u3002\u4e3b\u30ad\u30fc or \u30e6\u30cb\u30fc\u30af\u5236\u7d04\u9055\u53cd\u304c\u30c8\u30ea\u30ac\u30fc\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>CTE\uff08WITH\u53e5\uff09\u306e\u6d3b\u7528\u6cd5\u2502\u8907\u96d1\u306aSQL\u3092\u90e8\u54c1\u5316\u3057\u3066\u53ef\u8aad\u6027\u3092\u5287\u7684\u306b\u5411\u4e0a<\/h2>\n<p>\u30b5\u30d6\u30af\u30a8\u30ea\u304c\u4f55\u91cd\u306b\u3082\u30cd\u30b9\u30c8\uff08\u5165\u308c\u5b50\u306b\uff09\u3055\u308c\u3001\u30a4\u30f3\u30c7\u30f3\u30c8\u304c\u6df1\u304f\u306a\u308a\u3059\u304e\u3066\u3001\u3082\u306f\u3084\u89e3\u8aad\u4e0d\u80fd\u306aSQL\u6587\u3001\u901a\u79f0\u300c\u79d8\u4f1d\u306e\u30bf\u30ec\u300d\u5316\u3057\u305f\u30af\u30a8\u30ea\u306b\u906d\u9047\u3057\u305f\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u304b\uff1f\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u5f37\u529b\u306a\u6b66\u5668\u304c <strong>CTE (Common Table Expression)<\/strong>\u3001\u901a\u79f0 <strong><code>WITH<\/code>\u53e5<\/strong> \u3067\u3059\u3002<\/p>\n<p><code>WITH<\/code>\u53e5\u3092\u4f7f\u3046\u3068\u3001\u8907\u96d1\u306aSQL\u3092<strong>\u610f\u5473\u306e\u3042\u308b\u5358\u4f4d\u3067\u90e8\u54c1\u5316\u3057\u3001\u305d\u308c\u305e\u308c\u306b\u540d\u524d\u3092\u4ed8\u3051\u308b<\/strong>\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u7d50\u679c\u3068\u3057\u3066\u3001SQL\u306e<strong>\u53ef\u8aad\u6027<\/strong>\u3068<strong>\u30e1\u30f3\u30c6\u30ca\u30f3\u30b9\u6027<\/strong>\u304c\u5287\u7684\u306b\u5411\u4e0a\u3057\u307e\u3059\u3002<\/p>\n<h3>\u57fa\u672c\u306e<code>WITH<\/code>\u53e5\uff1a\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u64b2\u6ec5\u3057\u3001\u53ef\u8aad\u6027\u3092\u624b\u306b\u5165\u308c\u308b<\/h3>\n<p><code>WITH<\/code>\u53e5\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u306f\u3001<code>SELECT<\/code>\u6587\u306e\u524d\u306b <code>WITH \u540d\u524d AS (SELECT ...)<\/code> \u3068\u3044\u3046\u5f62\u3067\u3001\u4e00\u6642\u7684\u306a\u7d50\u679c\u30bb\u30c3\u30c8\u3092\u5b9a\u7fa9\u3059\u308b\u3053\u3068\u3067\u3059\u3002<\/p>\n<h4>&#x1f373; \u8eab\u8fd1\u306a\u4f8b\u3048\uff1a\u6599\u7406\u306e\u4e0b\u3054\u3057\u3089\u3048<\/h4>\n<p><code>WITH<\/code>\u53e5\u306f\u6599\u7406\u306e\u300c\u4e0b\u3054\u3057\u3089\u3048\u300d\u306b\u4f3c\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong>\u30b5\u30d6\u30af\u30a8\u30ea\u3060\u3089\u3051\u306eSQL<\/strong>: \u5168\u3066\u306e\u98df\u6750\u3068\u8abf\u7406\u5de5\u7a0b\u3092\u4e00\u3064\u306e\u934b\u306b\u4e00\u5ea6\u306b\u653e\u308a\u8fbc\u3080\u3088\u3046\u306a\u3082\u306e\u3002<\/li>\n<li><strong><code>WITH<\/code>\u53e5\u3092\u4f7f\u3063\u305fSQL<\/strong>:\n<ol>\n<li>\u91ce\u83dc\u3092\u5207\u3063\u3066\u30dc\u30a6\u30eb\u306b\u5165\u308c\u3066\u304a\u304f (<code>WITH veges AS (...)<\/code>)<\/li>\n<li>\u30bd\u30fc\u30b9\u3092\u5225\u306e\u5668\u3067\u6df7\u305c\u3066\u304a\u304f (<code>WITH sauce AS (...)<\/code>)<\/li>\n<li>\u6700\u5f8c\u306b\u30d5\u30e9\u30a4\u30d1\u30f3\u30671\u30682\u3092\u7092\u3081\u5408\u308f\u305b\u308b (\u30e1\u30a4\u30f3\u306e<code>SELECT<\/code>)<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h4>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u90e8\u9580\u5225\u306e\u58f2\u4e0a\u3068\u5f93\u696d\u54e1\u6570\u3092\u7b97\u51fa\u3059\u308b<\/h4>\n<p><strong>After: <code>WITH<\/code>\u53e5\u3067\u66f8\u304d\u63db\u3048\u305f\u4f8b<\/strong><\/p>\n<pre><code>-- \u51e6\u7406\u306e\u6d41\u308c\u304c\u4e0a\u304b\u3089\u4e0b\u3078\u3001\u76f4\u7dda\u7684\u3067\u5206\u304b\u308a\u3084\u3059\u3044\nWITH\n  -- \u90e8\u54c11: \u90e8\u9580\u3054\u3068\u306e\u58f2\u4e0a\u5408\u8a08\u3092\u8a08\u7b97\n  dept_sales AS (\n    SELECT\n      dept_id,\n      SUM(amount) AS total_amount\n    FROM sales\n    GROUP BY dept_id\n  ),\n  -- \u90e8\u54c12: \u90e8\u9580\u3054\u3068\u306e\u5f93\u696d\u54e1\u6570\u3092\u8a08\u7b97\n  dept_employees AS (\n    SELECT\n      dept_id,\n      COUNT(*) AS num_employees\n    FROM employees\n    GROUP BY dept_id\n  )\n-- \u90e8\u54c11\u3068\u90e8\u54c12\u3092\u7d50\u5408\u3057\u3066\u6700\u7d42\u7d50\u679c\u3092\u751f\u6210\nSELECT\n    d.dept_name,\n    ds.total_amount,\n    de.num_employees\nFROM\n    departments d\nJOIN dept_sales ds ON d.dept_id = ds.dept_id\nJOIN dept_employees de ON d.dept_id = de.dept_id;\n<\/code><\/pre>\n<h3>\u518d\u5e30CTE\uff1a\u7d44\u7e54\u56f3\u306a\u3069\u306e\u968e\u5c64\u69cb\u9020\u30c7\u30fc\u30bf\u3092\u81ea\u5728\u306b\u6271\u3046<\/h3>\n<p><code>WITH<\/code>\u53e5\u306e\u3082\u3046\u4e00\u3064\u306e\u5f37\u529b\u306a\u6a5f\u80fd\u304c\u300c<strong>\u518d\u5e30<\/strong>\u300d\u3067\u3059\u3002\u3053\u308c\u306f\u3001<strong>\u6df1\u3055\u304c\u4e0d\u5b9a\u306a\u968e\u5c64\u69cb\u9020\uff08\u30c4\u30ea\u30fc\u69cb\u9020\uff09\u306e\u30c7\u30fc\u30bf<\/strong>\u3092\u6271\u3046\u969b\u306b\u7d76\u5927\u306a\u5a01\u529b\u3092\u767a\u63ee\u3057\u307e\u3059\u3002<\/p>\n<h4>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u7d44\u7e54\u56f3\u306e\u968e\u5c64\u3092\u5c55\u958b\u3059\u308b<\/h4>\n<pre><code>-- '\u7530\u4e2d\u90e8\u9577' (id=10) \u914d\u4e0b\u306e\u5168\u7d44\u7e54\u3092\u53d6\u5f97\u3059\u308b\nWITH RECURSIVE subordinates AS (\n  -- 1. \u30a2\u30f3\u30ab\u30fc\u30e1\u30f3\u30d0\u30fc\uff1a\u518d\u5e30\u306e\u8d77\u70b9\n  SELECT\n    id, name, manager_id, 1 AS depth\n  FROM employees\n  WHERE id = 10\n\n  UNION ALL\n\n  -- 2. \u518d\u5e30\u30e1\u30f3\u30d0\u30fc\uff1a\u30a2\u30f3\u30ab\u30fc\u30e1\u30f3\u30d0\u30fc\u306e\u7d50\u679c\u3068\u81ea\u5df1\u7d50\u5408\n  SELECT\n    e.id, e.name, e.manager_id, s.depth + 1\n  FROM employees e\n  JOIN subordinates s ON e.manager_id = s.id\n)\nSELECT * FROM subordinates;\n<\/code><\/pre>\n<h4>&#x1f3e2; \u3053\u3093\u306a\u4ed5\u4e8b\u3067\u5f79\u7acb\u3064\uff01<\/h4>\n<ul>\n<li><strong>\u7d44\u7e54\u56f3<\/strong>: \u7279\u5b9a\u306e\u90e8\u7f72\u914d\u4e0b\u306e\u5168\u5f93\u696d\u54e1\u3092\u30ea\u30b9\u30c8\u30a2\u30c3\u30d7\u3059\u308b\u3002<\/li>\n<li><strong>\u90e8\u54c1\u8868 (BOM)<\/strong>: \u3042\u308b\u88fd\u54c1\u3092\u7d44\u307f\u7acb\u3066\u308b\u306e\u306b\u5fc5\u8981\u306a\u5168\u30d1\u30fc\u30c4\u3092\u5c55\u958b\u3059\u308b\u3002<\/li>\n<li><strong>Web\u30b5\u30a4\u30c8\u306e\u30ab\u30c6\u30b4\u30ea<\/strong>: \u30d1\u30f3\u304f\u305a\u30ea\u30b9\u30c8\u3092\u751f\u6210\u3059\u308b\u3002<\/li>\n<\/ul>\n<h2>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u3092\u4f7f\u3044\u3053\u306a\u3059\u2502SQL\u3067\u884c\u5185\u6bd4\u8f03\u3084\u9806\u4f4d\u4ed8\u3051\u3092\u81ea\u5728\u306b<\/h2>\n<p><code>GROUP BY<\/code> \u3092\u4f7f\u3063\u305f\u96c6\u8a08\u306fSQL\u306e\u57fa\u672c\u3067\u3059\u304c\u3001\u96c6\u8a08\u3059\u308b\u3068\u500b\u3005\u306e\u884c\u304c\u6301\u3064\u8a73\u7d30\u60c5\u5831\u304c\u5931\u308f\u308c\u3066\u3057\u307e\u3046\u3001\u3068\u3044\u3046\u5927\u304d\u306a\u5236\u7d04\u304c\u3042\u308a\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001\u300c\u5404\u5f93\u696d\u54e1\u306e\u7d66\u4e0e\u3068\u3001\u305d\u306e\u5f93\u696d\u54e1\u304c\u6240\u5c5e\u3059\u308b<strong>\u90e8\u9580\u306e\u5e73\u5747\u7d66\u4e0e<\/strong>\u3092\u4e26\u3079\u3066\u8868\u793a\u3057\u305f\u3044\u300d\u3068\u3044\u3046\u8981\u6c42\u306b\u3001<code>GROUP BY<\/code>\u3060\u3051\u3067\u5fdc\u3048\u308b\u306e\u306f\u56f0\u96e3\u3067\u3059\u3002<\/p>\n<p>\u3053\u306e\u8ab2\u984c\u3092\u30a8\u30ec\u30ac\u30f3\u30c8\u306b\u89e3\u6c7a\u3059\u308b\u306e\u304c<strong>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570<\/strong>\u3067\u3059\u3002<\/p>\n<p>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u306f\u3001<code>GROUP BY<\/code>\u306e\u3088\u3046\u306b\u884c\u3092\u4e00\u3064\u306b\u307e\u3068\u3081\u308b\u3053\u3068\u306a\u304f\u3001\u300c\u73fe\u5728\u306e\u884c\u306b\u95a2\u9023\u3059\u308b\u884c\u306e\u96c6\u307e\u308a\uff08=\u30a6\u30a3\u30f3\u30c9\u30a6\uff09\u300d\u306b\u5bfe\u3057\u3066\u8a08\u7b97\u3092\u884c\u3044\u307e\u3059\u3002\u7d50\u679c\u3068\u3057\u3066\u3001<strong>\u500b\u3005\u306e\u884c\u306e\u60c5\u5831\u3092\u4fdd\u6301\u3057\u305f\u307e\u307e<\/strong>\u3001\u96c6\u8a08\u3084\u9806\u4f4d\u4ed8\u3051\u306e\u7d50\u679c\u3092\u65b0\u3057\u3044\u5217\u3068\u3057\u3066\u8ffd\u52a0\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u8eab\u8fd1\u306a\u4f8b\u3067\u8a00\u3048\u3070\u3001<code>GROUP BY<\/code>\u304c\u300c3\u5e741\u7d44\u306e\u30c6\u30b9\u30c8\u306e\u5e73\u5747\u70b9\u306f80\u70b9\u300d\u3068\u3044\u3046\u8981\u7d04\u30921\u884c\u3060\u3051\u51fa\u3059\u306e\u306b\u5bfe\u3057\u3001\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u306f\u30af\u30e9\u30b9\u540d\u7c3f\u306e\u751f\u5f92\u4e00\u4eba\u3072\u3068\u308a\u306e\u6a2a\u306b\u300c\u3042\u306a\u305f\u306e\u70b9\u6570\uff1a85\u70b9\u3001\u30af\u30e9\u30b9\u5e73\u5747\uff1a80\u70b9\u300d\u3068\u66f8\u304d\u8fbc\u3093\u3067\u304f\u308c\u308b\u3088\u3046\u306a\u3082\u306e\u3067\u3059\u3002<\/p>\n<h3>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u306e\u57fa\u672c\u69cb\u6587\uff1a <code>OVER()<\/code>\u53e5<\/h3>\n<p>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u306f\u3001\u5fc5\u305a <code>OVER()<\/code> \u3068\u3044\u3046\u53e5\u3092\u4f34\u3063\u3066\u4f7f\u7528\u3055\u308c\u307e\u3059\u3002\u3053\u306e <code>OVER()<\/code> \u306e\u4e2d\u306b\u3001\u3069\u306e\u7bc4\u56f2\uff08\u30a6\u30a3\u30f3\u30c9\u30a6\uff09\u3067\u8a08\u7b97\u3092\u884c\u3046\u304b\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre><code>\u95a2\u6570\u540d(\u5f15\u6570) OVER (\n    [PARTITION BY \u5217\u540d...]  -- \u30a6\u30a3\u30f3\u30c9\u30a6\u3092\u5206\u5272\u3059\u308b\u57fa\u6e96\uff08\u3069\u306e\u30b0\u30eb\u30fc\u30d7\u3067\uff1f\uff09\n    [ORDER BY \u5217\u540d...]     -- \u30a6\u30a3\u30f3\u30c9\u30a6\u5185\u306e\u9806\u5e8f\u3092\u5b9a\u7fa9\uff08\u3069\u306e\u9806\u756a\u3067\uff1f\uff09\n)\n<\/code><\/pre>\n<ul>\n<li><code>PARTITION BY<\/code>: \u30a6\u30a3\u30f3\u30c9\u30a6\u3092\u533a\u5207\u308b\u305f\u3081\u306e\u5217\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002\u300c\u90e8\u9580\u3054\u3068\u300d\u300c\u5546\u54c1\u30ab\u30c6\u30b4\u30ea\u3054\u3068\u300d\u3068\u3044\u3063\u305f\u30b0\u30eb\u30fc\u30d7\u3092\u5b9a\u7fa9\u3059\u308b\u3001<strong>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u7248\u306e<code>GROUP BY<\/code><\/strong>\u3067\u3059\u3002<\/li>\n<li><code>ORDER BY<\/code>: <code>PARTITION BY<\/code>\u3067\u533a\u5207\u3089\u308c\u305f\u30a6\u30a3\u30f3\u30c9\u30a6\u306e\u4e2d\u3067\u3001\u3069\u306e\u3088\u3046\u306a\u9806\u5e8f\u3067\u884c\u3092\u4e26\u3079\u308b\u304b\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002\u7279\u306b\u9806\u4f4d\u4ed8\u3051\u3084\u7d2f\u8a08\u3092\u8a08\u7b97\u3059\u308b\u969b\u306b\u5fc5\u9808\u3067\u3059\u3002<\/li>\n<\/ul>\n<h3>\u4ee3\u8868\u7684\u306a\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u3068\u5177\u4f53\u4f8b<\/h3>\n<h4>1. \u9806\u4f4d\u4ed8\u3051\uff1a<code>ROW_NUMBER<\/code>, <code>RANK<\/code>, <code>DENSE_RANK<\/code><\/h4>\n<ul>\n<li><code>ROW_NUMBER()<\/code>: \u540c\u70b9\u3067\u3082\u91cd\u8907\u3057\u306a\u3044\u4e00\u610f\u306e\u9023\u756a\u3092\u632f\u308b (1, 2, 3, 4)<\/li>\n<li><code>RANK()<\/code>: \u540c\u70b9\u306b\u306f\u540c\u3058\u9806\u4f4d\u3092\u4ed8\u3051\u3001\u6b21\u306e\u9806\u4f4d\u306f\u98db\u3070\u3059 (1, 2, 2, 4)<\/li>\n<li><code>DENSE_RANK()<\/code>: \u540c\u70b9\u306b\u306f\u540c\u3058\u9806\u4f4d\u3092\u4ed8\u3051\u3001\u6b21\u306e\u9806\u4f4d\u306f\u98db\u3070\u3055\u306a\u3044 (1, 2, 2, 3)<\/li>\n<\/ul>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u90e8\u9580\u3054\u3068\u306b\u7d66\u4e0e\u306e\u9ad8\u3044\u5f93\u696d\u54e1\u3092\u9806\u4f4d\u4ed8\u3051\u3059\u308b<\/strong><\/p>\n<pre><code>SELECT\n    name, department, salary,\n    ROW_NUMBER() OVER(PARTITION BY department ORDER BY salary DESC) AS row_num,\n    RANK()       OVER(PARTITION BY department ORDER BY salary DESC) AS rank_num,\n    DENSE_RANK() OVER(PARTITION BY department ORDER BY salary DESC) AS dense_rank_num\nFROM\n    employees;\n<\/code><\/pre>\n<h4>2. \u96c6\u8a08\uff1a<code>SUM<\/code>, <code>AVG<\/code>, <code>COUNT<\/code> \u306a\u3069<\/h4>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u5f93\u696d\u54e1\u306e\u7d66\u4e0e\u3068\u90e8\u9580\u5e73\u5747\u7d66\u4e0e\u3092\u6bd4\u8f03\u3059\u308b<\/strong><\/p>\n<pre><code>SELECT\n    name, department, salary,\n    AVG(salary) OVER(PARTITION BY department) AS avg_salary_in_dept\nFROM\n    employees;\n<\/code><\/pre>\n<h4>3. <code>ROW_NUMBER<\/code>\u306b\u3088\u308b\u91cd\u8907\u6392\u9664<\/h4>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9\u304c\u91cd\u8907\u3057\u3066\u3044\u308b\u30e6\u30fc\u30b6\u30fc\u306e\u3046\u3061\u3001\u6700\u65b0\u306e1\u4ef6\u3060\u3051\u3092\u6b8b\u3059<\/strong><\/p>\n<pre><code>WITH RankedUsers AS (\n    SELECT\n        user_id, email, created_at,\n        ROW_NUMBER() OVER(PARTITION BY email ORDER BY created_at DESC) AS rn\n    FROM\n        users\n)\nDELETE FROM users\nWHERE user_id IN (SELECT user_id FROM RankedUsers WHERE rn &gt; 1);\n<\/code><\/pre>\n<h3><code>QUALIFY<\/code>\u53e5\uff1a\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u306e\u7d50\u679c\u3067\u7d5e\u308a\u8fbc\u3080<\/h3>\n<p><code>QUALIFY<\/code>\u53e5\u306f\u3001<strong>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u306e\u305f\u3081\u306e<code>HAVING<\/code>\u53e5<\/strong>\u3068\u8003\u3048\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a<code>QUALIFY<\/code>\u3067\u90e8\u9580\u3054\u3068\u30c8\u30c3\u30d72\u306e\u5f93\u696d\u54e1\u3092\u62bd\u51fa\u3059\u308b<\/strong><\/p>\n<pre><code>-- BigQuery, Snowflake\u306a\u3069\u3067\u5229\u7528\u53ef\u80fd\nSELECT\n    name, department, salary\nFROM\n    employees\nQUALIFY\n    RANK() OVER(PARTITION BY department ORDER BY salary DESC) &lt;= 2;\n<\/code><\/pre>\n<h2>\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u5fdc\u7528\uff1aEXISTS\u306b\u3088\u308b\u5b58\u5728\u30c1\u30a7\u30c3\u30af\u3068\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u4ed5\u7d44\u307f<\/h2>\n<p>\u3053\u3053\u3067\u306f\u3001<code>IN<\/code>\u3088\u308a\u3082\u52b9\u7387\u7684\u306a\u5834\u5408\u304c\u591a\u3044 <code>EXISTS<\/code>\u53e5\u3068\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u7406\u89e3\u3059\u308b\u4e0a\u3067\u907f\u3051\u3066\u306f\u901a\u308c\u306a\u3044\u300c\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u300d\u306e\u6982\u5ff5\u306b\u3064\u3044\u3066\u6398\u308a\u4e0b\u3052\u307e\u3059\u3002\u3053\u306e\u9055\u3044\u3092\u7406\u89e3\u3059\u308b\u3053\u3068\u306f\u3001\u5fdc\u7528\u60c5\u5831\u30fbDB\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u306e\u9577\u6587SQL\u3092\u8aad\u89e3\u3059\u308b\u4e0a\u3067\u6975\u3081\u3066\u91cd\u8981\u3067\u3059\u3002<\/p>\n<h3><code>EXISTS<\/code> \/ <code>NOT EXISTS<\/code>\uff1a\u52b9\u7387\u7684\u306a\u5b58\u5728\u30c1\u30a7\u30c3\u30af<\/h3>\n<p><code>EXISTS<\/code>\u306f\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u304c<strong>1\u884c\u3067\u3082\u7d50\u679c\u3092\u8fd4\u305b\u3070\u771f\uff08TRUE\uff09<\/strong>\u3001<strong>1\u884c\u3082\u8fd4\u3055\u306a\u3051\u308c\u3070\u507d\uff08FALSE\uff09<\/strong>\u3092\u8fd4\u3059\u6f14\u7b97\u5b50\u3067\u3059\u3002\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306f\u6761\u4ef6\u306b\u5408\u3046\u884c\u3092<strong>1\u884c\u898b\u3064\u3051\u305f\u6642\u70b9\u3067\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u5b9f\u884c\u3092\u6253\u3061\u5207\u308c\u308b<\/strong>\u305f\u3081\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u4e0a\u6709\u5229\u306b\u306a\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u4e00\u5ea6\u3067\u3082\u5546\u54c1\u3092\u8cfc\u5165\u3057\u305f\u3053\u3068\u304c\u3042\u308b\u9867\u5ba2\u3092\u62bd\u51fa\u3059\u308b<\/strong><\/p>\n<pre><code>SELECT\n    c.customer_id, c.name\nFROM\n    customers c\nWHERE\n    EXISTS (\n        -- c.customer_id\u306b\u7d10\u3065\u304f\u6ce8\u6587\u304c1\u4ef6\u3067\u3082\u3042\u308c\u3070\u3001\u3053\u306e\u30b5\u30d6\u30af\u30a8\u30ea\u306fTRUE\u3092\u8fd4\u3059\n        SELECT 1\n        FROM orders o\n        WHERE o.customer_id = c.customer_id\n    );\n<\/code><\/pre>\n<h3>\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u3068\u975e\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u9055\u3044<\/h3>\n<h4>1. \u975e\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea (Non-correlated Subquery)<\/h4>\n<p>\u30b5\u30d6\u30af\u30a8\u30ea\u304c<strong>\u5916\u5074\u306e\u30af\u30a8\u30ea\u3068\u306f\u72ec\u7acb\u3057\u3066\u5358\u4f53\u3067\u5b9f\u884c\u3067\u304d\u308b<\/strong>\u3082\u306e\u3067\u3059\u3002\u5148\u306b\u30b5\u30d6\u30af\u30a8\u30ea\u304c<strong>\u4e00\u5ea6\u3060\u3051<\/strong>\u5b9f\u884c\u3055\u308c\u3001\u305d\u306e\u7d50\u679c\u3092\u4f7f\u3063\u3066\u5916\u5074\u306e\u30af\u30a8\u30ea\u304c\u51e6\u7406\u3055\u308c\u307e\u3059\u3002<\/p>\n<h4>2. \u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea (Correlated Subquery)<\/h4>\n<p>\u30b5\u30d6\u30af\u30a8\u30ea\u304c<strong>\u5916\u5074\u306e\u30af\u30a8\u30ea\u306e\u5404\u884c\u306e\u5024\u3092\u53c2\u7167\u3057\u3066\u3044\u308b<\/strong>\u305f\u3081\u3001\u5358\u4f53\u3067\u306f\u5b9f\u884c\u3067\u304d\u306a\u3044\u3082\u306e\u3067\u3059\u3002\u5916\u5074\u306e\u30af\u30a8\u30ea\u3067\u51e6\u7406\u3055\u308c\u308b<strong>\u5404\u884c\u306b\u5bfe\u3057\u3066\u3001\u30b5\u30d6\u30af\u30a8\u30ea\u304c\u7e70\u308a\u8fd4\u3057\u5b9f\u884c<\/strong>\u3055\u308c\u307e\u3059\u3002<\/p>\n<h4>&#x1f4dd; 2\u7a2e\u985e\u306e\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u6bd4\u8f03<\/h4>\n<table border=\"1\">\n<thead>\n<tr>\n<th><\/th>\n<th>\u975e\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea (Non-correlated)<\/th>\n<th>\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea (Correlated)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u5b9f\u884c\u30bf\u30a4\u30df\u30f3\u30b0<\/strong><\/td>\n<td>\u5916\u5074\u306e\u30af\u30a8\u30ea\u3088\u308a\u5148\u306b<strong>\u4e00\u5ea6\u3060\u3051<\/strong>\u5b9f\u884c<\/td>\n<td>\u5916\u5074\u306e\u30af\u30a8\u30ea\u306e<strong>\u5404\u884c\u3054\u3068<\/strong>\u306b\u7e70\u308a\u8fd4\u3057\u5b9f\u884c<\/td>\n<\/tr>\n<tr>\n<td><strong>\u4f9d\u5b58\u95a2\u4fc2<\/strong><\/td>\n<td>\u72ec\u7acb\u3057\u3066\u304a\u308a\u3001\u5358\u4f53\u3067\u5b9f\u884c\u53ef\u80fd<\/td>\n<td>\u5916\u5074\u306e\u30af\u30a8\u30ea\u306b\u4f9d\u5b58\u3057\u3001\u5358\u4f53\u3067\u306f\u5b9f\u884c\u4e0d\u53ef<\/td>\n<\/tr>\n<tr>\n<td><strong>\u4e3b\u306a\u7528\u9014<\/strong><\/td>\n<td>\u5168\u4f53\u96c6\u8a08\u5024\u3068\u306e\u6bd4\u8f03 (<code>IN<\/code>, <code>&gt;<\/code>, <code>&lt;<\/code>)<\/td>\n<td>\u884c\u3054\u3068\u306e\u5b58\u5728\u30c1\u30a7\u30c3\u30af (<code>EXISTS<\/code>)<\/td>\n<\/tr>\n<tr>\n<td><strong>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9<\/strong><\/td>\n<td>\u4e00\u822c\u7684\u306b\u9ad8\u901f<\/td>\n<td>\u5916\u5074\u306e\u884c\u6570\u304c\u591a\u3044\u3068\u4f4e\u901f\u306b\u306a\u308a\u304c\u3061\uff08\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u91cd\u8981\uff09<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>JOIN\u53e5\u306e\u4f7f\u3044\u5206\u3051\u5b8c\u5168\u30ac\u30a4\u30c9\u2502INNER, LEFT JOIN\u3068ON, WHERE\u306e\u9055\u3044<\/h2>\n<p>\u30ea\u30ec\u30fc\u30b7\u30e7\u30ca\u30eb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u5fc3\u81d3\u90e8\u3068\u3082\u8a00\u3048\u308b<code>JOIN<\/code>\u53e5\u3002\u591a\u304f\u306e\u958b\u767a\u8005\u304c<code>INNER JOIN<\/code>\u3068<code>LEFT JOIN<\/code>\u3092\u65e5\u5e38\u7684\u306b\u4f7f\u3063\u3066\u3044\u307e\u3059\u304c\u3001\u305d\u306e\u6319\u52d5\u306e\u9055\u3044\u3084\u3001<strong>\u6761\u4ef6\u3092<code>ON<\/code>\u53e5\u306b\u66f8\u304f\u304b<code>WHERE<\/code>\u53e5\u306b\u66f8\u304f\u304b<\/strong>\u3067\u7d50\u679c\u304c\u6839\u672c\u7684\u306b\u5909\u308f\u308b\u30b1\u30fc\u30b9\u306b\u3064\u3044\u3066\u3001\u81ea\u4fe1\u3092\u6301\u3063\u3066\u8aac\u660e\u3067\u304d\u308b\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p>\u3053\u306e<code>ON<\/code>\u3068<code>WHERE<\/code>\u306e\u9055\u3044\u306f\u3001\u5fdc\u7528\u60c5\u5831\u6280\u8853\u8005\u8a66\u9a13\u3084\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u306e\u5348\u5f8c\u554f\u984c\u3067\u983b\u51fa\u306e\u300c\u3072\u3063\u304b\u3051\u554f\u984c\u300d\u3067\u3042\u308b\u3068\u540c\u6642\u306b\u3001\u5b9f\u52d9\u3067\u610f\u56f3\u3057\u306a\u3044\u30d0\u30b0\u3092\u751f\u307f\u51fa\u3059\u539f\u56e0\u306b\u3082\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u3053\u306e\u30bb\u30af\u30b7\u30e7\u30f3\u3067\u306f\u3001\u57fa\u672c\u7684\u306aJOIN\u306e\u7a2e\u985e\u3092\u304a\u3055\u3089\u3044\u3057\u3064\u3064\u3001\u6700\u91cd\u8981\u30dd\u30a4\u30f3\u30c8\u3067\u3042\u308b<code>ON<\/code>\u53e5\u3068<code>WHERE<\/code>\u53e5\u306e\u4f7f\u3044\u5206\u3051\u306b\u3064\u3044\u3066\u5fb9\u5e95\u7684\u306b\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<h3>JOIN\u306e\u7a2e\u985e\u3068\u305d\u308c\u305e\u308c\u306e\u5f79\u5272<\/h3>\n<p>\u307e\u305a\u306f\u4e3b\u8981\u306aJOIN\u306e\u7a2e\u985e\u3092\u3001\u30d9\u30f3\u56f3\u306e\u30a4\u30e1\u30fc\u30b8\u3068\u5171\u306b\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<table border=\"1\">\n<thead>\n<tr>\n<th>JOIN\u306e\u7a2e\u985e<\/th>\n<th>\u5f79\u5272<\/th>\n<th>\u30a4\u30e1\u30fc\u30b8<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong><code>INNER JOIN<\/code><\/strong><\/td>\n<td>\u5185\u90e8\u7d50\u5408<\/td>\n<td>2\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3067\u3001\u7d50\u5408\u30ad\u30fc\u304c<strong>\u4e00\u81f4\u3059\u308b\u884c\u3060\u3051<\/strong>\u3092\u8fd4\u3059\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong><code>LEFT JOIN<\/code><\/strong><\/td>\n<td>\u5de6\u5916\u90e8\u7d50\u5408<\/td>\n<td><strong>\u5de6\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u884c<\/strong>\u3068\u3001\u305d\u308c\u306b\u4e00\u81f4\u3059\u308b\u53f3\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u3092\u8fd4\u3059\u3002\u4e00\u81f4\u3057\u306a\u3044\u5834\u5408\u306f\u53f3\u5074\u304c<code>NULL<\/code>\u306b\u306a\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong><code>RIGHT JOIN<\/code><\/strong><\/td>\n<td>\u53f3\u5916\u90e8\u7d50\u5408<\/td>\n<td><strong>\u53f3\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u884c<\/strong>\u3068\u3001\u305d\u308c\u306b\u4e00\u81f4\u3059\u308b\u5de6\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u3092\u8fd4\u3059\u3002<code>LEFT JOIN<\/code>\u306e\u5de6\u53f3\u9006\u30d0\u30fc\u30b8\u30e7\u30f3\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong><code>FULL OUTER JOIN<\/code><\/strong><\/td>\n<td>\u5b8c\u5168\u5916\u90e8\u7d50\u5408<\/td>\n<td><strong>\u4e21\u65b9\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u5168\u884c<\/strong>\u3092\u8fd4\u3059\u3002\u3069\u3061\u3089\u304b\u4e00\u65b9\u306b\u3057\u304b\u5b58\u5728\u3057\u306a\u3044\u884c\u3082<code>NULL<\/code>\u3067\u88dc\u5b8c\u3057\u3066\u8868\u793a\u3059\u308b\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong><code>SELF JOIN<\/code><\/strong><\/td>\n<td>\u81ea\u5df1\u7d50\u5408<\/td>\n<td>1\u3064\u306e\u30c6\u30fc\u30d6\u30eb\u3092\u3001\u5225\u540d\uff08\u30a8\u30a4\u30ea\u30a2\u30b9\uff09\u3092\u4ed8\u3051\u3066\u81ea\u5206\u81ea\u8eab\u3068\u7d50\u5408\u3059\u308b\u30c6\u30af\u30cb\u30c3\u30af\u3002\u7d44\u7e54\u56f3\u306a\u3069\u3067\u4f7f\u3046\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code>LEFT JOIN<\/code>\u306f\u300c\u4e3b\u30c6\u30fc\u30d6\u30eb\u306e\u30c7\u30fc\u30bf\u306f\u5168\u90e8\u898b\u305f\u3044\u304c\u3001\u95a2\u9023\u3059\u308b\u30c7\u30fc\u30bf\u304c\u3042\u308c\u3070\u305d\u308c\u3082\u6b32\u3057\u3044\u300d\u3068\u3044\u3046\u5834\u9762\u3067\u975e\u5e38\u306b\u5f79\u7acb\u3061\u307e\u3059\u3002<\/p>\n<h3>\u6700\u91cd\u8981\u30dd\u30a4\u30f3\u30c8\uff1a<code>ON<\/code>\u53e5\u3068<code>WHERE<\/code>\u53e5\u306e\u6761\u4ef6\u306e\u9055\u3044<\/h3>\n<p><code>ON<\/code>\u53e5\u3068<code>WHERE<\/code>\u53e5\u306f\u3069\u3061\u3089\u3082\u300c\u6761\u4ef6\u300d\u3092\u6307\u5b9a\u3057\u307e\u3059\u304c\u3001\u305d\u306e\u5f79\u5272\u3068\u5b9f\u884c\u30bf\u30a4\u30df\u30f3\u30b0\u304c\u5168\u304f\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong><code>ON<\/code>\u53e5<\/strong>: \u30c6\u30fc\u30d6\u30eb\u3092<strong>\u7d50\u5408\u3059\u308b\u305f\u3081\u306e\u6761\u4ef6<\/strong>\u3002\u3069\u306e\u884c\u3068\u3069\u306e\u884c\u3092\u7d10\u4ed8\u3051\u308b\u304b\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/li>\n<li><strong><code>WHERE<\/code>\u53e5<\/strong>: \u7d50\u5408\u304c<strong>\u5b8c\u4e86\u3057\u305f\u5f8c<\/strong>\u306e\u4eee\u60f3\u7684\u306a\u5de8\u5927\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u3001\u6700\u7d42\u7684\u306b\u8868\u793a\u3059\u308b\u884c\u3092<strong>\u7d5e\u308a\u8fbc\u3080\u305f\u3081\u306e\u6761\u4ef6<\/strong>\u3002<\/li>\n<\/ul>\n<h4><code>LEFT JOIN<\/code>\u306e\u5834\u5408\uff1a<code>ON<\/code>\u3068<code>WHERE<\/code>\u3067\u7d50\u679c\u304c\u5168\u304f\u7570\u306a\u308b\uff01<\/h4>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u300c\u5168\u90e8\u7f72\u300d\u3068\u300c\u6240\u5c5e\u3059\u308b&#8221;\u6b63\u793e\u54e1&#8221;\u300d\u3092\u4e00\u89a7\u8868\u793a\u3057\u305f\u3044<\/strong><\/p>\n<p><strong>1. \u6761\u4ef6\u3092<code>ON<\/code>\u53e5\u306b\u66f8\u3044\u305f\u5834\u5408<\/strong><\/p>\n<pre><code>SELECT d.dept_name, e.name, e.status\nFROM departments d\nLEFT JOIN employees e\n    ON d.dept_id = e.dept_id AND e.status = '\u6b63\u793e\u54e1'; -- ON\u53e5\u3067\u7d5e\u308a\u8fbc\u307f\n<\/code><\/pre>\n<p><strong>\u7d50\u679c<\/strong>: <strong>\u5168\u90e8\u7f72\u304c\u8868\u793a\u3055\u308c\u308b\u3002<\/strong>\u90e8\u7f72\u306b\u6b63\u793e\u54e1\u304c\u3044\u306a\u3044\u5834\u5408\u3001\u5f93\u696d\u54e1\u540d\u306f<code>NULL<\/code>\u3068\u306a\u308b\u3002\u300c\u5168\u90e8\u7f72\u3092\u57fa\u8ef8\u306b\u3001\u3082\u3057\u6b63\u793e\u54e1\u304c\u3044\u308c\u3070\u8868\u793a\u3059\u308b\u300d\u3068\u3044\u3046\u8981\u4ef6\u901a\u308a\u306e\u7d50\u679c\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><strong>2. \u6761\u4ef6\u3092<code>WHERE<\/code>\u53e5\u306b\u66f8\u3044\u305f\u5834\u5408<\/strong><\/p>\n<pre><code>SELECT d.dept_name, e.name, e.status\nFROM departments d\nLEFT JOIN employees e\n    ON d.dept_id = e.dept_id\nWHERE e.status = '\u6b63\u793e\u54e1'; -- WHERE\u53e5\u3067\u7d5e\u308a\u8fbc\u307f\n<\/code><\/pre>\n<p><strong>\u7d50\u679c<\/strong>: `LEFT JOIN`\u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u305f<code>NULL<\/code>\u306e\u884c\u304c<code>WHERE<\/code>\u53e5\u3067\u9664\u5916\u3055\u308c\u3066\u3057\u307e\u3044\u3001<strong><code>INNER JOIN<\/code>\u3068\u540c\u3058\u7d50\u679c<\/strong>\u306b\u306a\u308a\u307e\u3059\u3002\u300c\u6b63\u793e\u54e1\u304c\u3044\u308b\u90e8\u7f72\u300d\u3060\u3051\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002<\/p>\n<table border=\"1\">\n<thead>\n<tr>\n<th>\u53e5<\/th>\n<th><code>INNER JOIN<\/code>\u3067\u306e\u52d5\u4f5c<\/th>\n<th><code>LEFT JOIN<\/code>\u3067\u306e\u52d5\u4f5c<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong><code>ON<\/code>\u53e5\u306e\u6761\u4ef6<\/strong><\/td>\n<td>\u7d50\u5408\u3059\u308b\u884c\u3092\u5b9a\u7fa9\u3002(\u7d50\u679c\u306f<code>WHERE<\/code>\u3068\u540c\u3058)<\/td>\n<td><strong>\u5148\u306b<\/strong>\u53f3\u30c6\u30fc\u30d6\u30eb\u3092\u7d5e\u308a\u8fbc\u3093\u3067\u304b\u3089\u7d50\u5408\u3002<strong>\u5de6\u30c6\u30fc\u30d6\u30eb\u306e\u884c\u306f\u5168\u3066\u6b8b\u308b\u3002<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong><code>WHERE<\/code>\u53e5\u306e\u6761\u4ef6<\/strong><\/td>\n<td>\u7d50\u5408<strong>\u5f8c<\/strong>\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u3092\u7d5e\u308a\u8fbc\u3080\u3002<\/td>\n<td>\u7d50\u5408<strong>\u5f8c<\/strong>\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u3092\u7d5e\u308a\u8fbc\u3080\u3002\u53f3\u30c6\u30fc\u30d6\u30eb\u306e\u6761\u4ef6\u3067\u7d5e\u308b\u3068<strong><code>INNER JOIN<\/code>\u306e\u3088\u3046\u306b\u306a\u308b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>LATERAL JOIN\u3068APPLY\u53e5\u2502\u5404\u884c\u306b\u95a2\u6570\u306e\u3088\u3046\u306b\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u9069\u7528\u3059\u308b<\/h2>\n<p>\u901a\u5e38\u306e<code>JOIN<\/code>\u306b\u306f\u3001\u300c<code>FROM<\/code>\u53e5\u306b\u66f8\u304b\u308c\u305f\u30c6\u30fc\u30d6\u30eb\u306f\u3001\u305d\u308c\u3088\u308a\u524d\u306b\u66f8\u304b\u308c\u305f\u30c6\u30fc\u30d6\u30eb\u306e\u5217\u3092\u53c2\u7167\u3067\u304d\u306a\u3044\u300d\u3068\u3044\u3046\u5236\u7d04\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u306e\u304c\u3001<strong>LATERAL JOIN<\/strong> (PostgreSQL, Oracle) \u3068 <strong>APPLY\u53e5<\/strong> (SQL Server) \u3067\u3059\u3002<\/p>\n<p>\u3053\u308c\u3089\u306e\u69cb\u6587\u3092\u4f7f\u3046\u3068\u3001\u5de6\u5074\u306e\u30c6\u30fc\u30d6\u30eb\u306e<strong>\u5404\u884c\u306b\u5bfe\u3057\u3066<\/strong>\u3001\u305d\u306e\u884c\u306e\u5024\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u3066\u53f3\u5074\u306e\u30b5\u30d6\u30af\u30a8\u30ea\u3092\u7e70\u308a\u8fd4\u3057\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002\u307e\u308b\u3067\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e<code>for-each<\/code>\u30eb\u30fc\u30d7\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002<\/p>\n<h3><code>LATERAL JOIN<\/code> (PostgreSQL \/ Oracle)<\/h3>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u5404\u90e8\u7f72\u3067\u7d66\u4e0e\u304c\u9ad8\u3044\u4e0a\u4f4d2\u540d\u306e\u5f93\u696d\u54e1\u3092\u53d6\u5f97\u3059\u308b<\/strong><\/p>\n<pre><code>SELECT\n    d.dept_name,\n    top_e.name,\n    top_e.salary\nFROM\n    departments d,\nLATERAL (\n    SELECT e.name, e.salary\n    FROM employees e\n    WHERE e.dept_id = d.dept_id -- d\u306e\u5404\u884c\u306e\u5024\u3092\u53c2\u7167\n    ORDER BY e.salary DESC\n    LIMIT 2\n) AS top_e;\n<\/code><\/pre>\n<h3><code>CROSS APPLY<\/code> \/ <code>OUTER APPLY<\/code> (SQL Server)<\/h3>\n<p>SQL Server\u3067\u306f\u3001<code>APPLY<\/code>\u53e5\u304c\u540c\u69d8\u306e\u6a5f\u80fd\u3092\u63d0\u4f9b\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong><code>CROSS APPLY<\/code><\/strong>: <code>INNER JOIN<\/code>\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002<\/li>\n<li><strong><code>OUTER APPLY<\/code><\/strong>: <code>LEFT JOIN<\/code>\u306e\u3088\u3046\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a<code>CROSS APPLY<\/code>\u3067\u90e8\u7f72\u3054\u3068\u306e\u30c8\u30c3\u30d72\u540d\u3092\u53d6\u5f97<\/strong><\/p>\n<pre><code>SELECT\n    d.dept_name,\n    top_e.name,\n    top_e.salary\nFROM\n    departments d\nCROSS APPLY (\n    SELECT e.name, e.salary\n    FROM employees e\n    WHERE e.dept_id = d.dept_id\n    ORDER BY e.salary DESC\n    OFFSET 0 ROWS FETCH NEXT 2 ROWS ONLY\n) AS top_e;\n<\/code><\/pre>\n<table border=\"1\">\n<thead>\n<tr>\n<th>\u69cb\u6587<\/th>\n<th>\u5bfe\u5fdcDBMS<\/th>\n<th>\u52d5\u4f5c\u306e\u30a4\u30e1\u30fc\u30b8<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong><code>LATERAL JOIN<\/code><\/strong><\/td>\n<td>PostgreSQL, Oracle<\/td>\n<td><code>INNER JOIN<\/code>\u306e\u3088\u3046\u306b\u632f\u308b\u821e\u3046\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong><code>LEFT JOIN LATERAL ... ON TRUE<\/code><\/strong><\/td>\n<td>PostgreSQL, Oracle<\/td>\n<td><code>LEFT JOIN<\/code>\u306e\u3088\u3046\u306b\u632f\u308b\u821e\u3046\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong><code>CROSS APPLY<\/code><\/strong><\/td>\n<td>SQL Server<\/td>\n<td><code>INNER JOIN<\/code>\u3068<code>LATERAL<\/code>\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u76f8\u5f53\u3002<\/td>\n<\/tr>\n<tr>\n<td><strong><code>OUTER APPLY<\/code><\/strong><\/td>\n<td>SQL Server<\/td>\n<td><code>LEFT JOIN<\/code>\u3068<code>LATERAL<\/code>\u306e\u7d44\u307f\u5408\u308f\u305b\u306b\u76f8\u5f53\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>GROUPING SETS\/ROLLUP\/CUBE\u5165\u9580\u2502\u4e00\u5ea6\u306b\u591a\u69d8\u306a\u96c6\u8a08\u3092\u5b9f\u73fe\u3059\u308bSQL<\/h2>\n<p>\u30c7\u30fc\u30bf\u5206\u6790\u30ec\u30dd\u30fc\u30c8\u3067\u306f\u3001\u300c\u5730\u57df\u3054\u3068\u306e\u58f2\u4e0a\u300d\u300c\u5546\u54c1\u30ab\u30c6\u30b4\u30ea\u3054\u3068\u306e\u58f2\u4e0a\u300d\u300c\u5168\u793e\u5408\u8a08\u306e\u58f2\u4e0a\u300d\u3068\u3044\u3063\u305f\u69d8\u3005\u306a\u7c92\u5ea6\u3067\u306e\u96c6\u8a08\u5024\u304c\u6c42\u3081\u3089\u308c\u307e\u3059\u3002\u901a\u5e38\u3001\u3053\u308c\u3089\u3092\u53d6\u5f97\u3059\u308b\u306b\u306f\u3001\u305d\u308c\u305e\u308c\u5225\u306e<code>GROUP BY<\/code>\u30af\u30a8\u30ea\u3092\u66f8\u3044\u3066<code>UNION ALL<\/code>\u3067\u7d50\u5408\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u3057\u305f\u3002\u3053\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u306e\u304c\u3001<code>GROUP BY<\/code>\u53e5\u306e\u62e1\u5f35\u3067\u3042\u308b<strong><code>GROUPING SETS<\/code><\/strong>, <strong><code>ROLLUP<\/code><\/strong>, <strong><code>CUBE<\/code><\/strong>\u3067\u3059\u3002<\/p>\n<h3><code>GROUPING SETS<\/code>\uff1a\u81ea\u7531\u306a\u7d44\u307f\u5408\u308f\u305b\u3067\u96c6\u8a08<\/h3>\n<p><code>GROUPING SETS<\/code>\u306f\u3001\u96c6\u8a08\u3057\u305f\u3044\u30b0\u30eb\u30fc\u30d7\u306e\u7d44\u307f\u5408\u308f\u305b\u3092\u81ea\u7531\u306b\u3001\u660e\u793a\u7684\u306b\u6307\u5b9a\u3067\u304d\u308b\u6700\u3082\u67d4\u8edf\u306a\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u300c\u5730\u57df\u5225\u5408\u8a08\u300d\u300c\u30ab\u30c6\u30b4\u30ea\u5225\u5408\u8a08\u300d\u300c\u7dcf\u5408\u8a08\u300d\u3092\u4e00\u5ea6\u306b\u53d6\u5f97<\/strong><\/p>\n<pre><code>SELECT region, category, SUM(sales) AS total_sales\nFROM sales_data\nGROUP BY GROUPING SETS (\n    (region),   -- \u5730\u57df\u3054\u3068\u306e\u96c6\u8a08\n    (category), -- \u30ab\u30c6\u30b4\u30ea\u3054\u3068\u306e\u96c6\u8a08\n    ()          -- \u7dcf\u5408\u8a08\uff08\u7a7a\u306e\u30bf\u30d7\u30eb\u3067\u6307\u5b9a\uff09\n);\n<\/code><\/pre>\n<h3><code>ROLLUP<\/code>\uff1a\u968e\u5c64\u7684\u306a\u5c0f\u8a08\u3068\u5408\u8a08\u3092\u4e00\u5ea6\u306b<\/h3>\n<p><code>ROLLUP<\/code>\u306f\u3001\u968e\u5c64\u69cb\u9020\u3092\u6301\u3064\u30c7\u30fc\u30bf\u306e\u96c6\u8a08\u306b\u7279\u5316\u3057\u305f\u30b7\u30e7\u30fc\u30c8\u30ab\u30c3\u30c8\u3067\u3059\u3002\u6307\u5b9a\u3057\u305f\u5217\u306e\u7d44\u307f\u5408\u308f\u305b\u304b\u3089\u3001\u3088\u308a\u4e0a\u4f4d\u306e\u968e\u5c64\u306e\u5c0f\u8a08\u3001\u305d\u3057\u3066\u7dcf\u8a08\u3078\u3068\u6bb5\u968e\u7684\u306b\u96c6\u8a08\uff08\u30ed\u30fc\u30eb\u30a2\u30c3\u30d7\uff09\u3057\u305f\u7d50\u679c\u3092\u751f\u6210\u3057\u307e\u3059\u3002<\/p>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u300c\u90fd\u5e02\u5225\u300d\u300c\u5730\u57df\u3054\u3068\u306e\u5c0f\u8a08\u300d\u300c\u7dcf\u5408\u8a08\u300d\u3092\u4e00\u5ea6\u306b<\/strong><\/p>\n<pre><code>SELECT region, city, SUM(sales) AS total_sales\nFROM sales_data\nGROUP BY ROLLUP(region, city);\n<\/code><\/pre>\n<h3><code>CUBE<\/code>\uff1a\u5168\u30d1\u30bf\u30fc\u30f3\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u96c6\u8a08<\/h3>\n<p><code>CUBE<\/code>\u306f\u3001\u6307\u5b9a\u3057\u305f\u5217\u306e\u8003\u3048\u3089\u308c\u308b<strong>\u3059\u3079\u3066\u306e\u7d44\u307f\u5408\u308f\u305b<\/strong>\u306b\u3064\u3044\u3066\u96c6\u8a08\u3092\u884c\u3044\u307e\u3059\u3002\u591a\u6b21\u5143\u5206\u6790\u3067\u30af\u30ed\u30b9\u96c6\u8a08\u8868\u3092\u4f5c\u6210\u3059\u308b\u3088\u3046\u306a\u5834\u5408\u306b\u4fbf\u5229\u3067\u3059\u3002<\/p>\n<h3><code>GROUPING()<\/code>\u95a2\u6570\uff1a\u96c6\u8a08\u884c\u306e<code>NULL<\/code>\u3092\u5224\u5b9a\u3059\u308b<\/h3>\n<p>\u96c6\u8a08\u306b\u3088\u3063\u3066\u751f\u6210\u3055\u308c\u305f<code>NULL<\/code>\u306a\u306e\u304b\u3001\u5143\u3005\u306e\u30c7\u30fc\u30bf\u304c<code>NULL<\/code>\u306a\u306e\u304b\u3092\u533a\u5225\u3059\u308b\u305f\u3081\u306b<code>GROUPING()<\/code>\u95a2\u6570\u3092\u4f7f\u3044\u307e\u3059\u3002<code>GROUPING(\u5217\u540d)<\/code>\u306f\u3001\u305d\u306e\u884c\u304c\u6307\u5b9a\u3057\u305f\u5217\u3067\u96c6\u8a08\u3055\u308c\u3066\u3044\u308b\u5834\u5408\u306f<strong>1<\/strong>\u3092\u3001\u305d\u3046\u3067\u306a\u3044\u5834\u5408\u306f<strong>0<\/strong>\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre><code>SELECT\n    CASE WHEN GROUPING(region) = 1 THEN '\u7dcf\u5408\u8a08' ELSE region END AS region,\n    CASE WHEN GROUPING(city) = 1 THEN '\u5730\u57df\u5408\u8a08' ELSE city END AS city,\n    SUM(sales) AS total_sales\nFROM\n    sales_data\nGROUP BY ROLLUP(region, city);\n<\/code><\/pre>\n<h2>WHERE\u53e5\u3068HAVING\u53e5\u306e\u9055\u3044\u2502\u96c6\u8a08\u524d\u5f8c\u306e\u30d5\u30a3\u30eb\u30bf\u30ea\u30f3\u30b0\u3092\u5fb9\u5e95\u89e3\u8aac<\/h2>\n<p><code>WHERE<\/code>\u3068<code>HAVING<\/code>\u306f\u3001\u3069\u3061\u3089\u3082\u30c7\u30fc\u30bf\u3092\u300c\u7d5e\u308a\u8fbc\u3080\u300d\u305f\u3081\u306e\u53e5\u3067\u3059\u304c\u3001\u305d\u306e\u5f79\u5272\u3068\u5b9f\u884c\u3055\u308c\u308b\u30bf\u30a4\u30df\u30f3\u30b0\u304c\u6839\u672c\u7684\u306b\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong><code>WHERE<\/code>\u53e5<\/strong>: <code>GROUP BY<\/code>\u3067\u96c6\u8a08\u3055\u308c\u308b<strong>\u524d<\/strong>\u306b\u3001\u500b\u3005\u306e<strong>\u884c<\/strong>\u3092\u7d5e\u308a\u8fbc\u3080\u3002<\/li>\n<li><strong><code>HAVING<\/code>\u53e5<\/strong>: <code>GROUP BY<\/code>\u3067\u96c6\u8a08\u3055\u308c\u305f<strong>\u5f8c<\/strong>\u306b\u3001\u305d\u306e\u7d50\u679c\u306e<strong>\u30b0\u30eb\u30fc\u30d7<\/strong>\u3092\u7d5e\u308a\u8fbc\u3080\u3002<\/li>\n<\/ul>\n<h3>&#x1f3eb; \u5b66\u6821\u306e\u30a4\u30d9\u30f3\u30c8\u3067\u4f8b\u3048\u308b<code>WHERE<\/code>\u3068<code>HAVING`<\/code><\/h3>\n<p><strong><code>WHERE<\/code>\u53e5\u306e\u5f79\u5272\u306f\u300c\u53c2\u52a0\u8cc7\u683c\u300d<\/strong>\u3067\u3059\u3002\u5927\u4f1a\u304c\u59cb\u307e\u308b<strong>\u524d<\/strong>\u306b\u3001\u751f\u5f92\u4e00\u4eba\u3072\u3068\u308a\uff08\uff1d<strong>\u884c<\/strong>\uff09\u3092\u30c1\u30a7\u30c3\u30af\u3057\u3066\u53c2\u52a0\u8005\u3092\u7d5e\u308a\u8fbc\u307f\u307e\u3059\u3002<\/p>\n<p><strong><code>HAVING`\u53e5\u306e\u5f79\u5272\u306f\u300c\u8868\u5f70\u6761\u4ef6\u300d<\/code><\/strong>\u3067\u3059\u3002\u5927\u4f1a\u304c\u7d42\u308f\u3063\u305f<strong>\u5f8c<\/strong>\u306e\u96c6\u8a08\u3055\u308c\u305f\u7d50\u679c\uff08\uff1d<strong>\u30b0\u30eb\u30fc\u30d7<\/strong>\uff09\u3092\u898b\u3066\u3001\u6761\u4ef6\u3092\u6e80\u305f\u3059\u30af\u30e9\u30b9\u3092\u8868\u5f70\u3057\u307e\u3059\u3002<\/p>\n<h3><code>WHERE`\u53e5\uff1a`GROUP BY`\u524d\u306e\u300c\u884c\u300d\u306b\u5bfe\u3059\u308b\u6761\u4ef6<\/code><\/h3>\n<p><code>WHERE`\u53e5\u306f\u3001\u96c6\u8a08\u95a2\u6570 (`SUM()`, `COUNT()`\u306a\u3069) \u304c\u8a08\u7b97\u3055\u308c\u308b\u3088\u308a\u3082<strong>\u524d\u306b<\/strong>\u51e6\u7406\u3055\u308c\u308b\u305f\u3081\u3001\u6761\u4ef6\u5f0f\u306e\u4e2d\u3067<strong>\u96c6\u8a08\u95a2\u6570\u3092\u4f7f\u3046\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093<\/strong>\u3002<\/code><\/p>\n<pre><code>SELECT product_category, SUM(sales) AS total_sales\nFROM sales_data\nWHERE region = '\u95a2\u6771' -- \u307e\u305a\u500b\u3005\u306e\u884c\u3092\u300c\u95a2\u6771\u300d\u306b\u7d5e\u308a\u8fbc\u3080\nGROUP BY product_category;\n<\/code><\/pre>\n<h3><code>HAVING`\u53e5\uff1a`GROUP BY`\u5f8c\u306e\u300c\u30b0\u30eb\u30fc\u30d7\u300d\u306b\u5bfe\u3059\u308b\u6761\u4ef6<\/code><\/h3>\n<p><code>HAVING`\u53e5\u306f\u3001<code>GROUP BY`\u306b\u3088\u3063\u3066\u96c6\u8a08\u30fb\u30b0\u30eb\u30fc\u30d7\u5316\u3055\u308c\u305f\u7d50\u679c\u30bb\u30c3\u30c8\u306b\u5bfe\u3057\u3066\u7d5e\u308a\u8fbc\u307f\u3092\u884c\u3046\u305f\u3081\u3001<strong>\u96c6\u8a08\u95a2\u6570\u3092\u4f7f\u3046\u306e\u304c\u4e00\u822c\u7684<\/strong>\u3067\u3059\u3002<\/code><\/code><\/p>\n<pre><code>SELECT product_category, SUM(sales) AS total_sales\nFROM sales_data\nWHERE region = '\u95a2\u6771'\nGROUP BY product_category\nHAVING SUM(sales) &gt; 1000000; -- \u96c6\u8a08\u5f8c\u306e\u7d50\u679c(\u30b0\u30eb\u30fc\u30d7)\u3092\u7d5e\u308a\u8fbc\u3080\n<\/code><\/pre>\n<h3>\u8a66\u9a13\u3067\u72d9\u308f\u308c\u3084\u3059\u3044\u8aa4\u89e3\u30d1\u30bf\u30fc\u30f3\u3068\u307e\u3068\u3081<\/h3>\n<p>SQL\u306e\u5185\u90e8\u7684\u306a\u5b9f\u884c\u9806\u5e8f\u306f <strong>FROM \u2192 WHERE \u2192 GROUP BY \u2192 HAVING \u2192 SELECT \u2192 ORDER BY<\/strong> \u3067\u3059\u3002<\/p>\n<table border=\"1\">\n<thead>\n<tr>\n<th><\/th>\n<th><code>WHERE<\/code>\u53e5<\/th>\n<th><code>HAVING<\/code>\u53e5<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u51e6\u7406\u30bf\u30a4\u30df\u30f3\u30b0<\/strong><\/td>\n<td><code>GROUP BY<\/code>\u306e\u524d<\/td>\n<td><code>GROUP BY<\/code>\u306e\u5f8c<\/td>\n<\/tr>\n<tr>\n<td><strong>\u5bfe\u8c61<\/strong><\/td>\n<td>\u500b\u3005\u306e<strong>\u884c<\/strong> (Rows)<\/td>\n<td>\u96c6\u7d04\u3055\u308c\u305f<strong>\u30b0\u30eb\u30fc\u30d7<\/strong> (Groups)<\/td>\n<\/tr>\n<tr>\n<td><strong>\u96c6\u8a08\u95a2\u6570\u306e\u4f7f\u7528<\/strong><\/td>\n<td>&#x274c; <strong>\u4e0d\u53ef<\/strong><\/td>\n<td>&#x2705; <strong>\u53ef\u80fd<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong><code>GROUP BY<\/code>\u306e\u8981\u5426<\/strong><\/td>\n<td>\u4e0d\u8981<\/td>\n<td>\u539f\u5247\u3068\u3057\u3066\u5fc5\u8981<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>PIVOT\/UNPIVOT\u306b\u3088\u308b\u884c\u5217\u5909\u63db\u2502SQL\u306eCASE\u5f0f\u3092\u4f7f\u3063\u305f\u66f8\u304d\u65b9\u3082\u89e3\u8aac<\/h2>\n<p>\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u3067\u306f\u30c7\u30fc\u30bf\u3092\u300c\u7e26\u6301\u3061\u300d\uff081\u884c1\u30d5\u30a1\u30af\u30c8\uff09\u3067\u4fdd\u6301\u3059\u308b\u306e\u304c\u4e00\u822c\u7684\u3067\u3059\u304c\u3001\u30ec\u30dd\u30fc\u30c8\u306a\u3069\u3067\u306f\u300c\u6a2a\u6301\u3061\u300d\uff08\u30af\u30ed\u30b9\u96c6\u8a08\u8868\uff09\u306e\u65b9\u304c\u898b\u3084\u3059\u3044\u5834\u5408\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u306e\u300c\u7e26\u6301\u3061\u300d\u3068\u300c\u6a2a\u6301\u3061\u300d\u3092\u76f8\u4e92\u306b\u5909\u63db\u3059\u308b\u64cd\u4f5c\u304c<strong>PIVOT<\/strong>\u3068<strong>UNPIVOT<\/strong>\u3067\u3059\u3002<\/p>\n<h3><code>PIVOT<\/code>\uff1a\u7e26\u6301\u3061\u30c7\u30fc\u30bf\u3092\u6a2a\u6301\u3061\u30c7\u30fc\u30bf\u306b\u5909\u63db<\/h3>\n<p><code>PIVOT<\/code>\u306f\u3001\u7279\u5b9a\u306e\u5217\u306e\u5024\u3092\u65b0\u3057\u3044\u5217\u540d\u3068\u3057\u3066\u5c55\u958b\u3057\u3001\u6307\u5b9a\u3057\u305f\u96c6\u8a08\u3092\u884c\u3046\u3053\u3068\u3067\u3001\u30c7\u30fc\u30bf\u3092\u7e26\u304b\u3089\u6a2a\u3078\u56de\u8ee2\u3055\u305b\u307e\u3059\u3002<\/p>\n<h4>\u66f8\u304d\u65b91\uff1a<code>PIVOT<\/code>\u6f14\u7b97\u5b50\u3092\u4f7f\u3046\uff08SQL Server \/ Oracle\u306a\u3069\uff09<\/h4>\n<pre><code>SELECT year, [1] AS Jan, [2] AS Feb, [3] AS Mar\nFROM\n    monthly_sales\nPIVOT (\n    SUM(sales)          -- \u4f55\u3092\u96c6\u8a08\u3059\u308b\u304b\n    FOR month           -- \u3069\u306e\u5217\u3092\u65b0\u3057\u3044\u5217\u540d\u306b\u3059\u308b\u304b\n    IN ([1], [2], [3])  -- \u65b0\u3057\u3044\u5217\u540d\u306b\u306a\u308b\u5024\u306e\u30ea\u30b9\u30c8\n) AS PivotTable;\n<\/code><\/pre>\n<h4>\u66f8\u304d\u65b92\uff1a<code>CASE<\/code>\u5f0f\u3092\u4f7f\u3046\uff08\u6a19\u6e96SQL\uff09<\/h4>\n<p><code>PIVOT<\/code>\u3092\u30b5\u30dd\u30fc\u30c8\u3057\u306a\u3044DBMS\u3067\u306f\u3001<code>GROUP BY<\/code>\u3068<code>CASE<\/code>\u5f0f\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u3067\u540c\u3058\u7d50\u679c\u3092\u5f97\u3089\u308c\u307e\u3059\u3002<\/p>\n<pre><code>SELECT\n    year,\n    SUM(CASE WHEN month = 1 THEN sales ELSE 0 END) AS Jan,\n    SUM(CASE WHEN month = 2 THEN sales ELSE 0 END) AS Feb,\n    SUM(CASE WHEN month = 3 THEN sales ELSE 0 END) AS Mar\nFROM\n    monthly_sales\nGROUP BY\n    year;\n<\/code><\/pre>\n<h3><code>UNPIVOT<\/code>\uff1a\u6a2a\u6301\u3061\u30c7\u30fc\u30bf\u3092\u7e26\u6301\u3061\u30c7\u30fc\u30bf\u306b\u5909\u63db<\/h3>\n<p><code>UNPIVOT<\/code>\u306f<code>PIVOT<\/code>\u306e\u9006\u3067\u3001\u8907\u6570\u306e\u5217\u3092\u884c\u306b\u5909\u63db\u3057\u307e\u3059\u3002<\/p>\n<h4>\u66f8\u304d\u65b91\uff1a<code>UNPIVOT`\u6f14\u7b97\u5b50\u3092\u4f7f\u3046\uff08SQL Server \/ Oracle\u306a\u3069\uff09<\/code><\/h4>\n<pre><code>SELECT student_name, subject, score\nFROM\n    grades_wide\nUNPIVOT (\n    score                -- \u5024\u304c\u5165\u308b\u5217\n    FOR subject          -- \u30ab\u30c6\u30b4\u30ea\u304c\u5165\u308b\u5217\n    IN (kokugo, sugaku)  -- \u7e26\u306b\u5c55\u958b\u3057\u305f\u3044\u5143\u306e\u5217\u30ea\u30b9\u30c8\n) AS UnpivotTable;\n<\/code><\/pre>\n<h4>\u66f8\u304d\u65b92\uff1a<code>UNION ALL`\u3092\u4f7f\u3046\uff08\u6a19\u6e96SQL\uff09<\/code><\/h4>\n<pre><code>SELECT student_name, '\u56fd\u8a9e' AS subject, kokugo AS score FROM grades_wide\nUNION ALL\nSELECT student_name, '\u6570\u5b66' AS subject, sugaku AS score FROM grades_wide;\n<\/code><\/pre>\n<h2>\u96c6\u5408\u6f14\u7b97\u5b50UNION, INTERSECT, EXCEPT\u306e\u4f7f\u3044\u5206\u3051\u2502JOIN\u3068\u306e\u9055\u3044\u3082\u89e3\u8aac<\/h2>\n<p><code>JOIN<\/code>\u304c\u30c6\u30fc\u30d6\u30eb\u3092<strong>\u6a2a\u306b\uff08\u5217\u3092\uff09<\/strong>\u7d50\u5408\u3059\u308b\u306e\u306b\u5bfe\u3057\u3001\u96c6\u5408\u6f14\u7b97\u5b50\u306f\u8907\u6570\u306e<code>SELECT<\/code>\u6587\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u3092<strong>\u7e26\u306b\uff08\u884c\u3092\uff09<\/strong>\u7d50\u5408\u3057\u305f\u308a\u3001\u6bd4\u8f03\u3057\u305f\u308a\u3057\u307e\u3059\u3002\u64cd\u4f5c\u306e\u524d\u63d0\u3068\u3057\u3066\u3001\u5404<code>SELECT<\/code>\u6587\u306e<strong>\u5217\u306e\u6570\u3068\u3001\u5bfe\u5fdc\u3059\u308b\u5217\u306e\u30c7\u30fc\u30bf\u578b\u304c\u4e00\u81f4\u3057\u3066\u3044\u308b<\/strong>\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h3><code>UNION<\/code> \/ <code>UNION ALL<\/code>\uff1a\u548c\u96c6\u5408\uff08\u7d50\u679c\u30bb\u30c3\u30c8\u306e\u8db3\u3057\u7b97\uff09<\/h3>\n<ul>\n<li><strong><code>UNION<\/code><\/strong>: \u91cd\u8907\u3059\u308b\u884c\u3092<strong>\u6392\u9664\u3057\u3066<\/strong>\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002<\/li>\n<li><strong><code>UNION ALL<\/code><\/strong>: \u91cd\u8907\u3059\u308b\u884c\u3092<strong>\u305d\u306e\u307e\u307e\u6b8b\u3057\u3066<\/strong>\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002\u3053\u3061\u3089\u306e\u65b9\u304c\u9ad8\u901f\u3067\u3059\u3002<\/li>\n<\/ul>\n<pre><code>SELECT product_id, amount FROM sales_2024\nUNION ALL\nSELECT product_id, amount FROM sales_2023;\n<\/code><\/pre>\n<h3><code>INTERSECT<\/code>\uff1a\u7a4d\u96c6\u5408\uff08\u5171\u901a\u90e8\u5206\u306e\u62bd\u51fa\uff09<\/h3>\n<p>2\u3064\u306e<code>SELECT<\/code>\u6587\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u306b<strong>\u5171\u901a\u3057\u3066\u5b58\u5728\u3059\u308b\u884c\u3060\u3051<\/strong>\u3092\u8fd4\u3057\u307e\u3059\u3002<\/p>\n<pre><code>SELECT user_id FROM premium_users\nINTERSECT\nSELECT user_id FROM newsletter_subscribers;\n<\/code><\/pre>\n<h3><code>EXCEPT` \/ `MINUS`\uff1a\u5dee\u96c6\u5408\uff08\u7247\u65b9\u3060\u3051\u306b\u5b58\u5728\u3059\u308b\u90e8\u5206\u306e\u62bd\u51fa\uff09<\/code><\/h3>\n<p>\u6700\u521d\u306e<code>SELECT`\u6587\u306e\u7d50\u679c\u304b\u3089\u30012\u756a\u76ee\u306e<code>SELECT`\u6587\u306e\u7d50\u679c\u306b\u542b\u307e\u308c\u308b\u884c\u3092<strong>\u53d6\u308a\u9664\u3044\u305f<\/strong>\u7d50\u679c\u3092\u8fd4\u3057\u307e\u3059\u3002Oracle\u3067\u306f`MINUS`\u304c\u4f7f\u308f\u308c\u307e\u3059\u3002<\/code><\/code><\/p>\n<pre><code>SELECT employee_id FROM employees\nEXCEPT\nSELECT manager_id FROM departments;\n<\/code><\/pre>\n<h3><code>JOIN`\u3068\u306e\u6c7a\u5b9a\u7684\u306a\u9055\u3044<\/code><\/h3>\n<table border=\"1\">\n<thead>\n<tr>\n<th><\/th>\n<th><strong>\u96c6\u5408\u6f14\u7b97\u5b50 (Set Operators)<\/strong><\/th>\n<th><strong><code>JOIN<\/code><\/strong><\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><strong>\u7d50\u5408\u65b9\u5411<\/strong><\/td>\n<td><strong>\u7e26 (Vertical)<\/strong> &#8211; \u884c\u3092\u8ffd\u52a0\u3059\u308b<\/td>\n<td><strong>\u6a2a (Horizontal)<\/strong> &#8211; \u5217\u3092\u8ffd\u52a0\u3059\u308b<\/td>\n<\/tr>\n<tr>\n<td><strong>\u5bfe\u8c61<\/strong><\/td>\n<td>2\u3064\u4ee5\u4e0a\u306e<code>SELECT<\/code>\u6587\u306e<strong>\u7d50\u679c\u30bb\u30c3\u30c8<\/strong><\/td>\n<td>2\u3064\u4ee5\u4e0a\u306e<strong>\u30c6\u30fc\u30d6\u30eb<\/strong><\/td>\n<\/tr>\n<tr>\n<td><strong>\u524d\u63d0\u6761\u4ef6<\/strong><\/td>\n<td>\u5217\u306e\u6570\u3068\u30c7\u30fc\u30bf\u578b\u304c\u4e00\u81f4\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b<\/td>\n<td>\u95a2\u9023\u3059\u308b\u30ad\u30fc\u5217\u3067\u7d50\u5408\u6761\u4ef6\u3092\u5b9a\u7fa9\u3059\u308b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>SQL\u3067\u306eJSON\u51e6\u7406\u3068\u30c6\u30fc\u30d6\u30eb\u5316\u2502JSON_TABLE\/OPENJSON\u3067\u975e\u69cb\u9020\u5316\u30c7\u30fc\u30bf\u3092\u6271\u3046<\/h2>\n<p>\u6700\u8fd1\u306e\u4e3b\u8981\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306f\u3001SQL\u3067\u76f4\u63a5JSON\u3092\u64cd\u4f5c\u3059\u308b\u305f\u3081\u306e\u5f37\u529b\u306a\u95a2\u6570\u3092\u5099\u3048\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001<strong>SQL\u306e\u30d1\u30ef\u30fc\uff08JOIN\u3084\u96c6\u8a08\u306a\u3069\uff09\u3092JSON\u30c7\u30fc\u30bf\u306b\u5bfe\u3057\u3066\u3082\u6d3b\u7528<\/strong>\u3067\u304d\u307e\u3059\u3002<\/p>\n<h3>JSON\u30c7\u30fc\u30bf\u304b\u3089\u306e\u5024\u306e\u62bd\u51fa<\/h3>\n<p>\u307e\u305a\u306f\u57fa\u672c\u3068\u3057\u3066\u3001JSON\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304b\u3089\u7279\u5b9a\u306e\u30ad\u30fc\u306e\u5024\u3092\u53d6\u308a\u51fa\u3059\u65b9\u6cd5\u3067\u3059\u3002<code>$.\u30ad\u30fc\u540d<\/code>\u306e\u3088\u3046\u306a\u300cJSON\u30d1\u30b9\u5f0f\u300d\u3092\u4f7f\u3063\u3066\u3001\u5024\u306e\u5834\u6240\u3092\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u30e6\u30fc\u30b6\u30fc\u30d7\u30ed\u30d5\u30a1\u30a4\u30eb\uff08JSON\uff09\u304b\u3089\u540d\u524d\u3068\u90fd\u5e02\u3092\u62bd\u51fa<\/strong><\/p>\n<p><strong>MySQL \/ MariaDB \u306e\u5834\u5408<\/strong><\/p>\n<pre><code>SELECT\n    profile -&gt;&gt; '$.name' AS name, -- `-&gt;&gt;` \u306f\u30c6\u30ad\u30b9\u30c8\u3068\u3057\u3066\u62bd\u51fa\n    profile -&gt;&gt; '$.address.city' AS city\nFROM users;\n<\/code><\/pre>\n<p><strong>SQL Server \u306e\u5834\u5408<\/strong><\/p>\n<pre><code>SELECT\n    JSON_VALUE(profile, '$.name') AS name,\n    JSON_VALUE(profile, '$.address.city') AS city\nFROM users;\n<\/code><\/pre>\n<h3>JSON\u914d\u5217\u306e\u30c6\u30fc\u30d6\u30eb\u5316<\/h3>\n<p>JSON\u95a2\u6570\u306e\u771f\u4fa1\u304c\u767a\u63ee\u3055\u308c\u308b\u306e\u304c\u3001JSON\u914d\u5217\u3092\u884c\u306b\u5c55\u958b\u3059\u308b\u300c\u30c6\u30fc\u30d6\u30eb\u5316\u300d\u3067\u3059\u3002<\/p>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u6ce8\u6587JSON\u30c7\u30fc\u30bf\u304b\u3089\u6ce8\u6587\u660e\u7d30\u3092\u30c6\u30fc\u30d6\u30eb\u5f62\u5f0f\u3067\u53d6\u308a\u51fa\u3059<\/strong><\/p>\n<h4><code>JSON_TABLE<\/code> (Oracle \/ MySQL 8.0\u4ee5\u964d)<\/h4>\n<pre><code>SELECT\n    o.order_id,\n    i.*\nFROM\n    orders o,\n    JSON_TABLE(\n        o.order_data,\n        '$.items[*]' -- [*]\u3067\u3059\u3079\u3066\u306e\u914d\u5217\u8981\u7d20\u3092\u5c55\u958b\n        COLUMNS (\n            product_id VARCHAR(10) PATH '$.prod_id',\n            quantity INT PATH '$.qty'\n        )\n    ) AS i;\n<\/code><\/pre>\n<h4><code>OPENJSON<\/code> (SQL Server)<\/h4>\n<pre><code>SELECT\n    o.order_id,\n    i.*\nFROM\n    orders o\nCROSS APPLY OPENJSON(o.order_data, '$.items')\n    WITH (\n        product_id VARCHAR(10) '$.prod_id',\n        quantity INT '$.qty'\n    ) AS i;\n<\/code><\/pre>\n<pre><code>\n<\/code><\/pre>\n<h2>OFFSET\/FETCH\/LIMIT\u306b\u3088\u308b\u30da\u30fc\u30b8\u30f3\u30b0\u51e6\u7406\u3068\u5b89\u5b9a\u30bd\u30fc\u30c8\u306e\u91cd\u8981\u6027<\/h2>\n<p>Web\u30b5\u30a4\u30c8\u306e\u691c\u7d22\u7d50\u679c\u306e\u3088\u3046\u306b\u3001\u7d50\u679c\u3092\u5206\u5272\u3057\u3066\u8868\u793a\u3059\u308b\u6a5f\u80fd\u3092<strong>\u30da\u30fc\u30b8\u30f3\u30b0<\/strong>\u3068\u547c\u3073\u307e\u3059\u3002\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304b\u3089\u6307\u5b9a\u3057\u305f\u7bc4\u56f2\u306e\u30c7\u30fc\u30bf\u3060\u3051\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b<code>OFFSET<\/code>, <code>FETCH<\/code>, <code>LIMIT<\/code>\u53e5\u304c\u4f7f\u308f\u308c\u307e\u3059\u3002<\/p>\n<h3>\u30da\u30fc\u30b8\u30f3\u30b0\u306e\u57fa\u672c\uff1a<code>OFFSET<\/code> \/ <code>FETCH<\/code> \/ <code>LIMIT<\/code><\/h3>\n<ul>\n<li><strong><code>OFFSET<\/code><\/strong>: \u5148\u982d\u304b\u3089\u6307\u5b9a\u3057\u305f\u884c\u6570\u3092<strong>\u30b9\u30ad\u30c3\u30d7<\/strong>\u3057\u307e\u3059\u3002<\/li>\n<li><strong><code>FETCH<\/code> \/ <code>LIMIT<\/code><\/strong>: \u30b9\u30ad\u30c3\u30d7\u3057\u305f\u5f8c\u306e\u4f4d\u7f6e\u304b\u3089\u3001\u6307\u5b9a\u3057\u305f\u884c\u6570\u3060\u3051\u3092<strong>\u53d6\u5f97<\/strong>\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<p><strong>&#x1f4d6; \u5177\u4f53\u4f8b\uff1a\u5546\u54c1\u4e00\u89a7\u30921\u30da\u30fc\u30b810\u4ef6\u3068\u3057\u3066\u30013\u30da\u30fc\u30b8\u76ee\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/strong><\/p>\n<p><strong>MySQL \/ PostgreSQL \u306e\u5834\u5408 (<code>LIMIT ... OFFSET ...<\/code>)<\/strong><\/p>\n<pre><code>SELECT product_id, product_name, price\nFROM products\nORDER BY created_at DESC\nLIMIT 10 OFFSET 20;\n<\/code><\/pre>\n<p><strong>SQL Standard \/ Oracle \/ SQL Server \u306e\u5834\u5408 (<code>OFFSET ... FETCH ...<\/code>)<\/strong><\/p>\n<pre><code>SELECT product_id, product_name, price\nFROM products\nORDER BY created_at DESC\nOFFSET 20 ROWS\nFETCH NEXT 10 ROWS ONLY;\n<\/code><\/pre>\n<h3>\u5b89\u5b9a\u30bd\u30fc\u30c8\u306e\u91cd\u8981\u6027\u3068\u9375\u4ed8\u304d\u30da\u30fc\u30b8\u30f3\u30b0<\/h3>\n<p><code>OFFSET<\/code>\u3092\u4f7f\u3063\u305f\u30da\u30fc\u30b8\u30f3\u30b0\u306b\u306f\u3001<code>ORDER BY<\/code>\u306e\u30ad\u30fc\u306b\u91cd\u8907\u304c\u3042\u3063\u305f\u5834\u5408\u306e\u9806\u5e8f\u304c\u4fdd\u8a3c\u3055\u308c\u306a\u3044\u3068\u3044\u3046\u843d\u3068\u3057\u7a74\u304c\u3042\u308a\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u30da\u30fc\u30b8\u9593\u3067\u8868\u793a\u304c\u91cd\u8907\u30fb\u6b20\u843d\u3059\u308b\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<h4>\u89e3\u6c7a\u7b561\uff1a\u5b89\u5b9a\u30bd\u30fc\u30c8 (Stable Sort)<\/h4>\n<p>\u89e3\u6c7a\u7b56\u306f\u3001<code>ORDER BY<\/code>\u53e5\u306e\u6700\u5f8c\u306b\u4e3b\u30ad\u30fc\u306a\u3069\u306e<strong>\u30e6\u30cb\u30fc\u30af\u306a\u5217\u3092\u5fc5\u305a\u8ffd\u52a0<\/strong>\u3057\u3001\u4e26\u3073\u9806\u304c\u4e00\u610f\u306b\u6c7a\u307e\u308b\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u3059\u3002<\/p>\n<pre><code>ORDER BY price DESC, product_id ASC;\n<\/code><\/pre>\n<h4>\u89e3\u6c7a\u7b562\uff1a\u9375\u4ed8\u304d\u30da\u30fc\u30b8\u30f3\u30b0 (Keyset Paging)<\/h4>\n<p><code>OFFSET<\/code>\u306f\u4ef6\u6570\u304c\u5897\u3048\u308b\u3068\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u52a3\u5316\u3057\u307e\u3059\u3002\u305d\u306e\u554f\u984c\u3092\u89e3\u6c7a\u3059\u308b\u306e\u304c<strong>\u9375\u4ed8\u304d\u30da\u30fc\u30b8\u30f3\u30b0<\/strong>\u3067\u3059\u3002\u300cN\u4ef6\u30b9\u30ad\u30c3\u30d7\u3059\u308b\u300d\u306e\u3067\u306f\u306a\u304f\u3001\u300c<strong>\u6700\u5f8c\u306b\u898b\u305f\u884c\u306e\u6b21\u306e\u884c\u304b\u3089<\/strong>N\u4ef6\u53d6\u5f97\u3059\u308b\u300d\u3068\u3044\u3046\u8003\u3048\u65b9\u3067\u3001\u975e\u5e38\u306b\u9ad8\u901f\u306b\u52d5\u4f5c\u3057\u307e\u3059\u3002<\/p>\n<pre><code>SELECT product_id, product_name, price\nFROM products\nWHERE\n    (price &lt; 5000) OR (price = 5000 AND product_id &gt; 123)\nORDER BY\n    price DESC, product_id ASC\nLIMIT 10;\n<\/code><\/pre>\n<h2>\u5b9f\u52d9\u3067\u5f79\u7acb\u3064SQL\u5fdc\u7528Tips\u2502\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30fb\u30ed\u30c3\u30af\u7af6\u5408\u30fb\u30c7\u30fc\u30bf\u6574\u5408\u6027\u306e\u8003\u616e\u70b9<\/h2>\n<p>\u5b9f\u52d9\u306e\u73fe\u5834\u3067\u306f\u3001\u5358\u306b\u300c\u6b63\u3057\u3044\u7d50\u679c\u304c\u8fd4\u3063\u3066\u304f\u308b\u300d\u3060\u3051\u3067\u306f\u4e0d\u5341\u5206\u3067\u3059\u3002\u300c<strong>\u3044\u304b\u306b\u901f\u304f\u3001\u5b89\u5168\u306b\u3001\u305d\u3057\u3066\u30c7\u30fc\u30bf\u306e\u4e00\u8cab\u6027\u3092\u4fdd\u3061\u306a\u304c\u3089<\/strong>\u300d\u5b9f\u884c\u3067\u304d\u308b\u304b\u304c\u554f\u308f\u308c\u307e\u3059\u3002<\/p>\n<h3>\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3068\u5b9f\u884c\u8a08\u753b<\/h3>\n<p>\u540c\u3058\u7d50\u679c\u3092\u8fd4\u3059SQL\u3067\u3082\u3001\u66f8\u304d\u65b9\u306b\u3088\u3063\u3066\u5b9f\u884c\u901f\u5ea6\u304c\u5927\u304d\u304f\u5909\u308f\u308a\u307e\u3059\u3002\u305d\u306e\u9375\u3092\u63e1\u308b\u306e\u304c<strong>\u5b9f\u884c\u8a08\u753b<\/strong>\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong><code>EXPLAIN<\/code><\/strong>: \u3053\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u304c\u7acb\u3066\u305f\u5b9f\u884c\u8a08\u753b\u3092\u78ba\u8a8d\u3057\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u30c1\u30e5\u30fc\u30cb\u30f3\u30b0\u306e\u7b2c\u4e00\u6b69\u3068\u3057\u307e\u3059\u3002<\/li>\n<li><strong>\u30d5\u30eb\u30c6\u30fc\u30d6\u30eb\u30b9\u30ad\u30e3\u30f3\u3092\u907f\u3051\u308b<\/strong>: \u5b9f\u884c\u8a08\u753b\u3067\u300cFull Table Scan\u300d\u3068\u8868\u793a\u3055\u308c\u305f\u5834\u5408\u3001\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u304c\u4f7f\u3048\u3066\u3044\u306a\u3044\u30b5\u30a4\u30f3\u3067\u3059\u3002<code>WHERE<\/code>\u53e5\u306e\u6761\u4ef6\u304c\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u52b9\u304f\u66f8\u304d\u65b9\uff08SARGable\uff09\u306b\u306a\u3063\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3057\u307e\u3057\u3087\u3046\u3002<\/li>\n<li><strong>\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306e\u8cbc\u308a\u3059\u304e\u306b\u6ce8\u610f<\/strong>: \u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306f<code>SELECT<\/code>\u3092\u9ad8\u901f\u5316\u3057\u307e\u3059\u304c\u3001<code>INSERT<\/code>\u3084<code>UPDATE<\/code>\u306e\u6027\u80fd\u3092\u4f4e\u4e0b\u3055\u305b\u308b\u539f\u56e0\u306b\u3082\u306a\u308a\u307e\u3059\u3002<\/li>\n<\/ul>\n<h3>\u30ed\u30c3\u30af\u7af6\u5408\u3092\u907f\u3051\u308b\u5de5\u592b<\/h3>\n<p>\u8907\u6570\u4eba\u304c\u540c\u6642\u306b\u30c7\u30fc\u30bf\u3092\u66f4\u65b0\u3059\u308b\u30b7\u30b9\u30c6\u30e0\u3067\u306f\u3001\u30c7\u30fc\u30bf\u306e\u6574\u5408\u6027\u3092\u4fdd\u3064\u305f\u3081\u306b<strong>\u30ed\u30c3\u30af<\/strong>\u304c\u50cd\u304d\u307e\u3059\u3002\u3053\u306e\u30ed\u30c3\u30af\u306e\u5f85\u3061\u6642\u9593\u304c<strong>\u30ed\u30c3\u30af\u7af6\u5408<\/strong>\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong>\u30c8\u30e9\u30f3\u30b6\u30af\u30b7\u30e7\u30f3\u306f\u77ed\u304f<\/strong>: <code>BEGIN<\/code>\u304b\u3089<code>COMMIT<\/code>\u307e\u3067\u306e\u9593\u306f\u3001\u672c\u5f53\u306b\u5fc5\u8981\u306a\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u64cd\u4f5c\u3060\u3051\u306b\u7d5e\u308a\u307e\u3057\u3087\u3046\u3002<\/li>\n<li><strong>\u30ea\u30bd\u30fc\u30b9\u3078\u306e\u30a2\u30af\u30bb\u30b9\u9806\u3092\u7d71\u4e00\u3059\u308b<\/strong>: \u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u5168\u4f53\u3067\u30c6\u30fc\u30d6\u30eb\u3084\u884c\u3092\u30ed\u30c3\u30af\u3059\u308b\u9806\u756a\u3092\u7d71\u4e00\u3059\u308b\u3068\u3001<strong>\u30c7\u30c3\u30c9\u30ed\u30c3\u30af<\/strong>\u306e\u767a\u751f\u3092\u6e1b\u3089\u305b\u307e\u3059\u3002<\/li>\n<\/ul>\n<h3>\u30c7\u30fc\u30bf\u6574\u5408\u6027\u3092\u5b88\u308b\u5236\u7d04<\/h3>\n<p>\u30c7\u30fc\u30bf\u306e\u54c1\u8cea\u3068\u4e00\u8cab\u6027\u3092\u4fdd\u8a3c\u3059\u308b\u305f\u3081\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u81ea\u8eab\u306e<strong>\u5236\u7d04\uff08Constraints\uff09<\/strong>\u6a5f\u80fd\u3092\u6d3b\u7528\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong><code>PRIMARY KEY<\/code> \/ <code>UNIQUE`<\/code><\/strong>: \u884c\u306e\u91cd\u8907\u3092\u9632\u304e\u307e\u3059\u3002<\/li>\n<li><strong><code>NOT NULL`<\/code><\/strong>: \u5fc5\u9808\u9805\u76ee\u3092\u4fdd\u8a3c\u3057\u307e\u3059\u3002<\/li>\n<li><strong><code>CHECK`<\/code><\/strong>: \u5217\u304c\u6e80\u305f\u3059\u3079\u304d\u6761\u4ef6\u3092\u5b9a\u7fa9\u3057\u307e\u3059 (\u4f8b: <code>CHECK (price &gt;= 0)<\/code>)\u3002<\/li>\n<li><strong><code>FOREIGN KEY`\uff08\u5916\u90e8\u30ad\u30fc\uff09<\/code><\/strong>: \u30c6\u30fc\u30d6\u30eb\u9593\u306e\u95a2\u9023\u6027\u3092\u4fdd\u8a3c\u3057\u3001\u300c\u5b64\u5150\u30ec\u30b3\u30fc\u30c9\u300d\u306e\u767a\u751f\u3092\u9632\u3050\u6700\u3082\u91cd\u8981\u306a\u5236\u7d04\u3067\u3059\u3002<\/li>\n<\/ul>\n<h2>\u5fdc\u7528\u60c5\u5831\u30fbDB\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u983b\u51fa\u306e\u5fdc\u7528SQL\u53e5\u2502\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\u304b\u3089CTE\u307e\u3067\u5fb9\u5e95\u89e3\u8aac<\/h2>\n<p>\u57fa\u672c\u60c5\u5831\u6280\u8853\u8005\u8a66\u9a13\u3067\u5b66\u3076<code>SELECT<\/code>, <code>WHERE<\/code>, <code>GROUP BY<\/code>\u3068\u3044\u3063\u305f\u57fa\u672c\u7684\u306aSQL\u53e5\u3060\u3051\u3067\u306f\u3001\u5fdc\u7528\u60c5\u5831\u3084\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da\u30b7\u30e3\u30ea\u30b9\u30c8\u306e\u5348\u5f8c\u554f\u984c\u3001\u7279\u306b\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u5206\u6790\u3084\u96c6\u8a08\u304c\u6c42\u3081\u3089\u308c\u308b\u8a2d\u554f\u306b\u306f\u5bfe\u5fdc\u3057\u304d\u308c\u307e\u305b\u3093\u3002\u3053\u3053\u3067\u306f\u3001\u5b9f\u52d9\u3067\u3082\u983b\u7e41\u306b\u5229\u7528\u3055\u308c\u3001\u8a66\u9a13\u3067\u3082\u5408\u5426\u3092\u5206\u3051\u308b\u30dd\u30a4\u30f3\u30c8\u3068\u306a\u308b\u5fdc\u7528\u7684\u306aSQL\u53e5\u3092\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\n<h3>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570\uff1aRANK\/ROW_NUMBER\/SUM\/AVG\u306b\u3088\u308b\u9ad8\u5ea6\u306a\u96c6\u8a08<\/h3>\n<p><strong>\u30a6\u30a3\u30f3\u30c9\u30a6\u95a2\u6570<\/strong>\u306f\u3001<code>GROUP BY<\/code>\u306e\u3088\u3046\u306b\u884c\u3092\u96c6\u7d04\u305b\u305a\u3001<strong>\u5143\u306e\u884c\u3092\u6b8b\u3057\u305f\u307e\u307e<\/strong>\u3067\u96c6\u8a08\u3084\u9806\u4f4d\u4ed8\u3051\u304c\u3067\u304d\u308b\u5f37\u529b\u306a\u6a5f\u80fd\u3067\u3059\u3002<code>OVER<\/code>\u53e5\u3092\u4f7f\u3063\u3066\u8a08\u7b97\u5bfe\u8c61\u3068\u306a\u308b\u884c\u306e\u7bc4\u56f2\uff08\u30a6\u30a3\u30f3\u30c9\u30a6\uff09\u3092\u52d5\u7684\u306b\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong>\u9806\u4f4d\u4ed8\u3051<\/strong>: <code>RANK()<\/code>\uff08\u540c\u9806\u4f4d\u3092\u8003\u616e\uff09\u3001<code>DENSE_RANK()<\/code>\uff08\u540c\u9806\u4f4d\u3092\u8003\u616e\u3057\u3001\u6b21\u306e\u9806\u4f4d\u306f\u9023\u756a\uff09\u3001<code>ROW_NUMBER()<\/code>\uff08\u540c\u9806\u4f4d\u3067\u3082\u4e00\u610f\u306e\u9023\u756a\uff09\u3092\u4f7f\u3044\u5206\u3051\u3001\u58f2\u4e0a\u30e9\u30f3\u30ad\u30f3\u30b0\u3084\u6210\u7e3e\u9806\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u3059\u308b\u554f\u984c\u3067\u554f\u308f\u308c\u307e\u3059\u3002<\/li>\n<li><strong>\u7d2f\u7a4d\u8a08\u7b97<\/strong>: <code>SUM() OVER (ORDER BY ...)<\/code>\u306e\u3088\u3046\u306b\u4f7f\u3044\u3001\u5e74\u5ea6\u3054\u3068\u306e\u7d2f\u7a4d\u58f2\u4e0a\u3084\u3001\u6642\u7cfb\u5217\u3067\u306e\u5728\u5eab\u6570\u306e\u63a8\u79fb\u306a\u3069\u3092\u8a08\u7b97\u3067\u304d\u307e\u3059\u3002<\/li>\n<li><strong>\u79fb\u52d5\u5e73\u5747<\/strong>: <code>AVG() OVER (ORDER BY ... ROWS BETWEEN ...)<\/code>\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001\u76f4\u8fd13\u30f6\u6708\u306e\u5e73\u5747\u58f2\u4e0a\u306e\u3088\u3046\u306a\u3001\u4e00\u5b9a\u7bc4\u56f2\u3067\u306e\u5e73\u5747\u5024\u3092\u7b97\u51fa\u3059\u308b\u554f\u984c\u306b\u5bfe\u5fdc\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n<h3>\u96c6\u5408\u6f14\u7b97\u5b50\uff1aUNION, INTERSECT, EXCEPT \u306e\u9055\u3044<\/h3>\n<p>\u8907\u6570\u306e<code>SELECT<\/code>\u6587\u306e\u7d50\u679c\u3092\u4e00\u3064\u306b\u307e\u3068\u3081\u308b\u969b\u306b\u4f7f\u3046\u306e\u304c\u96c6\u5408\u6f14\u7b97\u5b50\u3067\u3059\u3002\u305d\u308c\u305e\u308c\u306e\u5f79\u5272\u3092\u6b63\u78ba\u306b\u7406\u89e3\u3057\u3066\u304a\u304f\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong><code>UNION<\/code><\/strong>: 2\u3064\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u306e<strong>\u548c\u96c6\u5408<\/strong>\u3092\u8fd4\u3057\u307e\u3059\uff08\u91cd\u8907\u884c\u306f\u6392\u9664\uff09\u3002\u6771\u4eac\u672c\u793e\u306e\u9867\u5ba2\u30ea\u30b9\u30c8\u3068\u5927\u962a\u652f\u793e\u306e\u9867\u5ba2\u30ea\u30b9\u30c8\u3092\u540d\u5bc4\u305b\u3057\u30661\u3064\u306e\u30ea\u30b9\u30c8\u306b\u3059\u308b\u3001\u3068\u3044\u3063\u305f\u30b1\u30fc\u30b9\u3067\u4f7f\u308f\u308c\u307e\u3059\u3002<\/li>\n<li><strong><code>UNION ALL<\/code><\/strong>: <code>UNION<\/code>\u3068\u4f3c\u3066\u3044\u307e\u3059\u304c\u3001<strong>\u91cd\u8907\u884c\u3092\u6392\u9664\u305b\u305a<\/strong>\u305d\u306e\u307e\u307e\u7d50\u5408\u3057\u307e\u3059\u3002\u5358\u7d14\u306b\u7d50\u679c\u3092\u7e26\u306b\u9023\u7d50\u3057\u305f\u3044\u5834\u5408\u3084\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u304c\u91cd\u8996\u3055\u308c\u308b\u5834\u9762\u3067\u6709\u52b9\u3067\u3059\u3002<\/li>\n<li><strong><code>INTERSECT<\/code><\/strong>: 2\u3064\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u306e<strong>\u7a4d\u96c6\u5408<\/strong>\u3092\u8fd4\u3057\u307e\u3059\u3002\u3064\u307e\u308a\u3001\u4e21\u65b9\u306b\u5171\u901a\u3057\u3066\u5b58\u5728\u3059\u308b\u884c\u306e\u307f\u3092\u62bd\u51fa\u3057\u307e\u3059\u3002\u300c\u30bb\u30df\u30ca\u30fcA\u306b\u3082\u30bb\u30df\u30ca\u30fcB\u306b\u3082\u53c2\u52a0\u3057\u305f\u9867\u5ba2\u300d\u3092\u62bd\u51fa\u3059\u308b\u3001\u3068\u3044\u3063\u305f\u8a2d\u554f\u3067\u5f79\u7acb\u3061\u307e\u3059\u3002<\/li>\n<li><strong><code>EXCEPT<\/code><\/strong>: 2\u3064\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u306e<strong>\u5dee\u96c6\u5408<\/strong>\u3092\u8fd4\u3057\u307e\u3059\u30021\u3064\u76ee\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u306b\u5b58\u5728\u3057\u30012\u3064\u76ee\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u306b\u306f\u5b58\u5728\u3057\u306a\u3044\u884c\u3092\u62bd\u51fa\u3057\u307e\u3059\u3002\u300c\u5546\u54c1\u3092\u8cfc\u5165\u3057\u305f\u304c\u3001\u307e\u3060\u30ec\u30d3\u30e5\u30fc\u3092\u6295\u7a3f\u3057\u3066\u3044\u306a\u3044\u30e6\u30fc\u30b6\u30fc\u300d\u3092\u63a2\u3059\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3067\u5229\u7528\u3057\u307e\u3059\u3002<\/li>\n<\/ul>\n<h3>GROUP BY \u306e\u62e1\u5f35\uff1aGROUPING SETS, ROLLUP, CUBE<\/h3>\n<p><code>GROUP BY<\/code>\u53e5\u3092\u62e1\u5f35\u3057\u3001\u5c0f\u8a08\u3084\u7dcf\u8a08\u3068\u3044\u3063\u305f\u8907\u6570\u306e\u7c92\u5ea6\u306e\u96c6\u8a08\u3092\u4e00\u5ea6\u306e\u30af\u30a8\u30ea\u3067\u53d6\u5f97\u3059\u308b\u305f\u3081\u306e\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong><code>ROLLUP<\/code><\/strong>: \u6307\u5b9a\u3057\u305f\u5217\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u3001\u968e\u5c64\u7684\u306a\u5c0f\u8a08\u3068\u7dcf\u8a08\u3092\u7b97\u51fa\u3057\u307e\u3059\u3002\u300c<code>ROLLUP(\u5e74\u5ea6, \u56db\u534a\u671f)<\/code>\u300d\u3068\u6307\u5b9a\u3059\u308c\u3070\u3001\u300c\u5e74\u5ea6\u3054\u3068\u30fb\u56db\u534a\u671f\u3054\u3068\u306e\u96c6\u8a08\u300d\u300c\u5e74\u5ea6\u3054\u3068\u306e\u5c0f\u8a08\u300d\u300c\u5168\u4f53\u306e\u7dcf\u8a08\u300d\u304c\u4e00\u5ea6\u306b\u5f97\u3089\u308c\u307e\u3059\u3002<\/li>\n<li><strong><code>CUBE<\/code><\/strong>: \u6307\u5b9a\u3057\u305f\u5217\u306e\u8003\u3048\u3046\u308b\u5168\u3066\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u96c6\u8a08\u7d50\u679c\u3092\u7b97\u51fa\u3057\u307e\u3059\u3002\u300c<code>CUBE(\u5546\u54c1\u30ab\u30c6\u30b4\u30ea, \u5730\u57df)<\/code>\u300d\u3068\u3059\u308c\u3070\u3001\u30ab\u30c6\u30b4\u30ea\u00d7\u5730\u57df\u306e\u96c6\u8a08\u306b\u52a0\u3048\u3001\u30ab\u30c6\u30b4\u30ea\u3054\u3068\u306e\u5c0f\u8a08\u3001\u5730\u57df\u3054\u3068\u306e\u5c0f\u8a08\u3001\u305d\u3057\u3066\u7dcf\u8a08\u307e\u3067\u7db2\u7f85\u7684\u306b\u51fa\u529b\u3067\u304d\u307e\u3059\u3002<\/li>\n<li><strong><code>GROUPING SETS<\/code><\/strong>: <code>ROLLUP<\/code>\u3084<code>CUBE<\/code>\u3068\u7570\u306a\u308a\u3001\u958b\u767a\u8005\u304c\u5fc5\u8981\u306a\u96c6\u8a08\u306e\u7d44\u307f\u5408\u308f\u305b\u3060\u3051\u3092\u660e\u793a\u7684\u306b\u6307\u5b9a\u3067\u304d\u307e\u3059\u3002\u3088\u308a\u67d4\u8edf\u306a\u96c6\u8a08\u304c\u53ef\u80fd\u3067\u3059\u3002<\/li>\n<\/ul>\n<h3>EXISTS \u3068 IN \u306e\u4f7f\u3044\u5206\u3051\u2502\u76f8\u95a2\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u4ee3\u8868\u683c<\/h3>\n<p><code>EXISTS<\/code>\u306f\u3001\u30b5\u30d6\u30af\u30a8\u30ea\uff08\u526f\u554f\u3044\u5408\u308f\u305b\uff09\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u306b\u884c\u304c\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u5224\u5b9a\u3057\u3001<code>TRUE<\/code>\u307e\u305f\u306f<code>FALSE<\/code>\u3092\u8fd4\u3059\u8ff0\u8a9e\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong><code>EXISTS<\/code>\u306e\u5f37\u307f<\/strong>: \u300c\uff5e\u3068\u3044\u3046\u6761\u4ef6\u306b\u5408\u81f4\u3059\u308b\u30c7\u30fc\u30bf\u304c<strong>\u4e00\u4ef6\u3067\u3082\u5b58\u5728\u3059\u308c\u3070\u826f\u3044<\/strong>\u300d\u3068\u3044\u3046\u5224\u5b9a\u306b\u4f7f\u3044\u307e\u3059\u3002\u30b5\u30d6\u30af\u30a8\u30ea\u5185\u3067\u6761\u4ef6\u306b\u5408\u3046\u884c\u304c1\u884c\u898b\u3064\u304b\u3063\u305f\u6642\u70b9\u3067\u8a55\u4fa1\u304c\u7d42\u4e86\u3059\u308b\u305f\u3081\u3001<code>IN<\/code>\u53e5\u3088\u308a\u3082\u9ad8\u901f\u306b\u52d5\u4f5c\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u7279\u306b\u300c\u4e00\u5ea6\u3067\u3082\u5546\u54c1A\u3092\u8cfc\u5165\u3057\u305f\u3053\u3068\u304c\u3042\u308b\u9867\u5ba2\u300d\u3092\u62bd\u51fa\u3059\u308b\u3088\u3046\u306a\u5834\u5408\u306b\u6709\u52b9\u3067\u3059\u3002<\/li>\n<li><strong><code>IN<\/code>\u3068\u306e\u9055\u3044<\/strong>: <code>IN<\/code>\u306f\u30b5\u30d6\u30af\u30a8\u30ea\u306e\u7d50\u679c\u30ea\u30b9\u30c8\u306b\u300c\u5024\u304c\u542b\u307e\u308c\u3066\u3044\u308b\u304b\u300d\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u306e\u306b\u5bfe\u3057\u3001<code>EXISTS<\/code>\u306f\u300c\u884c\u304c\u5b58\u5728\u3059\u308b\u304b\u300d\u3068\u3044\u3046\u5b58\u5728\u6709\u7121\u305d\u306e\u3082\u306e\u3092\u30c1\u30a7\u30c3\u30af\u3057\u307e\u3059\u3002\u3053\u306e\u9055\u3044\u304c\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u3084\u30af\u30a8\u30ea\u306e\u66f8\u304d\u65b9\u306b\u5f71\u97ff\u3057\u307e\u3059\u3002<\/li>\n<li><strong><code>NOT EXISTS<\/code><\/strong>: <code>EXISTS<\/code>\u306e\u9006\u3067\u3001\u4e00\u4ef6\u3082\u884c\u304c\u5b58\u5728\u3057\u306a\u3044\u5834\u5408\u306b<code>TRUE<\/code>\u3068\u306a\u308a\u307e\u3059\u3002\u300c\u307e\u3060\u4e00\u5ea6\u3082\u8cfc\u5165\u5c65\u6b74\u306e\u306a\u3044\u5546\u54c1\u300d\u3092\u63a2\u3059\u3001\u3068\u3044\u3063\u305f\u554f\u984c\u3067\u983b\u51fa\u3067\u3059\u3002<\/li>\n<\/ul>\n<h3>CTE\uff08\u5171\u901a\u30c6\u30fc\u30d6\u30eb\u5f0f\uff09\uff1aWITH\u53e5\u306b\u3088\u308bSQL\u306e\u53ef\u8aad\u6027\u5411\u4e0a<\/h3>\n<p><strong>CTE (Common Table Expression)<\/strong> \u306f\u3001<code>WITH<\/code>\u53e5\u3092\u4f7f\u3063\u3066\u3001\u8907\u96d1\u306aSQL\u6587\u3092\u4e00\u6642\u7684\u306a\u540d\u524d\u4ed8\u304d\u306e\u7d50\u679c\u30bb\u30c3\u30c8\uff08\u30c6\u30fc\u30d6\u30eb\uff09\u306b\u5206\u5272\u3057\u3066\u8a18\u8ff0\u3059\u308b\u6a5f\u80fd\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong>\u30e1\u30ea\u30c3\u30c8<\/strong>:\n<ul>\n<li><strong>\u53ef\u8aad\u6027\u306e\u5411\u4e0a<\/strong>: \u9577\u5927\u3067\u30cd\u30b9\u30c8\u304c\u6df1\u3044SQL\u3092\u3001\u51e6\u7406\u306e\u30b9\u30c6\u30c3\u30d7\u3054\u3068\u306b\u90e8\u54c1\u5316\u3067\u304d\u308b\u305f\u3081\u3001\u30b3\u30fc\u30c9\u304c\u975e\u5e38\u306b\u8aad\u307f\u3084\u3059\u304f\u306a\u308a\u307e\u3059\u3002<\/li>\n<li><strong>\u518d\u5229\u7528\u6027<\/strong>: \u4e00\u5ea6\u5b9a\u7fa9\u3057\u305fCTE\u3092\u3001\u540c\u3058\u30af\u30a8\u30ea\u5185\u3067\u4f55\u5ea6\u3082\u53c2\u7167\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u518d\u5e30CTE<\/strong>: <code>WITH RECURSIVE<\/code>\u3092\u4f7f\u3046\u3053\u3068\u3067\u3001CTE\u304c\u81ea\u5206\u81ea\u8eab\u3092\u53c2\u7167\u3059\u308b\u300c\u518d\u5e30\u30af\u30a8\u30ea\u300d\u3092\u8a18\u8ff0\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u7d44\u7e54\u56f3\u306e\u968e\u5c64\u3092\u305f\u3069\u3063\u305f\u308a\u3001\u90e8\u54c1\u8868\uff08BOM\uff09\u306e\u89aa\u5b50\u95a2\u4fc2\u3092\u5c55\u958b\u3057\u305f\u308a\u3059\u308b\u3088\u3046\u306a\u3001\u968e\u5c64\u69cb\u9020\u30c7\u30fc\u30bf\u3092\u6271\u3046\u554f\u984c\u3067\u5fc5\u9808\u306e\u30c6\u30af\u30cb\u30c3\u30af\u3067\u3059\u3002<\/li>\n<\/ul>\n<h3>MERGE\uff08UPSERT\uff09\u6587\u306b\u3088\u308b\u6761\u4ef6\u4ed8\u304dINSERT\/UPDATE<\/h3>\n<p><code>MERGE<\/code>\u6587\u306f\u3001\u5bfe\u8c61\u306e\u30c6\u30fc\u30d6\u30eb\u306b\u30c7\u30fc\u30bf\u304c\u5b58\u5728\u3059\u308b\u304b\u3069\u3046\u304b\u3092\u6761\u4ef6\u306b\u3001<code>UPDATE<\/code>\u51e6\u7406\u3068<code>INSERT<\/code>\u51e6\u7406\u3092\u81ea\u52d5\u3067\u632f\u308a\u5206\u3051\u308b\u6a5f\u80fd\u3067\u3059\u3002<strong>UPSERT<\/strong>\uff08UPDATE or INSERT\uff09\u3068\u3082\u547c\u3070\u308c\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong>\u5229\u7528\u30b7\u30fc\u30f3<\/strong>: \u65e5\u6b21\u30d0\u30c3\u30c1\u51e6\u7406\u3067\u30de\u30b9\u30bf\u30c7\u30fc\u30bf\u3092\u66f4\u65b0\u3059\u308b\u969b\u3001\u300c\u793e\u54e1\u756a\u53f7\u304c\u65e2\u306b\u5b58\u5728\u3059\u308c\u3070\u60c5\u5831\u3092\u66f4\u65b0\uff08<code>UPDATE<\/code>\uff09\u3057\u3001\u5b58\u5728\u3057\u306a\u3051\u308c\u3070\u65b0\u898f\u8ffd\u52a0\uff08<code>INSERT<\/code>\uff09\u3059\u308b\u300d\u3068\u3044\u3063\u305f\u51e6\u7406\u3092\u30011\u3064\u306eSQL\u6587\u3067\u5b8c\u7d50\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u308a\u3001\u51e6\u7406\u306e\u8a18\u8ff0\u304c\u30b7\u30f3\u30d7\u30eb\u306b\u306a\u308a\u3001\u30d1\u30d5\u30a9\u30fc\u30de\u30f3\u30b9\u5411\u4e0a\u3082\u671f\u5f85\u3067\u304d\u307e\u3059\u3002<\/li>\n<\/ul>\n<h3>WHERE \u3068 HAVING \u306e\u6c7a\u5b9a\u7684\u9055\u3044\u2502\u7d5e\u308a\u8fbc\u307f\u306e\u30bf\u30a4\u30df\u30f3\u30b0<\/h3>\n<p>\u3069\u3061\u3089\u3082\u884c\u3092\u7d5e\u308a\u8fbc\u3080\u305f\u3081\u306e\u53e5\u3067\u3059\u304c\u3001\u305d\u306e<strong>\u51e6\u7406\u30bf\u30a4\u30df\u30f3\u30b0<\/strong>\u304c\u6c7a\u5b9a\u7684\u306b\u7570\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li><strong><code>WHERE<\/code>\u53e5<\/strong>: <code>GROUP BY<\/code>\u3067\u96c6\u7d04\u3092\u884c\u3046<strong>\u524d<\/strong>\u306b\u3001\u5143\u306e\u30c6\u30fc\u30d6\u30eb\u306e\u5404\u884c\u306b\u5bfe\u3057\u3066\u7d5e\u308a\u8fbc\u307f\u3092\u884c\u3044\u307e\u3059\u3002<\/li>\n<li><strong><code>HAVING<\/code>\u53e5<\/strong>: <code>GROUP BY<\/code>\u3067\u96c6\u7d04\u3092\u884c\u3063\u305f<strong>\u5f8c<\/strong>\u306e\u7d50\u679c\u30bb\u30c3\u30c8\u306b\u5bfe\u3057\u3066\u3001\u7d5e\u308a\u8fbc\u307f\u3092\u884c\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<p>\u3053\u306e\u305f\u3081\u3001<code>SUM()<\/code>\u3084<code>AVG()<\/code>\u3068\u3044\u3063\u305f<strong>\u96c6\u8a08\u95a2\u6570\u3092\u6761\u4ef6\u306b\u4f7f\u3048\u308b\u306e\u306f<code>HAVING<\/code>\u53e5\u3060\u3051<\/strong>\u3067\u3059\u3002\u300c\u5408\u8a08\u91d1\u984d\u304c10,000\u5186\u4ee5\u4e0a\u306e\u9867\u5ba2\u30b0\u30eb\u30fc\u30d7\u306e\u307f\u3092\u62bd\u51fa\u3059\u308b\u300d\u3068\u3044\u3063\u305f\u6761\u4ef6\u306f\u3001\u96c6\u7d04\u5f8c\u3067\u306a\u3044\u3068\u8a55\u4fa1\u3067\u304d\u306a\u3044\u305f\u3081<code>HAVING<\/code>\u53e5\u3067\u8a18\u8ff0\u3057\u307e\u3059\u3002<\/p>\n<h3>\u5348\u5f8c\u8a66\u9a13\u306e\u7f60\uff1aJOIN \u3068 UNION \u306e\u6df7\u540c\u306b\u6ce8\u610f<\/h3>\n<p>\u6700\u5f8c\u306b\u3001\u5348\u5f8c\u8a66\u9a13\u3067\u53d7\u9a13\u8005\u304c\u9665\u308a\u3084\u3059\u3044\u3072\u3063\u304b\u3051\u30dd\u30a4\u30f3\u30c8\u3067\u3059\u3002<\/p>\n<ul>\n<li><strong><code>JOIN<\/code><\/strong>: \u30c6\u30fc\u30d6\u30eb\u3092<strong>\u6a2a\u65b9\u5411\uff08\u5217\u65b9\u5411\uff09\u306b\u9023\u7d50<\/strong>\u3057\u307e\u3059\u3002\u9867\u5ba2\u30c6\u30fc\u30d6\u30eb\u3068\u6ce8\u6587\u30c6\u30fc\u30d6\u30eb\u3092\u9867\u5ba2ID\u3067\u7d10\u3065\u3051\u3066\u3001\u9867\u5ba2\u540d\u3068\u6ce8\u6587\u65e5\u3092\u4e26\u3079\u3066\u8868\u793a\u3059\u308b\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3067\u4f7f\u3044\u307e\u3059\u3002<\/li>\n<li><strong><code>UNION<\/code><\/strong>: \u7d50\u679c\u30bb\u30c3\u30c8\u3092<strong>\u7e26\u65b9\u5411\uff08\u884c\u65b9\u5411\uff09\u306b\u9023\u7d50<\/strong>\u3057\u307e\u3059\u3002\u30a2\u30af\u30c6\u30a3\u30d6\u4f1a\u54e1\u30c6\u30fc\u30d6\u30eb\u3068\u4f11\u7720\u4f1a\u54e1\u30c6\u30fc\u30d6\u30eb\u3092\u7e26\u306b\u7e4b\u3044\u3067\u3001\u5168\u4f1a\u54e1\u30ea\u30b9\u30c8\u3092\u4f5c\u6210\u3059\u308b\u3088\u3046\u306a\u30b1\u30fc\u30b9\u3067\u4f7f\u3044\u307e\u3059\u3002<\/li>\n<\/ul>\n<p>\u554f\u984c\u6587\u3092\u3088\u304f\u8aad\u307f\u3001\u300c\u8907\u6570\u306e\u30c6\u30fc\u30d6\u30eb\u304b\u3089\u95a2\u9023\u60c5\u5831\u3092\u7d10\u3065\u3051\u3066\u5217\u3092\u5897\u3084\u3057\u305f\u3044\u306e\u304b\u300d\u3001\u305d\u308c\u3068\u3082\u300c\u69cb\u9020\u304c\u540c\u3058\u8907\u6570\u306e\u7d50\u679c\u3092\u4e00\u3064\u306b\u307e\u3068\u3081\u3066\u884c\u3092\u5897\u3084\u3057\u305f\u3044\u306e\u304b\u300d\u3092\u6b63\u78ba\u306b\u898b\u6975\u3081\u308b\u3053\u3068\u304c\u3001\u6b63\u89e3\u3078\u306e\u9375\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SELECT * FROM &#8230; \u306f\u3082\u3046\u5352\u696d\u3002\u3042\u306a\u305f\u306f\u3001\u5b9f\u52d9\u306e\u8907\u96d1\u306a\u30c7\u30fc\u30bf\u8981\u6c42\u3084\u3001\u5fdc\u7528\u60c5\u5831\u30fb\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u30b9\u30da-\u30b7\u30e3\u30ea\u30b9\u30c8\u8a66\u9a13\u306e\u5348\u5f8c\u554f\u984c\u3067\u51fa\u984c\u3055\u308c\u308b\u3088\u3046\u306a\u3001\u4e00\u7b4b\u7e04\u3067\u306f\u3044\u304b\u306a\u3044SQL\u306b\u982d\u3092\u60a9\u307e\u305b\u3066\u3044\u307e\u305b &#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-6062","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\/6062","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=6062"}],"version-history":[{"count":0,"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/posts\/6062\/revisions"}],"wp:attachment":[{"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/media?parent=6062"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/categories?post=6062"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sky-peace.net\/certified\/wp-json\/wp\/v2\/tags?post=6062"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}