You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tait.tech/_site/2020-01-23-sql-joins.html

197 lines
5.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SQL Joins</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="wrapper">
<nav>
<input type="checkbox" id="menu">
<label for="menu">&#9776;</label>
<div class="menu-content">
<a href="/" class="nav-link" >Home</a>
<a href="/blog/" class="nav-link" >Blog</a>
<a href="https://github.com/TTWNO/" class="nav-link" target="_blank" rel="noopener noreferrer" >Code</a>
<a href="/links/" class="nav-link" >Links</a>
</div>
</nav>
<h1>SQL Joins</h1>
<h4 class="post-date line-under"></h4>
<div class="article">
<p>When my database teacher started talking about <code class="highlighter-rouge">JOIN</code> statments, my mind started thinking about how blind partisan politics is dangerous. I started reading an article right there in class. I stopped listening to the teacher and I started thinking in my own world. (The article was <a href="https://colinmcmahonauthor.com/2016/11/02/how-not-to-be-partisan/">This one</a> by the way. Its pretty good no matter your political orientation, even iwth his bias acknowledged.)</p>
<p>Back to my main point: There are entire websites, and many <a href="https://TODO">YouTube channels</a> covering SQL. In this article though, I simply want to explain what a <code class="highlighter-rouge">JOIN</code> statment is in theory, why it can be useful, and how to use it in SQLite3—this is not the language used in my database class but it is very esay to get running, unlike Oracle SQL.</p>
<p>The simplest join, in my opinion, is the <code class="highlighter-rouge">JOIN ... ON</code> pattern. This joins two tables by a common column. To show an example, I must first setup a very basic database. It may look intimidating at first, but just take your time in soaking in the information.</p>
<h3 id="the-database">The Database</h3>
<p>This database will hold 4 tables.</p>
<ul>
<li><code class="highlighter-rouge">'books'</code> which will have a 13-digit ISBN, and a book title associated with that ISBN.</li>
<li><code class="highlighter-rouge">'customers'</code> which will have a first names, last name, address, and ID for all customers.</li>
<li><code class="highlighter-rouge">'orders'</code> which will contain an order ID, and a customer ID associated with it.</li>
<li><code class="highlighter-rouge">'order_items'</code> which will contain an order ID, and an ISBN associated with that order. There may be multiple rows with the same order ID, as there may be multiple books per order.</li>
</ul>
<p>Here is the <code class="highlighter-rouge">books</code> table:</p>
<table>
<thead>
<tr>
<th>ISBN</th>
<th>Title</th>
</tr>
</thead>
<tbody>
<tr>
<td>xxxxxxxxxx</td>
<td>s1</td>
</tr>
<tr>
<td>xxxxxxxxxx</td>
<td>s1</td>
</tr>
<tr>
<td>xxxxxxxxxx</td>
<td>s1</td>
</tr>
<tr>
<td>xxxxxxxxxx</td>
<td>s1</td>
</tr>
</tbody>
</table>
<p>This is the customers table:</p>
<table>
<thead>
<tr>
<th>ID</th>
<th>firstname</th>
<th>lastname</th>
<th>address</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Joe</td>
<td>Smith</td>
<td>123 Main St. NW</td>
</tr>
<tr>
<td>1</td>
<td>Joe</td>
<td>Smith</td>
<td>123 Main St. NW</td>
</tr>
<tr>
<td>1</td>
<td>Joe</td>
<td>Smith</td>
<td>123 Main St. NW</td>
</tr>
<tr>
<td>1</td>
<td>Joe</td>
<td>Smith</td>
<td>123 Main St. NW</td>
</tr>
</tbody>
</table>
<p>This is the orders table:</p>
<table>
<thead>
<tr>
<th>CustomerID</th>
<th>OrderID</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
</tr>
<tr>
<td>1</td>
<td>1</td>
</tr>
</tbody>
</table>
<p>Finally, this is the orderItems table:</p>
<table>
<thead>
<tr>
<th>orderID</th>
<th>ISBN</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>xxxxxxxxxx</td>
</tr>
<tr>
<td>1</td>
<td>xxxxxxxxxx</td>
</tr>
<tr>
<td>1</td>
<td>xxxxxxxxxx</td>
</tr>
<tr>
<td>1</td>
<td>xxxxxxxxxx</td>
</tr>
<tr>
<td>1</td>
<td>xxxxxxxxxx</td>
</tr>
</tbody>
</table>
<p>You dont have to memorize any of this information. The point is that ou can understand the relationship between the parts of this database. These tables are relate to eachother.</p>
<h3 id="joinon"><code class="highlighter-rouge">JOIN...ON</code></h3>
<p>Here is an example of using the <code class="highlighter-rouge">JOIN...ON</code> pattern. Here, we must specify which two columns will join the table.</p>
<div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>SELECT customers.firstname, customers.lastname, customers.address, orders.orderID
FROM customers
JOIN orders ON customers.ID=orders.customerID
</code></pre></div></div>
</div>
</div>
</body>
</html>