universal- and existential quantification in SQL -
given relation
x | y ------- | | ii b | ii b | ii how query for
- the set of
xthere existsyof length 2 (should yield{ a, b }) - the set of
xyhave length 2 (should yield{ b })
the first 1 easy (note, in examples length_function stands in product-specific length-of-string function in whatever sql database you're using):
select distinct x relationname length_function(y) = 2; for second one, there variety of ways approach problem.
select x relationname group x having min(length_function(y)) = 2 , max(length_function(y)) = 2 will aggregate x values , filter length of 2 while
select distinct x relationname length_function(y) = 2 , x not in (select distinct x relationname length_function(y) <> 2) uses same filter first query, additionally filters out x values exist elsewhere in table non-length-2 y value. finally
select distinct x relationname rn1 length_function(y) = 2 , not exists (select * relationname x = rn1.x , length_function(y) <> 2) does same filtering in way may find more expressive (but less performant)
Comments
Post a Comment