I have three tables with a many to many relationships with tbl_emp_dept as follows:
Code:
tbl_employee
emp_id INT
emp_name VARCHAR
tbl_emp_dept
emp_id INT
dept_id INT
tbl_department
dept_id INT
dept_name VARCHAR
An employee can be assigned to multiple departments or no departments if they are new.
I want to a list of all employees and the departments they are assigned to. The query should list all employees even if they are not assigned to any departments.
The query should also list all departments even if no employee have been assigned to that department.
I need the results to look something like below:
Code:
emp_name | dept_name | assigned_to_dept
--------------------------------------
emp_xyz | dept_1 | Y
emp_xyz | dept_2 | Y
emp_xyz | dept_3 | null
emp_abc | dept_1 | Y
emp_abc | dept_2 | null
emp_abc | dept_3 | null
Any help appreciated.