You are a data analyst at Vibe , a growing social media platform where users can follow each other to see updates and posts. The growth team wants to identify the most influential users on the platform by looking at how many followers each user has accumulated. This data will be used to power a Suggested Accounts feature and to shortlist creators for a brand partnership program. The database contains two tables: users — which stores user profile information including user_id , username , and joined_at — and follows — which tracks follow relationships between users, containing follow_id , follower_id (the user who is following), and followee_id (the user being followed). Each row in follows represents one user following another. Your task is to write a SQL query that returns each username along with their total follower count . Only include users who have at least 1 follower . The results should be sorted by follower count in descending order so the most-followed users appear at the top. Join the users table with the follows table by matching users.user_id to follows.followee_id . Use COUNT(f.follow_id) to calculate how many followers each user has. Group results by user_id and username . Return columns: username and follower_count . Sort output by follower_count in descending order. Only include users with at least 1 follower (use HAVING COUNT(f.follow_id) >= 1 or rely on the INNER JOIN to exclude them naturally). Hint: Pay close attention to the direction of the follow relationship. The followee_id is the person being followed — this is the column you should join to users.user_id to count how many people follow a given user. A very common mistake is joining on follower_id instead, which would give you the number of accounts each user follows, not their follower count.
Table Setup (SQL Schema)
CREATE TABLE users (
user_id SERIAL PRIMARY KEY,
username VARCHAR(50) NOT NULL,
joined_at DATE NOT NULL
);
CREATE TABLE follows (
follow_id SERIAL PRIMARY KEY,
follower_id INT NOT NULL REFERENCES users(user_id),
followee_id INT NOT NULL REFERENCES users(user_id)
);
INSERT INTO users (username, joined_at) VALUES
('alex_codes', '2021-01-10'),
('bella_travels', '2021-03-22'),
('carlos_eats', '2021-06-05'),
('dana_lifts', '2022-01-18'),
('evan_draws', '2022-04-30'),
('fiona_reads', '2022-08-14'),
('george_runs', '2023-02-02'),
('hana_bakes', '2023-05-19');
INSERT INTO follows (follower_id, followee_id) VALUES
(2, 1),
(3, 1),
(4, 1),
(5, 1),
(6, 1),
(7, 2),
(8, 2),
(1, 2),
(3, 2),
(1, 3),
(4, 3),
(5, 3),
(2, 4),
(6, 4),
(7, 5),
(3, 6),
(1, 7),
(2, 7),
(4, 7);