返回介绍

solution / 2000-2099 / 2072.The Winner University / README_EN

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

2072. The Winner University

中文文档

Description

Table: NewYork

+-------------+------+
| Column Name | Type |
+-------------+------+
| student_id  | int  |
| score     | int  |
+-------------+------+
In SQL, student_id is the primary key for this table.
Each row contains information about the score of one student from New York University in an exam.

 

Table: California

+-------------+------+
| Column Name | Type |
+-------------+------+
| student_id  | int  |
| score     | int  |
+-------------+------+
In SQL, student_id is the primary key for this table.
Each row contains information about the score of one student from California University in an exam.

 

There is a competition between New York University and California University. The competition is held between the same number of students from both universities. The university that has more excellent students wins the competition. If the two universities have the same number of excellent students, the competition ends in a draw.

An excellent student is a student that scored 90% or more in the exam.

Return:

  • "New York University" if New York University wins the competition.
  • "California University" if California University wins the competition.
  • "No Winner" if the competition ends in a draw.

The result format is in the following example.

 

Example 1:

Input: 
NewYork table:
+------------+-------+
| student_id | score |
+------------+-------+
| 1      | 90  |
| 2      | 87  |
+------------+-------+
California table:
+------------+-------+
| student_id | score |
+------------+-------+
| 2      | 89  |
| 3      | 88  |
+------------+-------+
Output: 
+---------------------+
| winner        |
+---------------------+
| New York University |
+---------------------+
Explanation:
New York University has 1 excellent student, and California University has 0 excellent students.

Example 2:

Input: 
NewYork table:
+------------+-------+
| student_id | score |
+------------+-------+
| 1      | 89  |
| 2      | 88  |
+------------+-------+
California table:
+------------+-------+
| student_id | score |
+------------+-------+
| 2      | 90  |
| 3      | 87  |
+------------+-------+
Output: 
+-----------------------+
| winner        |
+-----------------------+
| California University |
+-----------------------+
Explanation:
New York University has 0 excellent students, and California University has 1 excellent student.

Example 3:

Input: 
NewYork table:
+------------+-------+
| student_id | score |
+------------+-------+
| 1      | 89  |
| 2      | 90  |
+------------+-------+
California table:
+------------+-------+
| student_id | score |
+------------+-------+
| 2      | 87  |
| 3      | 99  |
+------------+-------+
Output: 
+-----------+
| winner  |
+-----------+
| No Winner |
+-----------+
Explanation:
Both New York University and California University have 1 excellent student.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT
  CASE
    WHEN n1.cnt > n2.cnt THEN 'New York University'
    WHEN n1.cnt < n2.cnt THEN 'California University'
    ELSE 'No Winner'
  END AS winner
FROM
  (SELECT COUNT(1) AS cnt FROM NewYork WHERE score >= 90) AS n1,
  (SELECT COUNT(1) AS cnt FROM California WHERE score >= 90) AS n2;

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

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

发布评论

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