File tree
Expand file treeCollapse file tree1 file changed
+57
-0
lines changed Expand file treeCollapse file tree1 file changed
+57
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
|
| 1 | +-- Problem: |
| 2 | + |
| 3 | +/* |
| 4 | +Table: Courses |
| 5 | +
|
| 6 | ++-------------+---------+ |
| 7 | +| Column Name | Type | |
| 8 | ++-------------+---------+ |
| 9 | +| student | varchar | |
| 10 | +| class | varchar | |
| 11 | ++-------------+---------+ |
| 12 | +(student, class) is the primary key (combination of columns with unique values) for this table. |
| 13 | +Each row of this table indicates the name of a student and the class in which they are enrolled. |
| 14 | +
|
| 15 | +
|
| 16 | +Write a solution to find all the classes that have at least five students. |
| 17 | +
|
| 18 | +Return the result table in any order. |
| 19 | +
|
| 20 | +The result format is in the following example. |
| 21 | +
|
| 22 | +
|
| 23 | +
|
| 24 | +Example 1: |
| 25 | +
|
| 26 | +Input: |
| 27 | +Courses table: |
| 28 | ++---------+----------+ |
| 29 | +| student | class | |
| 30 | ++---------+----------+ |
| 31 | +| A | Math | |
| 32 | +| B | English | |
| 33 | +| C | Math | |
| 34 | +| D | Biology | |
| 35 | +| E | Math | |
| 36 | +| F | Computer | |
| 37 | +| G | Math | |
| 38 | +| H | Math | |
| 39 | +| I | Math | |
| 40 | ++---------+----------+ |
| 41 | +Output: |
| 42 | ++---------+ |
| 43 | +| class | |
| 44 | ++---------+ |
| 45 | +| Math | |
| 46 | ++---------+ |
| 47 | +Explanation: |
| 48 | +- Math has 6 students, so we include it. |
| 49 | +- English has 1 student, so we do not include it. |
| 50 | +- Biology has 1 student, so we do not include it. |
| 51 | +- Computer has 1 student, so we do not include it. |
| 52 | +*/ |
| 53 | + |
| 54 | +------------------------------------------------------------------------------- |
| 55 | + |
| 56 | +-- Solution: |
| 57 | + |
1 | 58 | SELECT class FROM Courses
|
2 | 59 | GROUP BY class
|
3 | 60 | HAVING COUNT(student)>=5
|
You can’t perform that action at this time.
0 commit comments