universal- and existential quantification in SQL -


given relation

x | y ------- | | ii b | ii b | ii 

how query for

  1. the set of x there exists y of length 2 (should yield { a, b })
  2. the set of x y have 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

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -