Friday, 23 December 2011

Max Function in Independent subquery.To find the country with highest DensityPerKmSq

In the below queries, 'a' and 'b' are alias names. Here the inner Query is independent of the outer query. First the inner query is executed and then the outer query is executed. This is known as independent Subquery.

























Here in the second query, 'where b.DensityPerKmSq is not null' is included because null is an unknown value and hence '>=ALL' (select b.DensityPerKmSq from countrytable b) means '>=ALL' (ALL the Known values of b.DensityPerKmSq and also the unknown values).Hence the query will return zero rows if 'where b.DensityPerKmSq is not null' is not included.

Further more, '>=ALL' is included because '>=' alone can handle only single value. But the query,
'(select b.DensityPerKmSq from countrytable b where b.DensityPerKmSq is not null)' returns multiple values.To handle multiple values, '>=ALL' is included.

Further more, Viewers may think it would be better if the countries with maximum DensityPerKmSq for each continent is displayed. For that, Please refer the post on "Max Function in Corelated subquery. To find the country with highest DensityPerKmSq in each continent."

No comments:

Post a Comment