In SQL, how can I select all parents that have children?
Let's say I have two related tables parents and children with a
one-to-many relationship (one parent to many children). Normally when I
need to process the information on these tables together, I do a query
such as the following (usually with a WHERE clause added in):
SELECT * FROM parents INNER JOIN children ON (parents.id =
children.parent_id);
How can I select all parents that have at least one child without wasting
time joining all of the children to their parents?
I was thinking of using some sort of OUTER JOIN but I am not sure exactly
what to do with it.
(Note that I am asking this question generally, so don't give me an answer
that is tied to a specific RDBMS implementation unless there is no general
solution.)
No comments:
Post a Comment