Knowledge Guide
HomeDatabasesSQL Practice Problems

Second Degree Follower

Problem

Table: Follow

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| followee    | varchar |
| follower    | varchar |
+-------------+---------+
(followee, follower) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates that the user follower follows the user followee on a social network.
There will not be a user following themself.

Problem Definition

A second-degree follower is a user who:

Write a solution to report the second-degree users and the number of their followers.

Return the result table ordered by follower in alphabetical order.

Example

Image
Image

Output

Image
Image

Try It Yourself

java
-- TODO: Write your user queries here

Solution

To identify second-degree followers—users who both follow at least one other user and are followed by at least one user—we can leverage SQL's self-join capabilities. The approach involves isolating users who meet both criteria and then counting their followers.

SQL Query

SELECT f1.follower AS follower, COUNT(DISTINCT f2.follower) AS num FROM Follow f1 INNER JOIN Follow f2 ON f1.follower = f2.followee GROUP BY f1.follower ORDER BY f1.follower ASC;

Step-by-Step Approach

Step 1: Identify Users Who Follow Others

Determine all users who follow at least one other user by selecting distinct followers from the Follow table.

SQL Query:

SELECT DISTINCT follower FROM Follow;

Explanation:

Output After Step 1:

+----------+ | follower | +----------+ | Bob | | Cena | | Donald | | Edward | +----------+

Step 2: Identify Users Who Are Followed

Determine all users who are followed by at least one other user by selecting distinct followees from the Follow table.

SQL Query:

SELECT DISTINCT followee FROM Follow;

Explanation:

Output After Step 2:

+----------+ | followee | +----------+ | Alice | | Bob | | Donald | +----------+

Step 3: Determine Second-Degree Followers and Count Their Followers

Identify users who both follow others and are followed by others (second-degree followers) and count the number of their followers.

SQL Query:

SELECT f1.follower AS follower, COUNT(DISTINCT f2.follower) AS num FROM Follow f1 INNER JOIN Follow f2 ON f1.follower = f2.followee GROUP BY f1.follower ORDER BY f1.follower ASC;

Explanation:

Output After Step 3:

+----------+-----+ | follower | num | +----------+-----+ | Bob | 2 | | Donald | 1 | +----------+-----+
🤖 Don't fully get this? Learn it with Claude

Stuck on Second Degree Follower? 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 **Second Degree Follower** (Databases) and want to truly understand it. Explain Second Degree Follower 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 **Second Degree Follower** 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 **Second Degree Follower** 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 **Second Degree Follower** 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