返回介绍

solution / 1300-1399 / 1355.Activity Participants / README_EN

发布于 2024-06-17 01:03:20 字数 2809 浏览 0 评论 0 收藏 0

1355. Activity Participants

中文文档

Description

Table: Friends

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| id      | int   |
| name      | varchar |
| activity    | varchar |
+---------------+---------+
id is the id of the friend and the primary key for this table in SQL.
name is the name of the friend.
activity is the name of the activity which the friend takes part in.

 

Table: Activities

+---------------+---------+
| Column Name   | Type  |
+---------------+---------+
| id      | int   |
| name      | varchar |
+---------------+---------+
In SQL, id is the primary key for this table.
name is the name of the activity.

 

Find the names of all the activities with neither the maximum nor the minimum number of participants.

Each activity in the Activities table is performed by any person in the table Friends.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
Friends table:
+------+--------------+---------------+
| 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 table:
+------+--------------+
| id   | name     |
+------+--------------+
| 1  | Eating     |
| 2  | Singing    |
| 3  | Horse Riding |
+------+--------------+
Output: 
+--------------+
| activity   |
+--------------+
| Singing    |
+--------------+
Explanation: 
Eating activity is performed by 3 friends, maximum number of participants, (Jonathan D. , Elvis Q. and Daniel A.)
Horse Riding activity is performed by 1 friend, minimum number of participants, (Bob B.)
Singing is performed by 2 friends (Victor J. and Jade W.)

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  t AS (
    SELECT activity, COUNT(1) AS cnt
    FROM Friends
    GROUP BY activity
  )
SELECT activity
FROM t
WHERE cnt > (SELECT MIN(cnt) FROM t) AND cnt < (SELECT MAX(cnt) FROM t);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文