I have a problem where I have two relations, one containing attributes song_id, song_name, album_id, and the other containing album_id and album_name. I need to find the names of all the albums that do not have songs in the song relation. The problem is I can only use Rename, Projection, Selection, Grouping(with sum,min,max,count), Cartesian Product, and Natural join. I have spent a good amount of time working on this and would appreciate any help that pointed me in the right direction.
I have a problem where I have two relations, one containing attributes song_id, song_name,
Share
As @ErwinSmout pointed out, difference is a generally easy way to do it. But since you can’t use it, there is a tricky workaround using counts. I’m assuming that every
album_idpresent in the songs relation is also present in thealbumsrelation.PROJECTalbum_id from the songs relation (note that relational algebra’sPROJECTis equivalent to SQL’sSELECT DISTINCT). I’ll call this relationsong_albums. Now lets take the count of thealbumsrelation, call this m, and take the count of the new table, call this n.Take the Cartesian product of the albums relation and the song_albums relation. This new relation has m*n rows. Now if you do a count, grouped by
album_name, each of the malbum_name‘s will have a count of n. Not very helpful.But now, we
SELECTfrom the relation rows wherealbums.album_id != song_albums.album_id. Now, if you do a count grouped byalbum_name, the count for those albums that were not in the originalsongsrelation will be n, while those that were originally in there will have a count less than n, since rows would have been removed based on how many songs with that album were in the originalsongsrelation.Edit: As it turns out, this isn’t a strictly relational-algebra solution: In SQL, a 1 x 1 table, such as the one containing
ncan simply be treated as an integer and used in an equality comparison. However, according to Wikipedia, selection must make a comparison between either two attributes of a relation, or an attribute and a constant value.Another obstacle which will be dealt with by another ill-recommended Cartesian product: we can take the Cartesian product of the 1 x 1 relation containing
nwith our most recent relation. Now we can make a proper relational-algebra selection since we have an attribute that is always equal ton.Since this has gotten rather complex, here is a relational-algebra expression capturing the above english explanation:
Note that n is a 1 x 1 relation with an attribute named “count”.