Forum

Posted by Andrei, Jan. 19, 2022, 3:27 a.m.

99. Page recommendation possible bug

I fail to understand why the following query on question 99's environment does not return 1 on the count column for the following query.
In my opinion this is a bug, which also comes up if you are trying a different approach than the solution suggested.
select
u.user_id,
u.friend_id,
lp.page_id,
count(*)
from friends as u
join friends as f
on u.friend_id=f.user_id
join liked_page lp
on f.user_id=lp.user_id
and u.user_id!=lp.user_id
where u.user_id=1
group by 1,2,3
order by 4 desc

Answers

Hey Andrei,
I don't quite understand the logic of your query, but I suggest you break down your query into multiple steps with common table expressions (CTE) and check intermediary results.
Leon

Got your point Andrei, thanks for clarifying. It looks like your JOIN is creating a cartesian join that's why multiple rows are returned. select u.user_id, u.friend_id from friends as u join friends as f on u.friend_id=f.user_id user_id friend_id 1 47 1 47 1 47 1 47 1 76 1 76 1 76 1 76 1 76 1 76 1 76 1 76 1 76 1 76 1 76

Leon, Jan. 31, 2022, 8:48 a.m.

What I'm tryin to say is that the query mentioned should return 1 for all rows in the count(*) column and it is not. Which makes me think there's a bug somewhere in the env the query is running in. See, all my query is doing is joining the users w/ friends (many to 1) and likes pages (1 to 1). So if you take the count per user per friend per liked page, you should get one.

Andrei, Jan. 20, 2022, 2:42 p.m.
SQLPad user avatar

Leon (949)

Jan. 19, 2022, 10:28 a.m.