Re: Daily SQL Quiz
Posted in 1993
In article <1993May5.085551.10388@sei.cmu.edu> marc@sei.cmu.edu (Marc Graham) writes:
>
>It may be that even writing SQL code is more fun than working. (I
>guess that only applies if your not being paid to write SQL code.)
>Anyway, the problem is to
>
> select the row from a table having the largest date
> field among all rows of that table having the same task_Id.
>
>The solution proposed by Aahz (whose .signature is to be commended) is
>in the right direction but not at the proper destination. What is
>needed here is a subquery to find the value of the date. Comme ca:
>
>select whatever_you_want
>from Table Outer
>where date_field =
> (select max(date_field)
> from Table Inner
> where Inner.task_id = outer.task_id);>
>Inner and Outer are called corelation names (but don't ask me why
>they're called that).
That's "correlation" name and it comes from the concept that
it correlates the rows from the subquery with the rows in the
outer query. This is why a subquery that has outer references
is called a correlated subquery (whereas one that has no outer
references is merely a subquery). This was the initial purpose
for the use of a correlation name in a from clause. It is also
used to distinguish a table from itself when a self-join is done.
Jon Rosen