I have 2 SQL server tables lets say called child and parent with one parent to many children. I have a field in parent called ChildrenCount and would like a query that will join and returns results only if the number of records in the child table matches ChildrenCount in the parent table. Is this possible in SQL or will this require a stored procedure?
>Solution :
You could use a join combined with aggregation:
SELECT p.id
FROM parent p
INNER JOIN child c
ON c.parent_id = p.id
GROUP BY p.id, p.ChildrenCount
HAVING p.ChildrenCount = COUNT(*);