SELECT score FROM t WHERE id in (2, 4) HAVING COUNT(*) = 2 /* replace this with the number of IDs */
This will select the rows with ID 2 and 4. TheHAVINGclause then ensures that we find both rows; if one of them is missing, the count will be less than 2.
This is an example of a "collection within a collection" query. I recommend using thehavingclause for aggregation as it is the most flexible method.
select score from t group by score having sum(id = 2) > 0 and -- has id = 2 sum(id = 4) > 0 -- has id = 4
What this does is aggregate by score. Then the first part of thehavingclause (sum(id = 2)) counts how many "2"s there are in each fraction. The second one is the number of "4". Only scores of "2" and "4" are returned.
This will select the rows with ID 2 and 4. The
HAVING
clause then ensures that we find both rows; if one of them is missing, the count will be less than 2.This assumes
id
is the only column.This is an example of a "collection within a collection" query. I recommend using the
having
clause for aggregation as it is the most flexible method.What this does is aggregate by score. Then the first part of the
having
clause (sum(id = 2)
) counts how many "2"s there are in each fraction. The second one is the number of "4". Only scores of "2" and "4" are returned.