How to explain a not very complex query to an SQL beginner

Consider the following:

(item_id int(11),
 category_id int(11)

(category1_id int(11),
  category2_id int(11),
  relation varchar(20)

Then we have the query that uses item1_id and item2_id as inputs:

select cr.relation
from category_relations cr, item_categories ic1, item_categories ic2
where ic1.item_id=item1_id
and ic2.item_id=item2_id
and cr.category1_id=ic1.category_id
and cr.category2_id=ic2.category_id;

How would you best explain the need for calling item_categories twice to an SQL beginner?  Things like this has become pretty second-nature to me, but having to explain it in simple terms suddenly got me stumped.  Thanks.
LVL 24
Who is Participating?
Ryan McCauleyData and Analytics ManagerCommented:
It's not really clear from your query why you're linking to that table twice, unless you're just trying to validate the integrity of both sides of the relationship in the category_relations table.

If that's the case, then I'd just explain it like that - you have a row in the table representing a relationship, but you don't know what two categories its between (since you only have IDs) unless you link back to the item_categories table to get more information. Since you have two different relationships, you need to link to the categories table two different times - one for each side of the relationship.

Is that clear, or am I missing the point of what you're trying to explain?
johanntagleAuthor Commented:
Clear enough!  Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.