LeetCode-1355. Event participants (medium)

表: Friends

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| the above mentioned id | int |
| name | VARCHAR |
| Activity | VARCHAR |
+ --------------- + -------- - +
id is the primary key id and friends of the table
name is the name of a friend of
activity is a friend to participate in activities name
table: activities

+ --------- + --------------- +
| Column the Name | Type |
+ --------------- + + ---------
| the above mentioned id | int |
| name | VARCHAR |
+ --------------- + --------- +
the above mentioned id is the table's primary key
name is the name of the activity
 

Write a SQL query that neither the most nor the least active participants in the name of

The results can be returned in any order, participants in each activity Activities table are from Friends list

The following are examples of query results format:

Friends 表:
+------+--------------+---------------+
| id   | name         | activity      |
+------+--------------+---------------+
| 1    | Jonathan D.  | Eating        |
| 2    | Jade W.      | Singing       |
| 3    | Victor J.    | Singing       |
| 4    | Elvis Q.     | Eating        |
| 5    | Daniel A.    | Eating        |
| 6    | Bob B.       | Horse Riding  |
+------+--------------+---------------+

Activities 表:
+------+--------------+
| id   | name         |
+------+--------------+
| 1    | Eating       |
| 2    | Singing      |
| 3    | Horse Riding |
+------+--------------+

Result 表:
+--------------+
| results      |
+--------------+
| Singing      |
+--------------+

Eating three people to participate in activities, is the most-attended events (Jonathan D., Elvis Q. and A. Daniel)
Horse Riding activity a person participated in, is the least-attended event (Bob B.)
Singing activity has two participants ( Victor J. and Jade W.)

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/activity-participants
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Moderation:

Write a SQL query that neither the most nor the least active participants in the name of

The results can be returned in any order, participants in each activity Activities table are from Friends list

Thinking:

Problem solving:

break off both ends:

select activity
from Friends
group by activity
having count(id) != (select count(id) from Friends group by activity order by count(id) desc limit 1)
and count(id) != (select count(id) from Friends group by activity order by count(id) limit 1)

Knowledge points:

select temp.activity ACTIVITY 
from (select activity, count(*) from friends group by activity having count(*) between 
(select count(*)+1 a from friends group by activity order by a asc limit 1)  
and 
(select count(*)-1 a from friends group by activity order by a desc limit 1)) temp

 

Published 144 original articles · won praise 2 · Views 5727

Guess you like

Origin blog.csdn.net/Hello_JavaScript/article/details/104793465