Question

I need help with this exercise where I have to translate properties from English into sql.

Questions:

  1. length has at least one 0

    Answer: MIN(ABS(length)) = 0
    
  2. length has more that one value

    Answer: MIN(length) < MAX(length)
    
  3. there are no nulls for length

    Answer: COUNT(*) = COUNT(length)
    
  4. values of length are either positive or negative

    Answer: MIN(length) * MAX(length) > 0
    
  5. length differs above and below zero by the same amount

    Answer: MIN(length) = -max(length)
    
  6. all values of length are different

    Answer: COUNT(DISTINCT length) = COUNT(length)
    
  7. only one of min(length) or max(length) is positive

    Answer: MIN(length) * MAX(length) < 0 
    
  8. length has one value or null value

    Answer: MIN(length) = MAX(length)
    

Are these correct? I'm not sure about #3, 5, 6 and 8 in particular.

Was it helpful?

Solution

  1. length has at least one 0

    Answer: MIN(ABS(length)) = 0
    
  2. length has more that one value

    Answer: MIN(length) < MAX(length)
    or
    Answer: COUNT(distinct length) > 1
    
  3. there are no nulls for length

    Answer: COUNT(*) = COUNT(length)
    
  4. values of length are either positive or negative

    Answer: MIN(length) * MAX(length) > 0
    or
    Better Answer: Sign(MIN(length)) * Sign(MAX(length)) > 0
    
    -- Because, with large values of length, multiplying two of them
    -- together can give you an overflow.
    
  5. length differs above and below zero by the same amount

    Answer: MIN(length) = -max(length)
    
  6. all values of length are different

    Answer: COUNT(DISTINCT length) = COUNT(length)
    
  7. only one of min(length) or max(length) is positive

    Maybe Wrong Answer: MIN(length) * MAX(length) < 0 (0 is not positive)
    or
    Answer: MIN(length) <= 0 and MAX(length) > 0
    
    -- IF: one must be positive and the other must be negative
    -- change <= 0 to < 0
    
  8. length has one value or null value

    Wrong Answer: MIN(length) = MAX(length) -- null=null does not return true
    or
    Answer: COUNT(distinct length) <= 1
    

OTHER TIPS

I would question #2. You may have two identical length values.

For #3, I would not use the HAVING clause to check for non-nulls. Same thing for #4. HAVING is not the right place to be checking equivalency for each row against a scalar value.

#5 might have some problems worth wording. Are you certain they mean min and max and not distribution?

#8 is oddly worded as well. Null could be the value of length, or it could be absence of value. It depends on how the query is formated. Also, is there a requirement that you not use operators like AND & OR?

Also, NULL is treated as "unknown" with most database platforms. Since it is unknown, it cannot be checked for equivalency against another value. If you don't know what Value1 is, you can't possibly know if it's the same as what's in Value2...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top