+-------------+------+
|Column Name |Type|+-------------+------+
| student_id | int || score | int |+-------------+------+
student_id is the primarykeyfor this table.
Eachrowcontains information about the score of one student fromNew York University in an exam.
Table: California
1
2
3
4
5
6
7
8
+-------------+------+
|Column Name |Type|+-------------+------+
| student_id | int || score | int |+-------------+------+
student_id is the primarykeyfor this table.
Eachrowcontains 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.
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.
WITH NewYorkExcellent AS (
SELECTCOUNT(*) AScountFROM NewYork
WHERE score >=90 ),
CaliforniaExcellent AS (
SELECTCOUNT(*) AScountFROM California
WHERE score >=90 ),
Results AS (
SELECTCASEWHEN nx.count> cx.countTHEN'New York University'WHEN cx.count> nx.countTHEN'California University'ELSE'No Winner'ENDASResultFROM NewYorkExcellent nx, CaliforniaExcellent cx
)
SELECTResultFROM Results;