In social network like Facebook or Twitter, people send friend requests and accept others' requests as well.

Table request_accepted holds the data of friend acceptance, while requester_id and accepter_id both are the id of a person.

| requester_id | accepter_id | accept_date|
|--------------|-------------|------------|
| 1 | 2 | 2016_06-03 |
| 1 | 3 | 2016-06-08 |
| 2 | 3 | 2016-06-08 |
| 3 | 4 | 2016-06-09 |

Write a query to find the the people who has most friends and the most friends number. For the sample data above, the result is:

| id | num |
|----|-----|
| 3 | 3 |

Note:

    • It is guaranteed there is only 1 people having the most friends.
    • The friend request could only been accepted once, which mean there is no multiple records with the same requester_id and accepter_id value.

      Explanation:
      The person with id '3' is a friend of people '1', '2' and '4', so he has 3 friends in total, which is the most number than any others.

      Follow-up:
      In the real world, multiple people could have the same most number of friends, can you find all these people in this case?

Algorithm

Being friends is bidirectional, so if one person accepts a request from another person, both of them will have one more friend.

Thus, we can union column requester_id and accepter_id, and then count the number of the occurrence of each person.

select requester_id as ids from request_accepted
union all
select accepter_id from request_accepted;
Note: Here we should use union all instead of union because union all will keep all the records even the 'duplicated' one.

解法:

select ids as id, cnt as num
from
(
select ids, count(*) as cnt
from
(
select requester_id as ids from request_accepted
union all
select accepter_id from request_accepted
) as tbl1
group by ids
) as tbl2
order by cnt desc
limit 1
;  

解法2:

select a.id, count(*) as num
from
(select requester_id as id from request_accepted
union all
select accepter_id as id from request_accepted) a
group by id
order by num desc
limit 1  

解法3:

select t2.Id as id, t2.num as num
from (
select t1.Id, sum(cnt) as num
from(
select accepter_id as Id, count(*) as cnt
from request_accepted
group by accepter_id union all select requester_id as Id, count(*) as cnt
from request_accepted
group by requester_id) t1 group by t1.Id ) t2 order by t2.num DESC
limit 1

  

All LeetCode Questions List 题目汇总

最新文章

  1. 通俗理解Android事件分发与消费机制
  2. 自己修改的两个js文件
  3. NSNumber 、 NSValue 、 日期处理 、 集合类 、 NSArray(一)
  4. UML对象图(转载)
  5. nodejs学习:sails框架的学习
  6. flume+kafka+storm单机部署
  7. Java经典案例之-“统计英文字母、空格、数字和其它字符的个数”
  8. [Apio2012]dispatching
  9. 树莓派小车(三)Python控制小车
  10. Object类(根类)
  11. 集腋成裘-05-angularJS -初识angular
  12. flask,gunicorn,supervisor,nginx配置服务器接口
  13. Android Studio 下载与安装配置
  14. wchar_t,char,string,wstring等的总结
  15. File 操作
  16. iOS Bugly符号化使用分析
  17. 【动态规划】POJ-2385
  18. Bootstrap-Other:v2 教程
  19. android RecyclerView简单的使用
  20. Struts2_day02--课程安排_结果页面配置

热门文章

  1. Codeforces C. Elections(贪心枚举三分)
  2. 51nod 1254 最大子段和 V2
  3. 【Beta】Scrum meeting3
  4. axios基本设置
  5. Djiango-富文本编辑器
  6. 【模板】A*B Problem(FFT快速傅里叶)
  7. Codeforces Round #605 (Div. 3) C. Yet Another Broken Keyboard
  8. MySQL limit 分页查询优化(百万级优化)
  9. 洛谷P2730 [IOI]魔板 Magic Squares
  10. flume 测试 hive sink