|
|
|
|
|
by MattRogish
5108 days ago
|
|
It's been a loooong time since I wrote any longhand Relational Algebra, so I'll cheat and use SQL. All of this can be done pretty easily with relational algebra primitives. And we can make up any operator we want as long as it uses a primitive, so (to save typing I created a view, but you could copy-pasta). I added a "Title" to post because otherwise you could skip it entirely and just use the comment table twice, but where's the fun in that? <pre>
CREATE VIEW counts AS
( select count() AS comment_count, post_id
from comment
group by post_id ) SELECT id, title, comment_count
FROM post p
INNER JOIN counts AS c1
ON p.id = c1.post_id
WHERE NOT EXISTS( SELECT
FROM counts AS c2
WHERE c2.post_id != p.id
AND c2.comment_count > c1.comment_count )
</pre> |
|
However you can't do the same trick if I ask you to return the top 3 posts with the largest number of comments; or, to make the query more realistic, ask you to return the percentage of comments generated by the top 10% popular (by the number of comments) posts. Which is my point: pure relational algebra as advocated by Date et al in Tutorial D is less expressive than SQL, which probably explains the cold reception it got from the industry.
Edit: now that I think about it, you could do it without ORDER BY/LIMIT, but still it's harder than necessary.