114532 Views
101685 Views
86270 Views
54891 Views
51137 Views
49962 Views
Level Up your CAD Skills
Operation Pico
Raspberry Pi Home Hub
Hacky Temperature and Humidity Sensor
Robot Makers Almanac
High Five Bot
Using the Raspberry Pi Pico's Built-in Temperature Sensor
Getting Started with SQL
Introduction to the Linux Command Line on Raspberry Pi OS
How to install MicroPython
Wall Drawing Robot Tutorial
BrachioGraph Tutorial
KevsRobots Learning Platform
54% Percent Complete
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
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
orders
Suppose we have two tables:
| user_id | name | |---------|---------| | 1 | Alice | | 2 | Bob | | 3 | Charlie |
| 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;
Example: 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;
| 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
Note: Not all SQL databases support RIGHT JOIN. SQLite, for example, does not support RIGHT JOIN directly.
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;
Note: SQLite does not support FULL JOIN directly, but similar results can be achieved using UNION of LEFT JOIN and RIGHT JOIN.
FULL JOIN
UNION
LEFT 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
| 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;
| employee | manager | |----------|---------| | Alice | NULL | | Bob | Alice | | Charlie | Alice | | Diana | Bob |
Inner Join: Retrieve all users and their orders using an inner join.
Left Join: Retrieve all users, including those without orders.
Self Join: Retrieve each employee and their manager from the employees table.
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:
INNER JOIN orders ON users.user_id = orders.user_id
LEFT JOIN orders ON users.user_id = orders.user_id
RIGHT JOIN orders ON users.user_id = orders.user_id
FULL JOIN orders ON users.user_id = orders.user_id
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.
< Previous Next >