# 在不使用关系代数中的max函数的情况下寻找两个表的最大值[英] finding max value among two table without using max function in relational algebra ## 推荐答案

so:如何仅使用natjoin在表中找到最大m?这是在表上找到层次结构的表上的顶级节点的简化版本(想想汇编/组件爆炸或组织图).

```A NOT MATCHING
((A x (A RENAME m AS mm)) WHERE m < mm)
```

[我已经使用了教程D语法，以避免用希腊字母弄乱.]

## 其他推荐答案

```SELECT M FROM (SELECT M FROM A UNION SELECT M FROM B) ORDER BY M DESC LIMIT 1
```

### 问题描述

Suppose I have two tables A{int m} and B{int m} and I have to find maximum m among two tables using relational algebra but I cannot use max function.How can I do it?I think using join we can do it but i am not sure if my guess is correct or not.

Note: this is an interview question.

## 推荐答案

Hmm, I'm puzzled why the question involves two tables. For the question as asked, I would just UNION the two (as StilesCrisis has done), then solve for a single table.

So: how to find the maximum m in a table using only NatJOIN? This is a simplified version of finding the top node on a table that holds a hierarchy (think assembly/component explosions or org charts).

The key idea is that we need to 'copy' the table into something with a different attribute name so that we can compare the tuples pair-wise. (And this will therefore use the degenerate form of NatJOIN aka cross-product). See example here How can I find MAX with relational algebra?

```A NOT MATCHING
((A x (A RENAME m AS mm)) WHERE m < mm)
```

The subtrahend is all tuples with m less than some other tuples. The anti-join is all the tuples except those -- ie the MAX. (Using NOT MATCHING I think is both more understandable than MINUS, and doesn't need the relations to be UNION-compatible. It's roughly equivalent to SQL NOT EXISTS).)

[I've used Tutorial D syntax, to avoid mucking about with greek letters.]

## 其他推荐答案

```SELECT M FROM (SELECT M FROM A UNION SELECT M FROM B) ORDER BY M DESC LIMIT 1
```

This doesn't use MAX, just plain vanilla SQL.