2

I have a homework assignment that I can't seem to figure out.

I don't want the entire query as an answer, that's why I won't include any table structures or SQL. I just need some information on how to tackle this issue in SQL. Any help will be appreciated!

Given the following result table, how can I check (per row) if the Location_ID value of ID is the same as the Location value of ID's Substitute? The query should give me a new table for every case where the locations don't match.

Example result table

E.G. ID 1 has a location of 45 and 5 as Substitute. ID 5 has a location of 67, so they should be listed in the result table of the query.

ID 3 has a location of 34 and 2 as a substitute. ID 2 also has a location of 34, so ID 3 should be left out of the result table.

2 Answers 2

1

the Idea is to use a self join, something like:

select * from Mytable as a join Mytable as b on a.ID=b.ID
where a.Substitute!=b.Location_ID
Sign up to request clarification or add additional context in comments.

Comments

1

Thanks, I needed the self join in this case! I had to change the query a bit though, to check against both the ID's locations.

    select * from Mytable as a join Mytable as b on a.ID=b.ID 
    where a.Location_ID!=b.Location_ID

Much appreciated!

Comments

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.