Question

Advance multiple join query table

Hi i have relations database design like this design database relationship at dynobird.com

How can i find the names of all employees who are working on projects for ‘Client A’, including the names of those projects and the tasks associated with them.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
May 6, 2024

Hey!

I believe that the following should work for you:

SELECT 
    Employees.EmployeeName,
    Projects.ProjectName,
    Tasks.TaskDescription
FROM 
    Employees
JOIN 
    Departments ON Employees.DepartmentID = Departments.DepartmentID
JOIN 
    Projects ON Departments.DepartmentID = Projects.DepartmentID
JOIN 
    Clients ON Projects.ProjectID = Clients.ProjectID
JOIN 
    Tasks ON Projects.ProjectID = Tasks.ProjectID
WHERE 
    Clients.ClientName = 'Client A';

If you need additional adjustments or modifications to the query, please feel free to ask!

For anyone interested in learning more about SQL in general, I could suggest this free eBook:

💡 Introduction to SQL eBook

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
May 5, 2024

Heya @diko23,

To solve the problem of finding the names of all employees working on projects for “Client A”, including the names of those projects and the tasks associated with them, you will need to perform multiple joins across your tables. Since there’s no direct relation between employees and clients, you will join through the Projects and Departments tables.

Here’s how you can construct this query in SQL, assuming you’re using MySQL or PostgreSQL, which both follow similar SQL syntax for this kind of operation:

SELECT
    e.EmployeName,
    p.ProjectName,
    t.TaskDescription
FROM
    Clients c
JOIN
    Projects p ON c.ProjectID = p.ProjectID
JOIN
    Tasks t ON p.ProjectID = t.ProjectID
JOIN
    Departments d ON p.DepartmentID = d.DepartmentID
JOIN
    Employes e ON d.DepartmentID = e.DepartmentID
WHERE
    c.ClientName = 'Client A';

Explanation of the SQL Query:

  1. FROM Clients c: Start from the Clients table, which contains information about which client is associated with which project.

  2. JOIN Projects p ON c.ProjectID = p.ProjectID: Join the Clients table to the Projects table to get details about the projects that belong to the client.

  3. JOIN Tasks t ON p.ProjectID = t.ProjectID: Join the Projects table to the Tasks table to get information about the tasks associated with each project.

  4. JOIN Departments d ON p.DepartmentID = d.DepartmentID: Since employees are linked to projects via departments, join the Projects table to the Departments table.

  5. JOIN Employes e ON d.DepartmentID = e.DepartmentID: Finally, join the Departments table to the Employees table to list all employees who are in the department related to the project.

  6. WHERE c.ClientName = ‘Client A’: Filter the results to include only those entries that are related to ‘Client A’.

This query will give you the names of the employees (EmployeName), the names of the projects (ProjectName), and descriptions of the tasks (TaskDescription) that are associated with “Client A”.

Additional Notes:

  • Make sure that all column names match exactly what’s in your database, as SQL is case-sensitive in some setups.
  • This query assumes that every project is assigned to exactly one department, and departments are not shared between different projects. If the business logic differs (e.g., projects can span multiple departments), the SQL might need adjusting.
  • The query also assumes that all projects for a client will have tasks and that there are employees in every department.

This approach gives a comprehensive look at the teams working for “Client A” from a project and task perspective. If you have any more specific requirements or encounter any issues, feel free to provide more details!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel