Knowledge Guide
HomeDatabasesSQL Practice Problems

Find Followers Count

Problem

Table: Followers

+-------------+------+
| Column Name | Type |
+-------------+------+
| user_id     | int  |
| follower_id | int  |
+-------------+------+
(user_id, follower_id) is the primary key (combination of columns with unique values) for this table.
This table contains the IDs of a user and a follower in a social media app where the follower follows the user.

Problem Definition

Write a solution that will, for each user, return the number of followers.

Return the result table ordered by user_id in ascending order.

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To solve this problem, the approach involves using SQL queries to analyze the Followers table and determine the number of followers for each user in a social media app. The table consists of pairs of user and follower IDs, representing the follower-followee relationships.

The solution employs the COUNT function along with the GROUP BY clause to group the data based on the user_id. This allows the calculation of the count of followers for each user. The COUNT function counts the occurrences of each unique user_id in the table, providing the number of followers for each user.

The result set is then ordered by user_id in ascending order using the ORDER BY clause, as specified in the problem statement. This ensures that the final table presents the number of followers for each user, organized by the user IDs.

SELECT user_id, Count(user_id) AS followers_count FROM Followers GROUP BY user_id ORDER BY user_id;

Let's break down the query step by step:

Step 1: Counting followers for each user

We use the COUNT function to count the number of followers for each user_id by grouping the results based on user_id.

SELECT user_id, Count(user_id) AS followers_count FROM Followers GROUP BY user_id

Output After Step 1:

+---------+----------------+ | user_id | followers_count| +---------+----------------+ | 0 | 1 | | 1 | 1 | | 2 | 2 | +---------+----------------+

Step 2: Ordering the result by user_id

Finally, we order the result by user_id in ascending order as requested.

ORDER BY user_id;

Final Output:

+---------+----------------+ | user_id | followers_count| +---------+----------------+ | 0 | 1 | | 1 | 1 | | 2 | 2 | +---------+----------------+
🤖 Don't fully get this? Learn it with Claude

Stuck on Find Followers Count? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Find Followers Count** (Databases) and want to truly understand it. Explain Find Followers Count from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Find Followers Count** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Find Followers Count** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Find Followers Count** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes