Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining two tables with specific columns

Tags:

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.

like image 465
Natalia Natalie Avatar asked Jul 02 '13 20:07

Natalia Natalie


2 Answers

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.

like image 163
OCDan Avatar answered Oct 31 '22 14:10

OCDan


You can get columns from specific tables, either by their full name or using an alias:

SELECT E.EmpName, E.Address, S.Name, S.Address, S.Project FROM tbEmployees E INNER JOIN tbSupervisor S ON E.ID = S.SupervisorID 
like image 35
Fenton Avatar answered Oct 31 '22 13:10

Fenton