Scholarship Shortlist by CGPA or Hackathon Wins — SQL Easy Problem
The training and placement cell is short-listing students for a merit scholarship.
- Difficulty: Easy
- Topics: Basics
- Dialect: MySQL
- Problem: #1
Problem statement
The training and placement cell is short-listing students for a merit scholarship. A student qualifies if their cgpa is at least 9.0, or if they have won at least 2 hackathons — or both.
Return the student_id and name of every qualifying student, each student once, in any order.
Tables
Table: Student
| Column | Type |
|---|---|
| student_id | int |
| name | varchar |
| branch | varchar |
| cgpa | decimal |
| hackathon_wins | int |
Primary key: student_id.
cgpa is on a 10-point scale with two decimals; hackathon_wins counts inter-college hackathons won. Neither column is ever NULL.
Examples
Example 1
Student
| student_id | name | branch | cgpa | hackathon_wins |
|---|---|---|---|---|
| 1 | Aarav | CSE | 9.12 | 0 |
| 2 | Diya | ECE | 8.4 | 3 |
| 3 | Kabir | ME | 8.95 | 1 |
| 4 | Meera | CSE | 9 | 2 |
| 5 | Rohan | IT | 7.8 | 0 |
| 6 | Sneha | EEE | 8.1 | 2 |
| 7 | Vikram | CSE | 6.95 | 1 |
Output
| student_id | name |
|---|---|
| 1 | Aarav |
| 2 | Diya |
| 4 | Meera |
| 6 | Sneha |
How to solve Scholarship Shortlist by CGPA or Hackathon Wins
A student qualifies through either of two independent conditions, so the whole query is one WHERE with OR: cgpa >= 9.0 OR hackathon_wins >= 2. A student who meets both is still a single row — WHERE keeps or drops each row once and never duplicates it — so no DISTINCT is needed.
The words "at least" decide the operators. A CGPA of exactly 9.00, or exactly two wins, must qualify, so both comparisons are >=. Writing > silently drops the students sitting on the boundary, which is the most common wrong answer here.
Another way to read the rule is as two lists glued together: the students with a high CGPA, UNION the students with enough wins. UNION (not UNION ALL) removes the students who appear in both lists. It reads the table twice, so the single OR filter is the cheaper choice — one scan, linear in the size of the table.
By De Morgan's law the rule can also be written as NOT (cgpa < 9.0 AND hackathon_wins < 2): a student is left out only when both numbers are too low. That rewrite is safe here only because neither column is ever NULL — with NULLs, the negated form and the original can disagree.
Reference solution (MySQL)
SELECT student_id, name
FROM Student
WHERE cgpa >= 9.0 OR hackathon_wins >= 2Another way
SELECT student_id, name FROM Student WHERE cgpa >= 9.0 UNION SELECT student_id, name FROM Student WHERE hackathon_wins >= 2Another way
SELECT student_id, name FROM Student WHERE NOT (cgpa < 9.0 AND hackathon_wins < 2)Another way
SELECT student_id, name FROM Student WHERE CASE WHEN cgpa >= 9.0 THEN 1 WHEN hackathon_wins >= 2 THEN 1 ELSE 0 END = 1← All SQL problems · Canteen Orders Not Placed With the FEST50 Coupon →