How do you retrieve data from multiple tables using JOINs?
Posted by MaryJns
Last Updated: July 25, 2024
To retrieve data from multiple tables using JOINs in SQL, you combine rows from two or more tables based on a related column between them. There are several types of JOIN operations you can use, such as INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL JOIN. Here’s a brief overview of each, followed by examples.
Types of JOINs:
1. INNER JOIN: Returns records that have matching values in both tables. 2. LEFT JOIN (or LEFT OUTER JOIN): Returns all records from the left table, and the matched records from the right table. If there is no match, NULL values are returned for columns from the right table. 3. RIGHT JOIN (or RIGHT OUTER JOIN): Returns all records from the right table, and the matched records from the left table. If there is no match, NULL values are returned for columns from the left table. 4. FULL JOIN (or FULL OUTER JOIN): Returns all records when there is a match in either left or right table records. If there is no match, the result is NULL on the side that does not have a match.
Basic Syntax
Here’s how the basic syntax looks for a JOIN statement:
SELECT columns
FROM table1
JOIN table2
ON table1.common_column = table2.common_column;
Examples
Suppose you have two tables: employees and departments. - employees Table:
| id | name   | department_id |
  |----|--------|----------------|
  | 1  | Alice  | 1              |
  | 2  | Bob    | 2              |
  | 3  | Charlie| 1              |
- departments Table:
| id | department_name |
  |----|------------------|
  | 1  | HR               |
  | 2  | Engineering      |
Example 1: INNER JOIN
To retrieve a list of employees along with their department names:
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
Result:
| name   | department_name |
|--------|------------------|
| Alice  | HR               |
| Bob    | Engineering      |
| Charlie| HR               |
Example 2: LEFT JOIN
To get all employees and their department names, but include employees even if they don't belong to a department:
SELECT employees.name, departments.department_name
FROM employees
LEFT JOIN departments ON employees.department_id = departments.id;
Result: Assuming you have employees who may not be assigned to departments:
| name   | department_name |
|--------|------------------|
| Alice  | HR               |
| Bob    | Engineering      |
| Charlie| HR               |
| David  | NULL             |
Example 3: RIGHT JOIN
To retrieve all departments and the employees in those departments (including departments with no employees):
SELECT employees.name, departments.department_name
FROM employees
RIGHT JOIN departments ON employees.department_id = departments.id;
Result:
| name   | department_name |
|--------|------------------|
| Alice  | HR               |
| Charlie| HR               |
| Bob    | Engineering      |
| NULL   | Engineering      |
Example 4: FULL JOIN
To get a complete list of employees and departments, regardless of matches:
SELECT employees.name, departments.department_name
FROM employees
FULL JOIN departments ON employees.department_id = departments.id;
Conclusion
Using JOINs allows you to retrieve the related data you need from multiple tables in a relational database. Just ensure that the columns you're joining on have corresponding data types and meaningful relationships.