Skip to content
Advertisement

Select ALL instead of ANY in a Many-to-Many relationship

Currently we have the following sample DB structure with a Many-to-Many relationship between lead and tag tables.

Let’s imagine that we would like to perform the following queries.

  1. Give me all the leads that have ANY of the tags in a list.
  2. Give me all the leads that have ALL of the tags in the list

For the first one (1) we can do:

For the second one (2) where we want to search for Leads that have both tags. Which is the most performant option according to your opinion? Is this the best approach?

Advertisement

Answer

Provided unique (lead_id, tag_id) I would say

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