Skip to content
Advertisement

Joining two tables with specific columns

I am new to SQL, I know this is really basic but I really do not know how to do it! I am joining two tables, each tables lets say has 5 columns, joining them will give me 10 columns in total which I really do not want. What I want is to select specific columns from both of the tables so that they only show after the join. (I want to reduce my joining result to specific columns only)

SELECT * FROM tbEmployees

JOIN tbSupervisor

ON tbEmployees.ID = tbSupervisor.SupervisorID

The syntax above will give me all columns which I don’t want. I just want EmpName, Address from the tblEmployees table and Name, Address, project from the tbSupervisor table

I know this step:

SELECT EmpName, Address FROM tbEmployees

JOIN tbSupervisor

ON tbEmployees.ID = tbSupervisor.SupervisorID

but I am not sure about the supervisor table.

I am using SQL Server.

Advertisement

Answer

This is what you need:

Select e.EmpName, e.Address, s.Name, S.Address, s.Project
From tbEmployees e
JOIN tbSupervisor s on e.id = SupervisorID

You can read about this on W3Schools for more info.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement