-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 2356. Number of Unique Subjects Taught by Each Teacher
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
SQL_50/2356. Number of Unique Subjects Taught by Each Teacher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
|
||
|
||
|
||
|
||
Table: Teacher | ||
|
||
+-------------+------+ | ||
| Column Name | Type | | ||
+-------------+------+ | ||
| teacher_id | int | | ||
| subject_id | int | | ||
| dept_id | int | | ||
+-------------+------+ | ||
In SQL, (subject_id, dept_id) is the primary key for this table. | ||
Each row in this table indicates that the teacher with teacher_id teaches the subject subject_id in the department dept_id. | ||
|
||
|
||
Calculate the number of unique subjects each teacher teaches in the university. | ||
|
||
Return the result table in any order. | ||
|
||
The result format is shown in the following example. | ||
|
||
|
||
|
||
Example 1: | ||
|
||
Input: | ||
Teacher table: | ||
+------------+------------+---------+ | ||
| teacher_id | subject_id | dept_id | | ||
+------------+------------+---------+ | ||
| 1 | 2 | 3 | | ||
| 1 | 2 | 4 | | ||
| 1 | 3 | 3 | | ||
| 2 | 1 | 1 | | ||
| 2 | 2 | 1 | | ||
| 2 | 3 | 1 | | ||
| 2 | 4 | 1 | | ||
+------------+------------+---------+ | ||
Output: | ||
+------------+-----+ | ||
| teacher_id | cnt | | ||
+------------+-----+ | ||
| 1 | 2 | | ||
| 2 | 4 | | ||
+------------+-----+ | ||
Explanation: | ||
Teacher 1: | ||
- They teach subject 2 in departments 3 and 4. | ||
- They teach subject 3 in department 3. | ||
Teacher 2: | ||
- They teach subject 1 in department 1. | ||
- They teach subject 2 in department 1. | ||
- They teach subject 3 in department 1. | ||
- They teach subject 4 in department 1. | ||
|
||
----------------------------------------------------- | ||
# Write your MySQL query statement below | ||
select teacher_id,count(distinct(subject_id)) as cnt | ||
from teacher | ||
group by teacher_id; |