Skip to content
Advertisement

In a one to many relationship, is it possible to do a query for parent based on if they have certain children without using count and group by?

Say my the schema for these two tables is as follows:

Now, say the the records in these possession and owner tables are as follows:

In non-sql language: What is the query that could search for owners that have a truck and a hammer? And in sql language: is there a solution for this that does not use count + group by?

Advertisement

Answer

I like to do this with group by and having:

If owner/possession is unique, then use count(*) instead of count(distinct).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement