Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two tables

Department
<img src="http://www9.0zz0.com/2015/02/14/00/463246826.png"/>

Instructor

<img src="http://www9.0zz0.com/2015/02/14/00/386938495.png"/>

i want to return department and it's manager

some departments have not managers so manager name should be null

but the result show cartisan product

result

SQL
select  dpt.Dept_Id , dpt.Dept_Name,dpt.Dept_Location, inst.Ins_Name as [Dept_Manager] ,dpt.Dept_Desc
 from  Department dpt  join Instructor as inst
     on (dpt.Dept_Id=3) AND ((dpt.Dept_Manager = inst.Ins_Id) OR (dpt.Dept_Manager is null))



<img src="http://www9.0zz0.com/2015/02/14/00/216699126.png"/>

result should be

Dept_Id Dept_Name Dept_Location Dept_Manager Dept_Desc
3 tes ma NULL NULL

so what is wrong with my code
Posted
Comments
Zoltán Zörgő 13-Feb-15 16:19pm    
- why are you referencing in two directios?
- you need LEFT JOIN

1 solution

Just do a LEFT JOIN as mentioned in the comments.

SQL
SELECT *
FROM Department d
LEFT JOIN Instructor i ON d.dept_manager = i.ins_id
WHERE d.dept_id = 3
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900