We read your photo as:

How to fix the SQL error "Column 'EmpID' in field list is ambiguous" when joining Employee and Salary tables?

Computer Science

ScanSolve AI Answer

The error ERROR 1052 (23000): Column 'EmpID' in field list is ambiguous occurs because the EmpID column exists in both the Employee and Salary tables, and you haven't specified which table to select it from. The same applies to BasicSalary and DeptID.

To fix this, you need to use table aliases to clarify which table each column belongs to.

Here is the corrected query:

SELECT
    E.EmpID,
    E.FirstName,
    E.LastName,
    (S.BasicSalary - (S.BasicSalary * 0.12)) AS NetSalary
FROM
    Employee AS E
JOIN
    Salary AS S ON E.EmpID = S.EmpID
WHERE
    E.DeptID = 2;
  • Employee AS E: Assigns the alias E to the Employee table.
  • Salary AS S: Assigns the alias S to the Salary table.
  • E.EmpID, E.FirstName, E.LastName, E.DeptID: Specifies that these columns come from the Employee table.
  • S.BasicSalary: Specifies that this column comes from the Salary table.
  • (S.BasicSalary - (S.BasicSalary * 0.12)) AS NetSalary: Calculates the net salary by subtracting 12% tax from the BasicSalary and aliases the result as NetSalary.
Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question