File tree 2 files changed +59
-0
lines changed
2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change 493
493
| 1683 | Invalid Tweets | [ MySQL] ( ./database/1683-invalid-tweets.sql ) | Easy |
494
494
| 1757 | Recyclable and Low Fat Products | [ MySQL] ( ./database/1757-recyclable-and-low-fat-products.sql ) | Easy |
495
495
| 1934 | Confirmation Rate | [ MySQL] ( ./database/1934-confirmation-rate.sql ) | Medium |
496
+ | 2356 | Confirmation Rate | [ MySQL] ( ./database/2356-number-of-unique-subjects-taught-by-each-teacher.sql ) | Easy |
496
497
497
498
### Shell
498
499
| # | Title | Solution | Difficulty |
Original file line number Diff line number Diff line change
1
+ -- 2356. Number of Unique Subjects Taught by Each Teacher
2
+ -- Easy
3
+ -- https://leetcode.com/problems/number-of-unique-subjects-taught-by-each-teacher
4
+
5
+ /*
6
+ Table: Teacher
7
+ +-------------+------+
8
+ | Column Name | Type |
9
+ +-------------+------+
10
+ | teacher_id | int |
11
+ | subject_id | int |
12
+ | dept_id | int |
13
+ +-------------+------+
14
+ (subject_id, dept_id) is the primary key (combinations of columns with unique values) of this table.
15
+ Each row in this table indicates that the teacher with teacher_id teaches the subject subject_id in the department dept_id.
16
+
17
+ Write a solution to calculate the number of unique subjects each teacher teaches in the university.
18
+ Return the result table in any order.
19
+ The result format is shown in the following example.
20
+
21
+ Example 1:
22
+ Input:
23
+ Teacher table:
24
+ +------------+------------+---------+
25
+ | teacher_id | subject_id | dept_id |
26
+ +------------+------------+---------+
27
+ | 1 | 2 | 3 |
28
+ | 1 | 2 | 4 |
29
+ | 1 | 3 | 3 |
30
+ | 2 | 1 | 1 |
31
+ | 2 | 2 | 1 |
32
+ | 2 | 3 | 1 |
33
+ | 2 | 4 | 1 |
34
+ +------------+------------+---------+
35
+ Output:
36
+ +------------+-----+
37
+ | teacher_id | cnt |
38
+ +------------+-----+
39
+ | 1 | 2 |
40
+ | 2 | 4 |
41
+ +------------+-----+
42
+ Explanation:
43
+ Teacher 1:
44
+ - They teach subject 2 in departments 3 and 4.
45
+ - They teach subject 3 in department 3.
46
+ Teacher 2:
47
+ - They teach subject 1 in department 1.
48
+ - They teach subject 2 in department 1.
49
+ - They teach subject 3 in department 1.
50
+ - They teach subject 4 in department 1.
51
+ */
52
+
53
+ SELECT
54
+ teacher_id, COUNT (DISTINCT subject_id) as cnt
55
+ FROM
56
+ Teacher
57
+ GROUP BY
58
+ teacher_id;
You can’t perform that action at this time.
0 commit comments