Search code examples
sqlsql-serverdatabaseazure-sql-database

How to join 3 tables without duplicate columns?


Query :

SELECT *
FROM dbo.employer_job
LEFT JOIN dbo.employer_user
  ON dbo.employer_job.employer_id = dbo.employer_user.employer_user_id
LEFT JOIN dbo.company_profile
  ON dbo.company_profile.company_id = dbo.employer_user.company_id

Duplicate column results :

enter image description here

dbo.employer_job schema :

enter image description here

dbo.employer_user schema :

enter image description here

dbo.comnpany_profile schema :

enter image description here

How do I remove the duplicate company_id column? My Python app won't accept duplicated columns from the database. Most suggest to use left join but that's not solving the issue.


Solution

  • Don't use *, but list the columns you want from each table (preferably with an alias).

    SELECT EJ.job_id, EJ.employer_id, .... 
    FROM dbo.employer_job EJ 
    ...
    

    It's verbose, but how else would the database engine know what you'd like to see?