Counting distinct entries in a column using relational algebra So I have a table similar to this one. Each user has posted a review about one or more hotels(A,B,C,D) but on different dates so there are no duplicate tuples even though a person might have reviewed the same hotel more than once.

I need to count the number of DISTINCT hotels every user has reviewed using RELATIONAL ALGEBRA only. How can I do that?



example to show notations I use:

R = ƔUser,COUNT(Hotel_reviewed)-&gt;Num_Reviews (InitialRelation- table 1) 


would give the number of reviews by each user 

The result should be the following table:



example to show notations I use:

R = ƔUser,COUNT(Hotel_reviewed)-&gt;Num_Reviews (InitialRelation- table 1) 


would give the number of reviews by each user
Besides the more compact syntax (from @McNets' answer):

select   User,
         count(distinct Hotel_Reviewed) HotelsReviewed
from     InitialRelation
group by User;


we can also do a projection first to find distinct User, Hotel_Reviewed pairs and then aggregate:

select   User,
         count(Hotel_Reviewed) as Hotels_Reviewed
from     
    ( select distinct
               User,
               Hotel_Reviewed
      from     InitialRelation
    ) as D
group by User ;


This leads us to the relational algebra notation:

R = Ɣ User, COUNT(Hotel_Reviewed) -&gt; Hotels_Reviewed 
        (π User, Hotel_Reviewed (InitialRelation)) -&gt; D