KevsRobots Learning Platform
By Kevin McAleer, 6 Minutes
In this lesson, weโll explore how to work with multiple tables in a relational database using joins. Joins are essential for combining data across tables, helping you retrieve meaningful insights by connecting related data.
In relational databases, data is often distributed across multiple tables. Joins allow us to retrieve data from two or more tables based on related columns, typically through primary and foreign keys.
NULL.NULL.NULL.An INNER JOIN returns records that have matching values in both tables.
SELECT columns
FROM table1
INNER JOIN table2
ON table1.common_column = table2.common_column;
users and orders TablesSuppose we have two tables:
users
| user_id | name |
|---------|---------|
| 1 | Alice |
| 2 | Bob |
| 3 | Charlie |
orders
| order_id | user_id | item |
|----------|---------|-------------|
| 101 | 1 | Laptop |
| 102 | 2 | Smartphone |
| 103 | 1 | Tablet |
Example: Retrieve each user and their orders.
SELECT users.name, orders.item
FROM users
INNER JOIN orders ON users.user_id = orders.user_id;
Result:
| name | item |
|--------|------------|
| Alice | Laptop |
| Bob | Smartphone |
| Alice | Tablet |
Only users with orders appear in the results.
A LEFT JOIN returns all records from the left table and the matched records from the right table. If there is no match, the result is NULL for the columns from the right table.
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.common_column = table2.common_column;
users and ordersExample: Retrieve all users and their orders, including users without orders.
SELECT users.name, orders.item
FROM users
LEFT JOIN orders ON users.user_id = orders.user_id;
Result:
| name | item |
|----------|------------|
| Alice | Laptop |
| Bob | Smartphone |
| Alice | Tablet |
| Charlie | NULL |
The NULL value indicates that Charlie has no orders.
A RIGHT JOIN is the opposite of a left join. It returns all records from the right table and the matched records from the left table, with NULL for unmatched rows from the left.
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.common_column = table2.common_column;
RIGHT JOIN with users and orders, weโd retrieve all orders, with NULL for users without matching entries. This syntax is more commonly supported in MySQL and PostgreSQL than SQLite.Note: Not all SQL databases support
RIGHT JOIN. SQLite, for example, does not supportRIGHT JOINdirectly.
A FULL JOIN returns all records when thereโs a match in either table. Unmatched rows from each table show NULL for the other tableโs columns.
SELECT columns
FROM table1
FULL JOIN table2
ON table1.common_column = table2.common_column;
NULL where thereโs no match in either table.Note: SQLite does not support
FULL JOINdirectly, but similar results can be achieved usingUNIONofLEFT JOINandRIGHT JOIN.
A self join is when a table is joined with itself. This is useful for hierarchical data or when finding relationships within the same table.
employees Tableemployees
| emp_id | name | manager_id |
|--------|----------|------------|
| 1 | Alice | NULL |
| 2 | Bob | 1 |
| 3 | Charlie | 1 |
| 4 | Diana | 2 |
Example: Retrieve each employee and their managerโs name.
SELECT e1.name AS employee, e2.name AS manager
FROM employees AS e1
LEFT JOIN employees AS e2 ON e1.manager_id = e2.emp_id;
Result:
| employee | manager |
|----------|---------|
| Alice | NULL |
| Bob | Alice |
| Charlie | Alice |
| Diana | Bob |
Inner Join: Retrieve all users and their orders using an inner join.
SELECT users.name, orders.item
FROM users
INNER JOIN orders ON users.user_id = orders.user_id;
Left Join: Retrieve all users, including those without orders.
SELECT users.name, orders.item
FROM users
LEFT JOIN orders ON users.user_id = orders.user_id;
Self Join: Retrieve each employee and their manager from the employees table.
SELECT e1.name AS employee, e2.name AS manager
FROM employees AS e1
LEFT JOIN employees AS e2 ON e1.manager_id = e2.emp_id;
Full Join (Advanced): If your database supports it, retrieve all users and orders using a full join.
Hereโs a summary of the join types covered in this lesson:
| Join Type | Description | Example |
|---|---|---|
| INNER JOIN | Returns records with matching values in both tables | INNER JOIN orders ON users.user_id = orders.user_id |
| LEFT JOIN | Returns all records from the left table and matched records from the right | LEFT JOIN orders ON users.user_id = orders.user_id |
| RIGHT JOIN | Returns all records from the right table and matched records from the left | RIGHT JOIN orders ON users.user_id = orders.user_id |
| FULL JOIN | Returns all records when thereโs a match in either table | FULL JOIN orders ON users.user_id = orders.user_id |
| SELF JOIN | Joins a table with itself to compare rows within the same table | FROM employees AS e1 LEFT JOIN employees AS e2 |
By mastering joins, you can retrieve and analyze data from multiple tables effectively. In the next lesson, weโll focus on grouping and aggregating data for even more insightful analyses.
You can use the arrows โ โ on your keyboard to navigate between lessons.
Comments