1

I am trying to write an SQL statement producing the below output.
I have the two following tables:

UserMovie

userID | movieID
-----------------
  135  |  k0jps
  135  |  p1zka
  125  |  v0t67
  115  |  opp2s
  111  |  xnwri
  115  |  kspdl

Follows

followerid | followingid
------------------------
    122    |     135  
    192    |     111
    125    |     240
    120    |     125
    45     |     111

I want to fetch the number of followers of each user who's userid is in the UserMovie Table, giving the following result:

Result

userid | followerCount
----------------------
  135  |       1
  125  |       1
  115  |       0
  111  |       2

The following statement gives me partially what i want:

SELECT followingid, count(*) as followerCount   
FROM Follows   
WHERE followingid in (SELECT DISTINCT userID FROM UserMovie)   
GROUP BY followingid  

The issue with the above query is that users with 0 followers do not appear in the results giving the following output:

userid | followerCount
----------------------
  135  |       1
  125  |       1
  111  |       2

Any idea on how to do it?

0

4 Answers 4

1

Try this to include users with no follows:

SELECT  UserId, Count(followerid) AS followerCount
FROM (SELECT DISTINCT userId FROM UserMovie ) m
LEFT JOIN Follows f
ON f.followingid = m.userID
GROUP BY UserId

Now it generates :

UserId  followerCount
111 2
115 0
125 1
135 1
4
  • Giving wrong answer. I think something's wrong with your join.
    – Joe
    May 24, 2015 at 22:31
  • i am not sure who follows who in your follow table. try change the followngid.
    – Tim3880
    May 24, 2015 at 22:32
  • followerid follows followingid
    – Joe
    May 24, 2015 at 22:34
  • 1
    Sorry didn't notice that you don't have an user table with unique userid. Changed the code.
    – Tim3880
    May 24, 2015 at 22:43
0

The following worked for me. However I am getting NULLs instead of 0 for users with no followers

SELECT DISTINCT u.userid, t.followerCount   
FROM UserMovie u  
LEFT JOIN (   
SELECT followingid, count(*) AS followerCount      
FROM Follows      
WHERE followingid in (SELECT DISTINCT userID FROM UserMovie)      
GROUP BY followingid ) as t   
on t.followingid = u.userid
0

How about a solution using CASE?

SELECT userId,
CASE
  WHEN IFNULL(followerid, 0) = 0 THEN 0
  ELSE count(*)
END
FROM UserMovie
LEFT JOIN Follows on followingid=userID
GROUP BY userId;

Seems to work fine in SQLite3, just replace IFNULL with ISNULL (if SQLServer) or any other equivalent. It's pretty similar to what you've done.

2
  • Note that UserMovie's PK is the combination (UserID + MovieID) and not UserID itself (can have multiple entries)
    – Joe
    May 24, 2015 at 22:41
  • There's no need for a CASE expression. The query will return the specified result if we replace the CASE expression with COUNT(DISTINCT Follows.followerID). (Yes, the followerID column will be NULL when a matching row isn't found, but we don't need a CASE to hanle that, COUNT starts at zero and increments for non-NULL values. The DISTINCT keyword, inside the COUNT() means any "duplicated" values won't be included in the count, each unique value will be increment the count by one.) May 24, 2015 at 23:02
0

Here's one approach: get a distinct list of userID from UserMovie in an inline view (use either a GROUP BY or a DISTINCT keyword), and perform an "outer join" operation of that to the Followers table to find followers. Collapse the rows from that with a GROUP BY, and use an aggregate function to get a count of unique/distinct non-null values of userId from the Followers table.

For example:

SELECT u.userID
     , COUNT(DISTINCT f.userID) AS cnt_followers
  FROM ( SELECT m.userID
           FROM UserMovie m
          GROUP BY m.userID
       ) u
  LEFT
  JOIN Follows f
    ON f.followingid = u.userID
 GROUP BY u.userID

EDIT

There's an invalid column reference in the SELECT list, f.userID is not valid. That should be f.followerID.

When we fix that, the query returns:

userID  cnt_followers
111     2
115     0
125     1
135     1

SQL Fiddle HERE http://sqlfiddle.com/#!9/de3e7/2

As long as we are counting "distinct" followerid (question doesn't give any guarantee that (followerID,followingID) is UNIQUE in Followers table), we could eliminate the inline view

SELECT u.userID
     , COUNT(DISTINCT f.userID) AS cnt_followers
  FROM UserMovie u
  LEFT
  JOIN Follows f
    ON f.followingid = u.userID
 GROUP BY u.userID
2
  • This is correctly inserting 0s but giving a wrong value for the number of followers of users whose num of followers > 0
    – Joe
    May 24, 2015 at 22:34
  • The reference to f.userID in my query is invalid. That should be f.followerID. SQL Fiddle demonstration added. May 24, 2015 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.