File tree Expand file tree Collapse file tree 1 file changed +82
-0
lines changed
LeetCode SQL 50 Solution/1729. Find Followers Count Expand file tree Collapse file tree 1 file changed +82
-0
lines changed Original file line number Diff line number Diff line change 1+ Here’s a well-structured ` README.md ` for ** LeetCode 1729 - Find Followers Count** , formatted for a GitHub repository:
2+
3+ ``` md
4+ # 📊 Find Followers Count - LeetCode 1729
5+
6+ ## 📌 Problem Statement
7+ You are given a table **Followers** that contains the following information:
8+
9+ - `user_id`: The ID of the user being followed.
10+ - `follower_id`: The ID of the user who is following.
11+
12+ Your task is to return a list of users with their **follower count**, sorted in **ascending order of `user_id`**.
13+
14+ ---
15+
16+ ## 📊 Table Structure
17+
18+ ### **Followers Table**
19+ | Column Name | Type |
20+ | ----------- | ---- |
21+ | user_id | int |
22+ | follower_id | int |
23+
24+ - `(user_id, follower_id)` is the **primary key**.
25+ - Each row represents a **follower relationship** between two users.
26+
27+ ---
28+
29+ ## 📊 Example 1:
30+
31+ ### **Input:**
32+ #### **Followers Table**
33+ | user_id | follower_id |
34+ | ------- | ----------- |
35+ | 0 | 1 |
36+ | 1 | 0 |
37+ | 2 | 0 |
38+ | 2 | 1 |
39+
40+ ### **Output:**
41+ | user_id | followers_count |
42+ | ------- | --------------- |
43+ | 0 | 1 |
44+ | 1 | 1 |
45+ | 2 | 2 |
46+
47+ ### **Explanation:**
48+ - **User 0** has **1 follower** `{1}`.
49+ - **User 1** has **1 follower** `{0}`.
50+ - **User 2** has **2 followers** `{0, 1}`.
51+
52+ ---
53+
54+ ## 🖥 SQL Solution
55+
56+ ### ✅ **Approach:**
57+ 1. Use `COUNT(follower_id)` to count the number of followers for each `user_id`.
58+ 2. Use `GROUP BY user_id` to group the followers for each user.
59+ 3. Sort the result by `user_id` in **ascending order**.
60+
61+ ```sql
62+ SELECT user_id, COUNT(follower_id) AS followers_count
63+ FROM Followers
64+ GROUP BY user_id
65+ ORDER BY user_id;
66+ ```
67+
68+ ---
69+
70+ ## 📁 File Structure
71+ ```
72+ 📂 Find-Followers-Count
73+ │── 📜 README.md
74+ │── 📜 solution.sql
75+ │── 📜 test_cases.sql
76+ ```
77+
78+ ---
79+
80+ ## 🔗 Useful Links
81+ - 📖 [ LeetCode Problem] ( https://leetcode.com/problems/find-followers-count/ )
82+ - 📝 [ MySQL COUNT Function] ( https://www.w3schools.com/sql/sql_count.asp )
You can’t perform that action at this time.
0 commit comments