I am confused, how is sql allowing me to select multiple columns with same name.
as of my knowledge, it should show column ambiguous. How is it allowing like this, and what is the use of it ???
I am confused, how is sql allowing me to select multiple columns with same name.
as of my knowledge, it should show column ambiguous. How is it allowing like this, and what is the use of it ???
In your first query, you are actually selecting the same column 3 times. Nothing wrong with that and no ambiguity for the query engine.
In the second query, you are using the table alias prefix to select EmpName from E1 twice, and EmpName from E2 once (which is the same table - nothing wrong with that either, and no ambiguity for the query engine).
If you want to avoid ambiguity in the output, make sure to alias the columns you select:
SELECT E1.EmpName AS [Employee Name], E2.EmpName AS [ManagerName]
FROM tblEmp E1 INNER JOIN tblEmp E2 ON E2.EmpId = E1.MgrId
That is the way it works. sql lets you select columns of the same name. but it is going to show column ambiguous when you include it in a where condition. to avoid that use alias