Data Science

How do I write a SQL query to list all departments and their managers, including those without one?

GR Asked by Gregory Lawson · 14-11-2025
0 upvotes 14,532 views 0 comments
The question

I am working with a standard relational schema containing an Employees table and a Departments table. I need to generate a report that displays every department name alongside its manager's name. However, some of our newer departments haven't had a manager assigned yet. When I use a standard join, these departments disappear from my results. What is the correct JOIN syntax to ensure all departments are listed, with a NULL or 'No Manager' placeholder where applicable?

3 answers

0
KI
Answered on 16-11-2025

To solve this, you must use a LEFT OUTER JOIN (or simply LEFT JOIN). A standard INNER JOIN only returns rows where there is a match in both tables, which is why your manager-less departments are being filtered out. By starting with the Departments table on the "left," you tell SQL to keep all records from that table regardless of a match. You can also use the COALESCE function to replace the resulting NULL values with a more readable string like 'No Manager assigned.' For example: SELECT d.DeptName, COALESCE(e.EmpName, 'No Manager') FROM Departments d LEFT JOIN Employees e ON d.ManagerID = e.EmployeeID.

0
JE
Answered on 19-11-2025

Does your database schema use a self-referencing foreign key within the Employees table to denote managers, or is the relationship defined strictly through a ManagerID column located in the Departments table? This structure changes how you would write the join, as you might need to join the Employees table to itself if you are trying to pull additional manager details like their specific department or contact info.

RO 21-11-2025

Jeffrey, in this case, the ManagerID is specifically a column in the Departments table pointing to the EmployeeID. This makes the query slightly simpler since we don't need a self-join. However, if we wanted to see the manager's own department alongside the department they manage, your point about the self-referencing key would be critical to avoid confusion between the two different department contexts in the result set.

0
CY
Answered on 23-11-2025

The LEFT JOIN is definitely the way to go. It ensures the "parent" table (Departments) stays fully visible even if the "child" table (Employees) lacks a matching manager ID.

GR 25-11-2025

I agree with Cynthia. It’s a very common mistake for beginners to use INNER JOIN and wonder where their data went. Always think about which table is your "source of truth" for the list you want to see.

Share your thoughts

Your email address will not be published. Required fields are marked (*)

Professional Counselling Session

Still have questions?
Schedule a free counselling session

Our experts are ready to help you with any questions about courses, admissions, or career paths. Get personalized guidance from industry professionals.

Request a Call Back

Search Online

We Accept

We Accept

Follow Us

"PMI®", "PMBOK®", "PMP®", "CAPM®" and "PMI-ACP®" are registered marks of the Project Management Institute, Inc. | "CSM", "CST" are Registered Trade Marks of The Scrum Alliance, USA. | COBIT® is a trademark of ISACA® registered in the United States and other countries.

Book Free Session