Joining the Dots: Mastering Inner, Left, Right, and Full Joins in SQL
Introduction to Joins
In SQL, a join is used to combine rows from two or more tables based on a related column between them. Joins are a fundamental concept in database querying, allowing you to retrieve data from multiple tables in a single query. In this masterclass, we'll delve into the basics of inner, left, right, and full joins, exploring their syntax, use cases, and examples.
Inner Join
An inner join returns only the rows that have a match in both tables. It's the most common type of join and is used when you want to retrieve data that exists in both tables.
Syntax:
SELECT *
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
Example:
Suppose we have two tables, orders and customers, with the following data:
orders table:
| order_id | customer_id | order_date | | --- | --- | --- | | 1 | 1 | 2022-01-01 | | 2 | 1 | 2022-01-15 | | 3 | 2 | 2022-02-01 |
customers table:
| customer_id | name | email | | --- | --- | --- | | 1 | John Doe | johndoe@example.com | | 2 | Jane Doe | janedoe@example.com | | 3 | Bob Smith | bobsmith@example.com |
To retrieve the orders with their corresponding customer information, we can use an inner join:
SELECT orders.order_id, customers.name, customers.email
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.customer_id;
Result:
| order_id | name | email | | --- | --- | --- | | 1 | John Doe | johndoe@example.com | | 2 | John Doe | johndoe@example.com | | 3 | Jane Doe | janedoe@example.com |
Left Join (or Left Outer Join)
A left join returns all the rows from the left table and the matching rows from the right table. If there's no match, the result will contain NULL values for the right table columns.
Syntax:
SELECT *
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Example:
Using the same orders and customers tables, let's retrieve all orders with their corresponding customer information, including orders without a matching customer:
SELECT orders.order_id, customers.name, customers.email
FROM orders
LEFT JOIN customers
ON orders.customer_id = customers.customer_id;
Result:
| order_id | name | email | | --- | --- | --- | | 1 | John Doe | johndoe@example.com | | 2 | John Doe | johndoe@example.com | | 3 | Jane Doe | janedoe@example.com | | 4 | NULL | NULL |
Note that the last row has NULL values for the name and email columns because there's no matching customer for the order with order_id = 4.
Right Join (or Right Outer Join)
A right join is similar to a left join, but it returns all the rows from the right table and the matching rows from the left table.
Syntax:
SELECT *
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
Example:
Let's retrieve all customers with their corresponding order information:
SELECT customers.customer_id, orders.order_id
FROM orders
RIGHT JOIN customers
ON orders.customer_id = customers.customer_id;
Result:
| customer_id | order_id | | --- | --- | | 1 | 1 | | 1 | 2 | | 2 | 3 | | 3 | NULL |
Note that the last row has a NULL value for the order_id column because there's no matching order for the customer with customer_id = 3.
Full Join (or Full Outer Join)
A full join returns all rows from both tables, with NULL values in the columns where there's no match.
Syntax:
SELECT *
FROM table1
FULL JOIN table2
ON table1.column_name = table2.column_name;
Example:
Let's retrieve all orders and customers, with NULL values where there's no match:
SELECT orders.order_id, customers.customer_id
FROM orders
FULL JOIN customers
ON orders.customer_id = customers.customer_id;
Result:
| order_id | customer_id | | --- | --- | | 1 | 1 | | 2 | 1 | | 3 | 2 | | 4 | NULL | | NULL | 3 |
Note that the last two rows have NULL values in one of the columns because there's no match between the orders and customers tables.
Conclusion
In this masterclass, we've explored the basics of inner, left, right, and full joins in SQL. We've seen how to use each type of join to retrieve data from multiple tables, with examples and syntax explanations. Remember to practice using these joins to become more comfortable with querying complex data sets.
⚔️ YOUR MISSION
Mission: Joining the Dots
Your mission is to write a SQL query that retrieves the names of all customers who have placed an order, along with the order date and total amount. Use the orders and customers tables provided earlier.
Requirements:
- Use an inner join to combine the
ordersandcustomerstables. - Retrieve the
namecolumn from thecustomerstable and theorder_dateandtotal_amountcolumns from theorderstable. - Filter the results to only include customers who have placed an order (i.e., exclude customers without any orders).
Tips and Hints:
- Use the
INNER JOINclause to combine the two tables. - Use the
ONclause to specify the join condition. - Use the
WHEREclause to filter the results.
Read the exercise prompt
SELECT customers.name, orders.order_date, orders.total_amount
FROM orders
INNER JOIN customers
ON orders.customer_id = customers.customer_id;
