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

Posted on 2011-04-24
Last Modified: 2012-05-11
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.
Question by:johanntagle
    LVL 28

    Accepted Solution

    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?
    LVL 24

    Author Closing Comment

    Clear enough!  Thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now