<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>データベース &#8211; セキュリティエンジニアの雑多ブログ</title>
	<atom:link href="https://cybermemo.blog/tag/database/feed" rel="self" type="application/rss+xml" />
	<link>https://cybermemo.blog</link>
	<description>セキュリティエンジニアの学習記録</description>
	<lastBuildDate>Sun, 14 Dec 2025 08:10:33 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://cybermemo.blog/wp-content/uploads/2025/10/cropped-トップアイコン-32x32.jpg</url>
	<title>データベース &#8211; セキュリティエンジニアの雑多ブログ</title>
	<link>https://cybermemo.blog</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>外部結合ってどんな時に使うの？</title>
		<link>https://cybermemo.blog/when-to-use-outer-join</link>
					<comments>https://cybermemo.blog/when-to-use-outer-join#respond</comments>
		
		<dc:creator><![CDATA[miyuki]]></dc:creator>
		<pubDate>Sun, 14 Dec 2025 08:09:43 +0000</pubDate>
				<category><![CDATA[コマンド]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[データベース]]></category>
		<guid isPermaLink="false">https://cybermemo.blog/?p=584</guid>

					<description><![CDATA[<p><img src="https://cybermemo.blog/wp-content/uploads/2025/10/コマンド用アイキャッチ.jpg" class="webfeedsFeaturedVisual" /></p>はじめに SQLの勉強をしていて、外部結合をどんな時に使うのかイメージできなかったので調べてみた。 内部結合と外部結合の違い 内部結合（INNER JOIN） 両方のテーブルに 一致するデータがある行だけ 出す。「関係が [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><img src="https://cybermemo.blog/wp-content/uploads/2025/10/コマンド用アイキャッチ.jpg" class="webfeedsFeaturedVisual" /></p>
<h2 class="wp-block-heading">はじめに</h2>



<p>SQLの勉強をしていて、外部結合をどんな時に使うのかイメージできなかったので調べてみた。</p>



<h2 class="wp-block-heading">内部結合と外部結合の違い</h2>



<h3 class="wp-block-heading">内部結合（INNER JOIN）</h3>



<p>両方のテーブルに <strong>一致するデータがある行だけ</strong> 出す。<br>「関係が成立しているものだけ見たい」</p>



<h3 class="wp-block-heading">外部結合（OUTER JOIN）</h3>



<p>一致しない行も <strong>欠けたまま（NULL）</strong>で出す。<br>「関係が成立していないものも把握したい」</p>



<p class="is-style-icon_pen"><strong>”存在していない事実<strong>”</strong>を確認したいとき</strong>が外部結合の出番。</p>



<h2 class="wp-block-heading">サンプルデータを用意</h2>



<h3 class="wp-block-heading">シナリオ</h3>



<p>登録だけして注文をしたことがない人を確認したい。</p>



<h3 class="wp-block-heading">usersテーブル</h3>



<p>登録ユーザーが記録されたテーブル。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>user_id</th><th>name</th></tr></thead><tbody><tr><td>1</td><td>Sato</td></tr><tr><td>2</td><td>Suzuki</td></tr><tr><td>3</td><td>Tanaka</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">ordersテーブル</h3>



<p>注文履歴が記録されたテーブル。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>order_id</th><th>user_id</th><th>amount</th></tr></thead><tbody><tr><td>101</td><td>1</td><td>3000</td></tr><tr><td>102</td><td>3</td><td>1500</td></tr></tbody></table></figure>



<h2 class="wp-block-heading">外部結合でデータを抽出する</h2>



<h3 class="wp-block-heading">SQLで左外部結合</h3>



<pre class="wp-block-code"><code>SELECT
  u.user_id, u.name,
  o.order_id, o.order_date, o.amount
FROM users u
LEFT OUTER JOIN orders o
  ON u.user_id = o.user_id
ORDER BY u.user_id, o.order_id;</code></pre>



<h3 class="wp-block-heading">結果</h3>



<p>Suzukiさんは ordersテーブル に対応するデータがないため、注文していないことが分かる。</p>



<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>user_id</th><th>name</th><th>order_id</th><th>order_date</th><th>amount</th></tr></thead><tbody><tr><td>1</td><td>Sato</td><td>101</td><td>2025-12-06</td><td>3000</td></tr><tr><td data-has-cell-bg="1" data-text-color="black"><span class="swl-cell-bg has-swl-pale-01-background-color" data-text-color="black" aria-hidden="true"> </span>2</td><td data-has-cell-bg="1" data-text-color="black"><span class="swl-cell-bg has-swl-pale-01-background-color" data-text-color="black" aria-hidden="true"> </span>Suzuki</td><td data-has-cell-bg="1" data-text-color="black"><strong><span class="swl-cell-bg has-swl-pale-01-background-color" data-text-color="black" aria-hidden="true"> </span>NULL</strong></td><td data-has-cell-bg="1" data-text-color="black"><strong><span class="swl-cell-bg has-swl-pale-01-background-color" data-text-color="black" aria-hidden="true"> </span>NULL</strong></td><td data-has-cell-bg="1" data-text-color="black"><strong><span class="swl-cell-bg has-swl-pale-01-background-color" data-text-color="black" aria-hidden="true"> </span>NULL</strong></td></tr><tr><td>3</td><td>Tanaka</td><td>102</td><td>2025-12-07</td><td>1500</td></tr></tbody></table></figure>



<h3 class="wp-block-heading">解説</h3>



<p>左外部結合なので usersテーブル に ordersテーブル をくっつけている。<br>そのため、ordersテーブルに値がなくてもNULL表示される。<br>ordersテーブルの値がNULL = 注文をしたことがない人が分かる。</p>



<figure class="wp-block-image size-full"><img decoding="async" width="476" height="309" src="https://cybermemo.blog/wp-content/uploads/2025/12/ベン図_左外部結合-1.jpg" alt="ベン図_左外部結合" class="wp-image-606" srcset="https://cybermemo.blog/wp-content/uploads/2025/12/ベン図_左外部結合-1.jpg 476w, https://cybermemo.blog/wp-content/uploads/2025/12/ベン図_左外部結合-1-300x195.jpg 300w" sizes="(max-width: 476px) 100vw, 476px" /></figure>



<h2 class="wp-block-heading">結論</h2>



<p>外部結合は「ある／ない」を確認したいときに使える。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://cybermemo.blog/when-to-use-outer-join/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
