1

I'd like to get the following information:

prisoner_id, first_name, last_name, job_description for all prisoners, even if they don't have a job

From the tables:

Work:
Prisoner_id
job_id
start_date

Job:
job_id
job_description

Prisoner:
Prisoner_id
person_ssn

Person:
person_ssn
first_name
last_name

Here's my attempt at it, but I don't know how to get the job description.

select prisoner_id, first_name, last_name, job_description from
prisoner
left join work on prisoner_id
natural join person on person_ssn;

6 Answers 6

1

Assuming that a prisoner always has a record on the person table:-

SELECT prisoner.prisoner_id, 
    person.first_name, 
    person.last_name, 
    job.job_description 
FROM prisoner
INNER JOIN person on prisoner.person_ssn = person.person_ssn
LEFT OUTER JOIN work on prisoner.prisoner_id = work.prisoner_id
LEFT OUTER JOIN job ON work.job_id = job.job_id;

Avoiding using natural joins.

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

4 Comments

Thank you very much, this works great. Question, why avoid natural joins?
@FatalProphet, you can as well use natural joins (NJ is equivalent to inner join on the same column name).
What happens if someone adds a column to one table that has the same name as a column in another table that a query uses a natural join to join them together? For example say you had a table of parents and a table of children each containing a column for address id. Easy to find parents / children who live in the same house with a natural join. Them someone adds a column for (say) date of birth to each table. Suddenly the natural join is trying to join where there is a match on address and date of birth.
Or a better example. Say someone added a column to your prisoner table for when their sentence started, and decided to call that column start_date. A natural join between prisoner and work that would have worked fine joining on prisoner_id will now fail by trying to join on prisoner_id AND start_date.
1

Try this

select pr.Prisoner_id,
p.first_name,
p.last_name,
j.job_description
from prisoner pr
left join work w on pr.Prisoner_id = w.Prisoner_id
left join job j on w.job_id = j.job_id
inner join person p on pr.person_ssn = p.person_ssn

Comments

0
select prisoner_id, first_name, last_name, job_description from
prisoner left join work on work.prisoner_id = prisoner.prisoner_id    
natural join person on person.person_ssn = prisoner.person_ssn
natural join job on work.job_id =job.job_id

Comments

0

Try this,

select p.prisoner_id, per.first_name, per.last_name, j.job_description
from person per, prisoner p, work w left join job on w.job_id=job.job_id
where w.prisoner_id=p.prisoner_id and per.person_ssn=p.person_ssn
  1. Left join between work table and job table
  2. Inner join between work and prisoner
  3. Inner join between person and prisoner

Comments

0

Try this..

select prisoner_id, first_name, last_name, job_description from prisoner
join person on person.person_ssn = prisoner.person_ssn
left join work on work.prisoner_id = prisoner.prisoner_id
left join job on job.job_id = work.job_id

Comments

0

Try this:

SELECT w.prisoner_id, p.first_name, p.last_name, j.job_description FROM WORK w LEFT JOIN 
prisoner pr ON w.prisoner_id=pr.prisoner_id LEFT JOIN person p ON    
p.person_ssn=pr.person_ssn
LEFT JOIN Job j ON w.job_id=j.job_id

2 Comments

It's usually helpful if you provide some context or explanation along with your answer.
@PatrickQ thanks for your suggestion.In future ll improve my answers by providing the explanation...

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.