4

So I have a relation:

Cars(model, passenger)

The models are all unique, let's say, {A, B, C, D, E}.

Passengers is just the capacity of the car (any positive non-zero integer), let's say {1,2,2,3,3}

Model|Passenger
A    |1
B    |2
C    |2
D    |3
E    |3

I need to find the relational algebra expression that would yield what capacities occur for more than 1 vehicle. So with the example values above, the expression should return {2, 3} since they appear more than once for different vehicles.

I have a strong inclination to think that the expression will use a join of some sort but I can't figure out how to do it.

Doronz
  • 704
  • 8
  • 21
  • Does this answer your question? [Using Relational Algebra, how can I find duplicate rows in a tuple?](https://stackoverflow.com/questions/19864120/using-relational-algebra-how-can-i-find-duplicate-rows-in-a-tuple) – philipxy Dec 05 '19 at 08:52

2 Answers2

1

I figured it out:

Assuming an existing relation Cars(model, passenger) that contains all of the cars in question and their passenger capacities.

CARS2(model,passenger)≔ρ_(m,p) (CARS)
Answer (passenger)≔π_passenger (CARS⋈_(model ≠ m AND passenger=p) CARS2)
Doronz
  • 704
  • 8
  • 21
0

I'm not sure about relational algebra expression, which might look something along the lines

π Passenger σ Count(Model) >= 2 G Passenger (Table1)

but if you're looking for a query than it doesn't include a JOIN in it

SELECT passenger
  FROM table1
 GROUP BY passenger
HAVING COUNT(model) >= 2

Outcome:

| PASSENGER |
|-----------|
|         2 |
|         3 |

Here is SQLFiddle demo

peterm
  • 91,357
  • 15
  • 148
  • 157
  • Sorry. I am not looking for a SQL answer. I am looking for a relational algebra expression. – Doronz Oct 14 '14 at 03:11