Skip to main content

The Winner University - Solution & Explanation

EasyPremiumFree on FleetCodeDatabase4 min readAsked at: Walmart Labs
Practice this problem

Problem Statement

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.

Approach Overview

Problem Overview: Two university tables store student scores. You need to count how many students scored at least 90 in each university and return which university has the higher count. If both counts are equal, return No Winner.

Approach 1: SQL Aggregation + CASE Comparison (O(n) time, O(1) space)

The solution scans both tables and counts qualifying students using COUNT with a filtering condition (score >= 90). Each count represents the number of high-performing students in that university. After computing the two counts, a CASE expression compares them and returns the winner: New York University, California University, or No Winner if the counts match.

This works because relational databases handle aggregation efficiently. Each table is scanned once to compute the filtered count. The comparison step is constant time and does not require joins or additional structures.

In MySQL, this is typically implemented using two subqueries that compute counts and a CASE statement to determine the result. Since the dataset is small and operations are straightforward aggregations, the query remains efficient even with large tables.

Conceptually, this problem tests understanding of database queries, conditional logic in SQL, and aggregation patterns like COUNT-based summaries. These patterns appear frequently in analytics-style interview questions where multiple datasets must be compared.

Recommended for interviews: The aggregation + CASE approach is the expected solution. It demonstrates that you know how to filter rows, compute counts, and derive conditional results directly in SQL. There is no meaningful brute-force alternative because SQL engines already optimize table scans and aggregations.

Solution

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Aggregation with COUNT and CASEO(n + m)O(1)Standard SQL approach when comparing filtered counts from two tables
Subqueries with Derived CountsO(n + m)O(1)Useful when computing counts separately and comparing them in a final SELECT

Video Solution

LeetCode 2072 Amazon Interview SQL Question with Detailed Explanation | Practice SQL • Everyday Data Science • 2,392 views views

Watch 1 more video solutions →

Frequently Asked Questions

Is The Winner University easy or hard?
The Winner University is classified as an Easy database problem. It focuses on basic SQL skills such as filtering rows, counting results, and returning a conditional value using CASE.
The Winner University Python/Java solution
The original problem is designed for SQL, typically solved in MySQL using COUNT and CASE expressions. In Python or Java, the equivalent logic would iterate through two datasets, count scores >= 90, and compare the totals to determine the winner.
How to solve The Winner University in O(n)?
Use two COUNT aggregations with a filtering condition such as score >= 90. Compute one count for each university and compare them using a CASE statement. Because each table is scanned only once, the overall complexity is linear in the number of rows.
What is the best approach for The Winner University?
The best approach uses SQL aggregation with COUNT and a CASE comparison. Count students with score >= 90 in each university table, then compare the counts to determine the winner. The query scans each table once, giving O(n + m) time complexity and constant extra space.
Is The Winner University asked at Google/Amazon/Meta?
Database aggregation and comparison problems appear frequently in interviews at companies like Amazon, Google, and Meta. While this exact question may not appear directly, the pattern of filtered counts and conditional results in SQL is commonly tested.
What data structure is used in The Winner University?
This problem relies on relational database tables and SQL aggregation functions rather than traditional in-memory data structures. The key operations are COUNT aggregation, filtering with conditions, and conditional logic using CASE.
What is the time complexity of The Winner University?
The time complexity is O(n + m), where n and m are the number of rows in the two university tables. Each table is scanned once to count students meeting the score condition. The final comparison using CASE is constant time.

Ready to solve this problem?

Practice The Winner University with our built-in code editor and test cases.

Practice on FleetCode