Sunday, October 23, 2011

Finding the Maximum using Relational Algebra

We know that if you want to get the maximum value of a column in SQL, you can simply use the MAX function as shown below:
SELECT MAX(value) FROM T
You can also do it without the MAX function as follows:
SELECT T.* FROM T
MINUS
SELECT T.* FROM T, T as T2 WHERE T.value<T2.value
or:
SELECT T.* FROM T
LEFT JOIN T as T2 ON T.value<T2.value
WHERE T2.value IS NULL
Relational Algebra:
Using Relational Algebra (RA) syntax, this would be:
\project_{value}(T)
\diff
\project_{value} (
    \select_{value < value2}(
      \project_{value}(T)
      \cross
      \rename_{value2}(\project_{value}(T))
    )
)
where:
  • \cross is the relational cross-product operator
  • \diff is the relational diff operator
  • \project_{attr_list} is the relational projection operator
  • \rename_{new_attr_name_list} is the relational renaming operator
  • \select_{cond} is the relational selection operator

1 comments: