mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-11 02:58:13 +08:00
48 lines
1.2 KiB
HTML
48 lines
1.2 KiB
HTML
<p>Table: <code>Followers</code></p>
|
|
|
|
<pre>
|
|
+-------------+------+
|
|
| Column Name | Type |
|
|
+-------------+------+
|
|
| user_id | int |
|
|
| follower_id | int |
|
|
+-------------+------+
|
|
(user_id, follower_id) is the primary key 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.</pre>
|
|
|
|
<p> </p>
|
|
|
|
<p>Write an SQL query that will, for each user, return the number of followers.</p>
|
|
|
|
<p>Return the result table ordered by <code>user_id</code>.</p>
|
|
|
|
<p>The query result format is in the following example.</p>
|
|
|
|
<p> </p>
|
|
<p><strong>Example 1:</strong></p>
|
|
|
|
<pre>
|
|
<strong>Input:</strong>
|
|
Followers table:
|
|
+---------+-------------+
|
|
| user_id | follower_id |
|
|
+---------+-------------+
|
|
| 0 | 1 |
|
|
| 1 | 0 |
|
|
| 2 | 0 |
|
|
| 2 | 1 |
|
|
+---------+-------------+
|
|
<strong>Output:</strong>
|
|
+---------+----------------+
|
|
| user_id | followers_count|
|
|
+---------+----------------+
|
|
| 0 | 1 |
|
|
| 1 | 1 |
|
|
| 2 | 2 |
|
|
+---------+----------------+
|
|
<strong>Explanation:</strong>
|
|
The followers of 0 are {1}
|
|
The followers of 1 are {0}
|
|
The followers of 2 are {0,1}
|
|
</pre>
|