Search code examples
mysqlgroup-bycountdata-analysishaving

Check whether User is a bot or not in MySql


I'm doing data analysis on Instagram user database, I need to find users who are Bots by checking which user has liked all photos in site as liking all photo is not practically possible for a real user.

Table Format

like
-- 
user_id
photo_id
created_at
photos 
--
id
image_url
user_id
created_at
user
--
id
username
created_at

---- Tried ----

SELECT photos.user_id, count(photos.user_id) AS users_in_photos, count(likes.user_id) AS users_in_likes
FROM photos
INNER JOIN likes
ON photos.user_id = likes.user_id
group by photos.user_id;

With this I expected to get users who are in both photos and like table, and by seeing their count I can determine who are bots.


Solution

  • The number of all photos can be obtained with:

    SELECT COUNT(*) FROM photos
    

    Assuming there are no duplicate combinations of user_id and photo_id in the table likes, use aggregation and filter the bots in the HAVING clause:

    SELECT user_id
    FROM likes
    GROUP BY user_id
    HAVING COUNT(*) = (SELECT COUNT(*) FROM photos);