What is JOIN?

  • JOIN is a clause that is used to combine rows from two or more tables based on a related column between them, such as a foreign key.
  • Example:
transactions + customers
transaction_id amount customer_id
primary key foreign key
1000 4.99 3
1001 2.86 2
1002 3.33 3
1003 2.15 1
+
customer_id name
primary key
1 Hans
2 John
3 Laila

=
transaction_id amount customer_id name
primary key foreign key
1000 4.99 3 Laila
1001 2.86 2 John
1002 3.33 3 Laila
1003 2.15 1 Hans

Demonstration

  • Assume that you already have all the data key in into the system (here for reference), let's insert another row into "transactions" table.
INSERT INTO transactions (amount, customer_id)
VALUES (1.00, NULL);
SELECT * FROM transactions;

transaction_id amount customer_id
primary key foreign key
1000 4.99 3
1001 2.86 2
1002 3.33 3
1003 2.15 1
1004 1.00 NULL

  • Now, let's add another customer into "customers" table:
INSERT INTO customers (name)
VALUES ("Julia");
SELECT * FROM customers;

customer_id name
primary key
1 Hans
2 John
3 Laila
4 Julia

  • So, here, not all transactions have a customer ID, and not all customers have ever initiated a transaction. Maybe, they are registered as a customer, but they never bought anything.

Inner Join

  • To create an inner join, if we want the transactions table to be on the left, and customers table to be on the right, you may type in the following code:
SELECT *
FROM transactions INNER JOIN customers
ON transactions.customer_id = customers.customer_id;
  • In this query, the join is performed by matching the values of the "customer_id" column in both tables. It connects related data between the "transactions" table and the "customers" table based on the shared customer ID.
  • The result of this query would be a table that combines the columns from both tables, including all columns from the "transactions" table and all columns from the "customers" table.
  • The rows in the result set would include only the records where the customer ID exists in both tables, creating a relationship between the two based on the foreign key and primary key relationship.

transaction_id amount customer_id name
1000 4.99 3 Laila
1001 2.86 2 John
1002 3.33 3 Laila
1003 2.15 1 Hans

Inner Join with Specific Column

  • It is not necessary for us to display all column, we can select which column we want it to display, for instance:
SELECT *
FROM transactions INNER JOIN customers
ON transactions.customer_id = customers.customer_id;

transaction_id amount name
1000 4.99 Laila
1001 2.86 John
1002 3.33 Laila
1003 2.15 Hans

Left Join

  • In left join, we will display everythong from the table on the left.
  • Let's try this:
SELECT *
FROM transactions LEFT JOIN customers
ON transactions.customer_id = customers.customer_id;;

transaction_id amount customer_id customer_id name
1000 4.99 3 3 Laila
1001 2.86 2 2 John
1002 3.33 3 3 Laila
1003 2.15 1 1 Hans
1004 1.00 NULL NULL NULL

Right Join

  • In right join, we will display everything from the table on the right.
  • Let's try this:
SELECT *
FROM transactions RIGHT JOIN customers
ON transactions.customer_id = customers.customer_id;;

transaction_id amount customer_id customer_id name
1003 2.15 1 1 Hans
1001 2.86 2 2 John
1000 4.99 3 3 Laila
1002 3.33 3 3 Laila
NULL NULL NULL 4 Julia