{ "data": { "question": { "questionId": "2495", "questionFrontendId": "2356", "categoryTitle": "Database", "boundTopicId": 1706030, "title": "Number of Unique Subjects Taught by Each Teacher", "titleSlug": "number-of-unique-subjects-taught-by-each-teacher", "content": "
Table: Teacher
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| teacher_id | int |\n| subject_id | int |\n| dept_id | int |\n+-------------+------+\n(subject_id, dept_id) is the primary key (combinations of columns with unique values) of this table.\nEach row in this table indicates that the teacher with teacher_id teaches the subject subject_id in the department dept_id.\n\n\n
\n\n
Write a solution to calculate the number of unique subjects each teacher teaches in the university.
\n\nReturn the result table in any order.
\n\nThe result format is shown in the following example.
\n\n\n
Example 1:
\n\n\nInput: \nTeacher table:\n+------------+------------+---------+\n| teacher_id | subject_id | dept_id |\n+------------+------------+---------+\n| 1 | 2 | 3 |\n| 1 | 2 | 4 |\n| 1 | 3 | 3 |\n| 2 | 1 | 1 |\n| 2 | 2 | 1 |\n| 2 | 3 | 1 |\n| 2 | 4 | 1 |\n+------------+------------+---------+\nOutput: \n+------------+-----+\n| teacher_id | cnt |\n+------------+-----+\n| 1 | 2 |\n| 2 | 4 |\n+------------+-----+\nExplanation: \nTeacher 1:\n - They teach subject 2 in departments 3 and 4.\n - They teach subject 3 in department 3.\nTeacher 2:\n - They teach subject 1 in department 1.\n - They teach subject 2 in department 1.\n - They teach subject 3 in department 1.\n - They teach subject 4 in department 1.\n\n", "translatedTitle": "每位教师所教授的科目种类的数量", "translatedContent": "
表: Teacher
\n+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| teacher_id | int |\n| subject_id | int |\n| dept_id | int |\n+-------------+------+\n在 SQL 中,(subject_id, dept_id) 是该表的主键。\n该表中的每一行都表示带有 teacher_id 的教师在系 dept_id 中教授科目 subject_id。\n\n\n
\n\n
查询每位老师在大学里教授的科目种类的数量。
\n\n以 任意顺序 返回结果表。
\n\n查询结果格式示例如下。
\n\n\n\n
示例 1:
\n\n\n输入: \nTeacher 表:\n+------------+------------+---------+\n| teacher_id | subject_id | dept_id |\n+------------+------------+---------+\n| 1 | 2 | 3 |\n| 1 | 2 | 4 |\n| 1 | 3 | 3 |\n| 2 | 1 | 1 |\n| 2 | 2 | 1 |\n| 2 | 3 | 1 |\n| 2 | 4 | 1 |\n+------------+------------+---------+\n输出: \n+------------+-----+\n| teacher_id | cnt |\n+------------+-----+\n| 1 | 2 |\n| 2 | 4 |\n+------------+-----+\n解释: \n教师 1:\n - 他在 3、4 系教科目 2。\n - 他在 3 系教科目 3。\n教师 2:\n - 他在 1 系教科目 1。\n - 他在 1 系教科目 2。\n - 他在 1 系教科目 3。\n - 他在 1 系教科目 4。\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 19, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [ { "name": "Database", "slug": "database", "translatedName": "数据库", "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */", "__typename": "CodeSnippetNode" }, { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef count_unique_subjects(teacher: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" }, { "lang": "PostgreSQL", "langSlug": "postgresql", "code": "-- Write your PostgreSQL query statement below", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"16.4K\", \"totalSubmission\": \"19.7K\", \"totalAcceptedRaw\": 16358, \"totalSubmissionRaw\": 19708, \"acRate\": \"83.0%\"}", "hints": [], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"Teacher\":[\"teacher_id\",\"subject_id\",\"dept_id\"]},\"rows\":{\"Teacher\":[[1,2,3],[1,2,4],[1,3,3],[2,1,1],[2,2,1],[2,3,1],[2,4,1]]}}", "metaData": "{\"mysql\":[\"Create table If Not Exists Teacher (teacher_id int, subject_id int, dept_id int)\"],\"mssql\":[\"Create table Teacher (teacher_id int, subject_id int, dept_id int)\"],\"oraclesql\":[\"Create table Teacher (teacher_id int, subject_id int, dept_id int)\"],\"database\":true,\"name\":\"count_unique_subjects\",\"pythondata\":[\"Teacher = pd.DataFrame([], columns=['teacher_id', 'subject_id', 'dept_id']).astype({'teacher_id':'Int64', 'subject_id':'Int64', 'dept_id':'Int64'})\"],\"postgresql\":[\"Create table If Not Exists Teacher (teacher_id int, subject_id int, dept_id int)\"],\"database_schema\":{\"Teacher\":{\"teacher_id\":\"INT\",\"subject_id\":\"INT\",\"dept_id\":\"INT\"}}}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [ "Create table If Not Exists Teacher (teacher_id int, subject_id int, dept_id int)", "Truncate table Teacher", "insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '3')", "insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '2', '4')", "insert into Teacher (teacher_id, subject_id, dept_id) values ('1', '3', '3')", "insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '1', '1')", "insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '2', '1')", "insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '3', '1')", "insert into Teacher (teacher_id, subject_id, dept_id) values ('2', '4', '1')" ], "enableRunCode": true, "envInfo": "{\"mysql\":[\"MySQL\",\"
\\u7248\\u672c\\uff1a mssql server 2019.<\\/p>\"],\"oraclesql\":[\"Oracle\",\" Oracle Sql 11.2.<\\/p>\"],\"pythondata\":[\"Pandas\",\" Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"],\"postgresql\":[\"PostgreSQL\",\" PostgreSQL 16<\\/p>\"]}",
"book": null,
"isSubscribed": false,
"isDailyQuestion": false,
"dailyRecordStatus": null,
"editorType": "CKEDITOR",
"ugcQuestionId": null,
"style": "LEETCODE",
"exampleTestcases": "{\"headers\":{\"Teacher\":[\"teacher_id\",\"subject_id\",\"dept_id\"]},\"rows\":{\"Teacher\":[[1,2,3],[1,2,4],[1,3,3],[2,1,1],[2,2,1],[2,3,1],[2,4,1]]}}",
"__typename": "QuestionNode"
}
}
}MySQL 8.0<\\/code><\\/p>\"],\"mssql\":[\"MS SQL Server\",\"