Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have 2 tables department(with columns depID and Departmentname)and employee(with columns EmpID,Emp_name,DepID1,DepID2).
I resultant table as
EmpID,Emp_name,Departmentname,Departmentname
i need the query which gives me above resultant table.
Explain my query
what i want is a result which is similar to employee table but the dep_id1, dep_id2 should replaced wit respective departmentname from department table.
Suppose an employee belongs to 2 departments then
i need the result as
empid,empname,departmentname, departmentname
Posted

1 solution

This is the most basic SQL you could ever write. It's called an inner join, where you join two tables together by using a value common to both, in this case, the depID. As you have two tables, you need to join twice, as in

SQL
select E.EmpID, E.Emp_Name, d1.DepartmentName, d2.DepartmentName from Employee e inner join department d1 on e.DepID1 = d1.DepID inner join department d2 on e.depID2 = d2.DepID



You should buy a basic SQL book and read it.
 
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