SQL Server I am trying to connect the two tables with the help of a join, but it is not working in my SQL [duplicate]

fslejnso  于 2023-08-02  发布在  其他
关注(0)|答案(1)|浏览(110)

This question already has answers here:

Ambiguous column name in SQL Server (3 answers)
Closed 20 days ago.

I am trying to connect to tables with the help of join but it is not working in SQL.

Please guide me to the mistake I am making in this query.

select first_Name, salary, dept
from employees
inner join department on employees.dept = department.dept

I get an error

Ambiguous column name 'dept'

but I don't know why...

I was trying to get the data from both the tables on the bases of dept.

1zmg4dgp

1zmg4dgp1#

You select dept , but the DBMS want to know whether you mean employees.dept or department.dept , despite them having the same value in your query.

It is good practise to qualify all columns with their table name when working with multiple tables:

select employees.first_Name, employees.salary, employees.dept
from employees
inner join department on employees.dept = department.dept;

(Which shows that the join to the department table is completely superfluous as yet.)

As the long table names can make queries harder to read, it is also good practice to use mnemonic table aliases:

select e.first_Name, e.salary, e.dept
from employees e
inner join department d on e.dept = d.dept;

相关问题