Build Your Own AI Assistant Part 1 - Creating the Assistant
116820 Views
Is the new Raspberry Pi AI Kit better than Google Coral?
114678 Views
Control Arduino with Python using Firmata / PyFirmata
87081 Views
How to Map with LiDAR - using a Raspberry Pi Zero 2W, RPLidar and Rviz
57314 Views
Creating a Supercomputer with a Raspberry Pi 5 Cluster and Docker Swarm!
53588 Views
Node-Red Automation, MQTT, NodeMCU & MicroPython
52067 Views
LidarBot
Snaszy NAS a 3D printed NAS for Raspberry Pi
Waveshare CM5 boards
The Best Arduino Robot for Beginners
SMARS Lab upgrade with PyCharm
Chicken Nugget Piano
Mini-Rack 3D Design Tutorial
0h 20m
Using the Raspberry Pi Pico's Built-in Temperature Sensor
0h 24m
Getting Started with SQL
0h 32m
Introduction to the Linux Command Line on Raspberry Pi OS
0h 42m
How to install MicroPython
0h 8m
Wall Drawing Robot Tutorial
0h 22m
Learn Linux from the basics to advanced topics.
Learn how to use a Raspberry Pi Pico
Learn MicroPython the best language for MicroControllers
Learn Docker, the leading containerization platform. Docker is used to build, ship, and run applications in a consistent and reliable manner, making it a popular choice for DevOps and cloud-native development.
Learn how to build SMARS robots, starting with the 3D Printing the model, Designing SMARS and Programming SMARS
Learn how to build robots, starting with the basics, then move on to learning Python and MicroPython for microcontrollers, finally learn how to make things with Fusion 360.
Learn Python, the most popular programming language in the world. Python is used in many different areas, including Web Development, Data Science, Machine Learning, Robotics and more.
Learn how to create robots in 3D, using Fusion 360 and FreeCAD. The models can be printed out using a 3d printer and then assembled into a physical robot.
Learn how to create Databases in Python, with SQLite3 and Redis.
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 >