SQL Joins Explained: What Is A SQL Join?
SQL Joins Explained: What Is A SQL Join?
Typically, you need to extract, transform, and load data into your RDBMS before you’re
able to manage it using SQL, which you can accomplish by using a tool like Stitch.
http://www.sql-join.com/ 1/5
10/31/2018 SQL Joins Explained
Customers
Here, information about each customer is stored in its own row, with columns specifying
different bits of information, including their rst name, last name, and email address.
Additionally, we associate a unique customer number, or primary key, with each customer
record.
Orders
1 07/04/1776 $234.56 1
2 03/14/1760 $78.50 3
3 05/23/1784 $124.00 2
http://www.sql-join.com/ 2/5
10/31/2018 SQL Joins Explained
4 09/03/1790 $65.50 3
5 07/21/1795 $25.50 10
6 11/27/1787 $14.40 9
Again, each row contains information about a speci c order. Each order has its own
unique identi cation key – order_id for this table – assigned to it as well.
Relational Model
You’ve probably noticed that these two examples share similar information. You can see
these simple relations diagrammed below:
Note that the orders table contains two keys: one for the order and one for the customer
who placed that order. In scenarios when there are multiple keys in a table, the key that
refers to the entity being described in that table is called the primary key (PK) and other
key is called a foreign key (FK).
In our example, order_id is a primary key in the orders table, while customer_id is both
a primary key in the customers table and a foreign key in the orders table. Primary and
foreign keys are essential to describing relations between the tables, and in performing
SQL joins.
Let’s say we want to nd all orders placed by a particular customer. We can do this by
joining the customers and orders tables together using the relationship established by
the customer_id key:
http://www.sql-join.com/ 3/5
10/31/2018 SQL Joins Explained
1 select order_date, order_amount
2 from customers
3 join orders
4 on customers.customer_id = orders.customer_id
5 where customer_id = 3
Here, we’re joining the two tables using the join keyword, and specifying what key to use
when joining the tables in the on customers.customer_id = orders.customer_id
line following the join statement. Here is the result of the above SQL query, which
includes two orders placed by Thomas Jefferson (customer_id = 3):
2 3/14/1760 $78.50
4 9/03/1790 $65.50
This particular join is an example of an “inner” join. Depending on the kind of analysis
you’d like to perform, you may want to use a different method. There are actually a
number of different ways to join the two tables together, depending on your application.
The next section will explain inner, left, right, and full joins, and provide examples using
the data tables used above.
NEXT PAGE →
MYSQL TO REDSHIFT | HEROKU TO POSTGRES | ADWORDS TO BIGQUERY | SALESFORCE TO SNOWFLAKE | GOOGLE ANALYTICS
TO PANOPLY | QUERY MONGO | CUSTOMER LIFETIME VALUE | CHURN RATE | COHORT ANALYSIS | ETL DATABASE | DATA
INTEGRATION | COLUMNAR DATABASE
http://www.sql-join.com/ 4/5
10/31/2018 SQL Joins Explained
TRY STITCH
14-day free trial | setup in minutes | no ETL scripts necessary
http://www.sql-join.com/ 5/5