31

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.

Fenton
  • 241,084
  • 71
  • 387
  • 401
Natalia Natalie
  • 647
  • 3
  • 10
  • 17

7 Answers7

45

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.

Nicolas Gervais
  • 33,817
  • 13
  • 115
  • 143
OCDan
  • 1,103
  • 1
  • 10
  • 19
  • Minute correction for MS SQL... `Select e.EmpName, e.Address, s.Name, S.Address, s.Project From tbEmployees e JOIN tbSupervisor s on e.id = s.SupervisorID` – Manoj Kumar Aug 20 '16 at 14:47
16

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
Fenton
  • 241,084
  • 71
  • 387
  • 401
6

You can use the table name as part of the column specification:

SELECT tbEmployees.EmpName, tbEmployeesAddress, tbSupervisor.Name,
       tbSupervisor.Address, tbSupervisor.project

FROM tbEmployees

JOIN tbSupervisor

ON tbEmployees.ID = tbSupervisor.SupervisorID
5


    SELECT employees.EmpName, employees.Address AS employeer address, 
           supervisor.Name, supervisor.Address AS supervisor address,supervisor.project 
    FROM tbEmployees 
       AS employees 
    JOIN tbSupervisor 
       AS supervisor 
    ON 
       employees.ID = supervisor.SupervisorID



2
SELECT product_report.*, 
       product.pgroup 
FROM   `product_report` 
       INNER JOIN product 
               ON product_report.product_id = product.id 
WHERE  product.pgroup = '5' 
ORDER  BY product.id DESC 
4b0
  • 21,981
  • 30
  • 95
  • 142
Md Yeasin Arafat
  • 2,683
  • 1
  • 10
  • 5
1

You need to learn about aliases. They will make your queries more maintainable. Also, you should always use aliases when referencing columns, so your query is clear about what it is doing:

SELECT e.EmpName, e.Address, s.name, s.address as SupervisorAddress
FROM tbEmployees e  JOIN
     tbSupervisor s
     ON e.ID = s.SupervisorID;

Note that I also renamed the second address so its name is unique.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

Specify the table name and field name in your selection

SELECT tbEmployees.EmpName,
       tbEmployees.Address,
       tbSupervisor.[column name]
  FROM tbEmployees
  JOIN tbSupervisor ON tbEmployees.ID = tbSupervisor.SupervisorID
Sudipta Mondal
  • 2,550
  • 1
  • 19
  • 20
Riv
  • 1,849
  • 1
  • 16
  • 16