The Talking Walnut The Talking Walnut - 1 year ago 74
SQL Question

SQL Query to get records of parent table that have a list of child records

I have two tables in an MS SQL Server 2005 database, parent and child, where the parent may be related to many child records. [Child.parent_id] is related to []. The child table also has column [foo] I need to bring back all records in the parent table where [] matches on each of one to many parameters. For example I would like all parent records that have a [] value of 'fizz' and a [] value of 'buzz.'
I have tried the below query, but it is returning records that match on only one.

FROM Parent
INNER JOIN Child ON Parent.ID = Child.parent_id
WHERE ( = 'fizz')
SELECT Parent_1.ID
FROM Parent AS Parent_1
INNER JOIN Child AS Child_1 ON Parent_1.ID = Child_1.parent_id
WHERE ( = 'buzz')

mjv mjv
Answer Source

This will return all Parent records which have [at least] one child with a 'fizz' foo AND [at least] one child with 'buzz' foo. Which is what I think is required in the question.

Also, while being potentially sub-optimal, this query is generic in a sense that it will work with most SQL implementation, not only the more modern ones, where the support of CTE, subqueries and related constructs are supported.

    FROM Parent
    JOIN Child C1 ON Parent.ID = C1.parent_Id
    JOIN Child C2 ON Parent.ID = C2.parent_id
    WHERE = 'fizz'
      AND = 'buzz'

Now that Joel Potter has fixed the query in his answer, we probably agree that his approach has several advantages over the query listed above (please give him a few +reps). In particular:

  • the structure of the query doesn't change when we add, or remove, targeted values for the column foo.
  • the query is probably more easily optimized [by the server itself]
  • the structure of the query allows it to handle variations on the definition of the filter. For example we can query all Parents which have children such that say 2 of 5 possible values of foo are present.

Following is Joel's query here, slightly modified, to show how it can be expanded for more than 2 foo values.

SELECT Parent.Id
FROM Parent
INNER JOIN Child on Parent.Id = child.parent_id
WHERE IN ('fizz', 'buzz')  -- or say, ... IN ('fizz', 'buzz', 'bang', 'dong')
GROUP BY Parent.Id
HAVING COUNT(DISTINCT = 2  -- or 4 ...