P.s.
Except for great performance, when facing the lack of table such as "USER", this approach present a major advantage since there is no need to use FULL JOIN nor to generate a sub-query that will function as the base for LEFT JOIN
Solution 1
select *
from ( select 'U' as tab ,idUser from "USER"
union all select 'S' ,idUser from Subscription
union all select 'F' ,idUser from Favourite
union all select 'H' ,idUser from History
)
pivot (count(*) for tab in ('S' as nb_sub,'F' as nb_fav,'H' as nb_hist))
;
Solution 2
This solution has less clean syntax but gives you a lot of freedom to manipulate the data ,e.g. add the nb_total column
select idUser
,count (decode (tab,'U',1)) as nb_user -- If the data is good there supposed to be 1 record per user
,count (decode (tab,'S',1)) as nb_sub
,count (decode (tab,'H',1)) as nb_fav
,count (decode (tab,'F',1)) as nb_hist
,count (*) as nb_total
from ( select 'U' as tab ,idUser from "USER"
union all select 'S' ,idUser from Subscription
union all select 'F' ,idUser from Favourite
union all select 'H' ,idUser from History
)
group by idUser
;
give me number of users who favors subscription x, for example)?select idUser, count(*)....from SUBSCRIPTION/HISTORY/FAVORITEin Derived Tables or CTEs and join them.WHEREclause and start using an explicitJOINoperator