0

I am learning PostgreSQL and was practicing questions.

I am stuck at this question here

My Solution does not seem to work and I am not sure why:

SELECT first_name, last_name, department_id, department_name 
FROM employees 
JOIN departments ON departments.department_id = employees.department_id;

Their solution:

SELECT first_name, last_name, department_id, department_name 
FROM employees 
JOIN departments USING (department_id);

I'd really appreciate the help.

1
  • What is your question? You have a solution that works in the question. Commented May 18, 2021 at 1:54

1 Answer 1

2

Your solution does not work because the field names are ambiguous. Try prefixing like this

SELECT 
  employees.first_name, 
  employees.last_name, 
  departments.department_id, 
  departments.department_name 
FROM employees 
JOIN departments ON departments.department_id = employees.department_id;

The problem does not appear when you use USING, because then there will be only a single department_id column in the join result.

Sign up to request clarification or add additional context in comments.

5 Comments

you might also have to specify the join type. e.g., "INNER JOIN" or "LEFT JOIN" instead of just JOIN.
Thanks alot. Since the website does not show the error, I could not figure it out. One Question , isn't JOIN by default mean INNER JOIN ?
JOIN might well be INNER JOIN in Postgres. That's allowed in some SQL variants for sure; I'm just never sure which.
@Aadmaa . . . JOIN means INNER JOIN. The INNER is totally redundant.
Thx @GordonLinoff - I didn't realize it was the actual SQL standard. But I confirmed here: contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt "If a <qualified join> is specified and a <join type> is not specified, then INNER is implicit."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.