SQL Joins

When my database teacher started talking about JOIN 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 This one by the way. It’s pretty good no matter your political orientation, even iwth his bias acknowledged.)

Back to my main point: There are entire websites, and many YouTube channels covering SQL. In this article though, I simply want to explain what a JOIN 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.

The simplest join, in my opinion, is the JOIN ... ON 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.

The Database

This database will hold 4 tables.

Here is the books table:

ISBN Title
xxxxxxxxxx s1
xxxxxxxxxx s1
xxxxxxxxxx s1
xxxxxxxxxx s1

This is the customers table:

ID firstname lastname address
1 Joe Smith 123 Main St. NW
1 Joe Smith 123 Main St. NW
1 Joe Smith 123 Main St. NW
1 Joe Smith 123 Main St. NW

This is the orders table:

CustomerID OrderID
1 1
1 1
1 1
1 1
1 1

Finally, this is the orderItems table:

orderID ISBN
1 xxxxxxxxxx
1 xxxxxxxxxx
1 xxxxxxxxxx
1 xxxxxxxxxx
1 xxxxxxxxxx

You don’t 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.

JOIN...ON

Here is an example of using the JOIN...ON pattern. Here, we must specify which two columns will join the table.

SELECT customers.firstname, customers.lastname, customers.address, orders.orderID
       FROM customers
       JOIN orders ON customers.ID=orders.customerID