How can I find the maximum without using aggregate operation in relational algebra?
The schema of Database is as follows
- Item(IName, Brand)
- Shop(SName, City, Address)
- Sells(IName, SName, Price)
How can I find the item name and snop name which is sold in maximum price without using aggregate function in relational algebra? I know to solve this with using aggregate functions but not sure without using it.
In SQL, but not in relational algebra
You can sort by
Pricedescendingly and limit the results to 1.Example in MySQL
The highest Priced item will be the first record in the result and due the to limit you ignore anything else.
Alternatively, you can find the r1 record which has no r2 record with higher price:
The query above selects the items for which we do not find a higher priced pair.
LEFT JOINallows the right-hand-side to be nonexistent, so anyr1record will have at least a pair, but thoser1records that have the greatest price will have all nulls inr2. Equating for acolumnwithnullensures that we filter out allr1records that had a higher priced pair. We still have aLIMITclause, because in the case of a tie, i.e. more records share the same maximumPricethen we break the tie by selecting the first item that matched the criteria.By default, the first approach I have shown should be preferred, but, if it is not an option for some reason, then you can go by the second approach.
EDIT
Relational algebra
@philipxy pointed out in the comment section that relational algebra has no null or sorting or limit. As a result, in relational algebra one would need to write an expression that searches among the records that which has a price for which does not exist another record whose price is higher.