Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Joining More Than Two Tables | Advanced Join Techniques and Multi-Table Queries
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
SQL Joins in Depth

bookJoining More Than Two Tables

When you work with databases in real-world scenarios, you often need to gather information that spans more than just two tables. Multi-table joins allow you to combine data from three or more related tables in a single query, which is essential for generating comprehensive reports and insights. For instance, you might want to see which employees are assigned to which projects and also include details about the departments responsible for those projects. This approach is common in business reporting, such as analyzing employee involvement across departments or tracking project assignments company-wide.

1234567
SELECT employees.name AS employee_name, projects.project_name FROM employees INNER JOIN assignments ON employees.employee_id = assignments.employee_id INNER JOIN projects ON assignments.project_id = projects.project_id;
copy

This query demonstrates how to join three tables—employees, assignments, and projects—to show which employees are assigned to which projects. The logic begins by joining employees to assignments using the employee_id column, which links each employee to their assignments. Next, it joins assignments to projects using the project_id, connecting each assignment to its corresponding project. By chaining these INNER JOIN operations, you can traverse relationships across multiple tables and retrieve the combined data you need.

When you want to include even more context, such as the department each project belongs to, you can extend the join to a fourth table. The following query brings together employees, assignments, projects, and departments to display employee names, project names, and department names in a single result.

123456789
SELECT employees.name AS employee_name, projects.project_name, departments.department_name FROM employees INNER JOIN assignments ON employees.employee_id = assignments.employee_id INNER JOIN projects ON assignments.project_id = projects.project_id INNER JOIN departments ON projects.department_id = departments.department_id;
copy

1. What is required to join more than two tables in a single SQL query?

2. How do you avoid ambiguity when joining multiple tables with columns of the same name?

3. Fill in the blanks to complete a query that joins employees, assignments, and projects to show each employee's name and the project they are assigned to.

question mark

What is required to join more than two tables in a single SQL query?

Select the correct answer

question mark

How do you avoid ambiguity when joining multiple tables with columns of the same name?

Select the correct answer

question-icon

Fill in the blanks to complete a query that joins employees, assignments, and projects to show each employee's name and the project they are assigned to.

SELECT employees.name, projects.project_name FROM employees assignments ON employees.employee_id = assignments.employee_id projects ON assignments.project_id = projects.project_id;
Alice Johnson | Website Redesign
Bob Smith | Website Redesign
Bob Smith | Mobile App
Carol White | Product Launch
David Brown | Website Redesign
Eve Black | Mobile App
Frank Green | Product Launch
Grace Lee | Product Launch
Hannah Adams | Recruitment Drive

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

Suggested prompts:

Can you explain how INNER JOIN works in these queries?

What would happen if I used LEFT JOIN instead?

How can I filter the results to show only employees from a specific department?

bookJoining More Than Two Tables

Свайпніть щоб показати меню

When you work with databases in real-world scenarios, you often need to gather information that spans more than just two tables. Multi-table joins allow you to combine data from three or more related tables in a single query, which is essential for generating comprehensive reports and insights. For instance, you might want to see which employees are assigned to which projects and also include details about the departments responsible for those projects. This approach is common in business reporting, such as analyzing employee involvement across departments or tracking project assignments company-wide.

1234567
SELECT employees.name AS employee_name, projects.project_name FROM employees INNER JOIN assignments ON employees.employee_id = assignments.employee_id INNER JOIN projects ON assignments.project_id = projects.project_id;
copy

This query demonstrates how to join three tables—employees, assignments, and projects—to show which employees are assigned to which projects. The logic begins by joining employees to assignments using the employee_id column, which links each employee to their assignments. Next, it joins assignments to projects using the project_id, connecting each assignment to its corresponding project. By chaining these INNER JOIN operations, you can traverse relationships across multiple tables and retrieve the combined data you need.

When you want to include even more context, such as the department each project belongs to, you can extend the join to a fourth table. The following query brings together employees, assignments, projects, and departments to display employee names, project names, and department names in a single result.

123456789
SELECT employees.name AS employee_name, projects.project_name, departments.department_name FROM employees INNER JOIN assignments ON employees.employee_id = assignments.employee_id INNER JOIN projects ON assignments.project_id = projects.project_id INNER JOIN departments ON projects.department_id = departments.department_id;
copy

1. What is required to join more than two tables in a single SQL query?

2. How do you avoid ambiguity when joining multiple tables with columns of the same name?

3. Fill in the blanks to complete a query that joins employees, assignments, and projects to show each employee's name and the project they are assigned to.

question mark

What is required to join more than two tables in a single SQL query?

Select the correct answer

question mark

How do you avoid ambiguity when joining multiple tables with columns of the same name?

Select the correct answer

question-icon

Fill in the blanks to complete a query that joins employees, assignments, and projects to show each employee's name and the project they are assigned to.

SELECT employees.name, projects.project_name FROM employees assignments ON employees.employee_id = assignments.employee_id projects ON assignments.project_id = projects.project_id;
Alice Johnson | Website Redesign
Bob Smith | Website Redesign
Bob Smith | Mobile App
Carol White | Product Launch
David Brown | Website Redesign
Eve Black | Mobile App
Frank Green | Product Launch
Grace Lee | Product Launch
Hannah Adams | Recruitment Drive

Натисніть або перетягніть елементи та заповніть пропуски

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 2. Розділ 1
some-alt