{ "data": { "question": { "questionId": "2495", "questionFrontendId": "2356", "boundTopicId": null, "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": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 323, "dislikes": 19, "isLiked": null, "similarQuestions": "[]", "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]]}}", "categoryTitle": "Database", "contributors": [], "topicTags": [ { "name": "Database", "slug": "database", "translatedName": null, "__typename": "TopicTagNode" } ], "companyTagStats": null, "codeSnippets": [ { "lang": "MySQL", "langSlug": "mysql", "code": "# Write your MySQL query statement below\n", "__typename": "CodeSnippetNode" }, { "lang": "MS SQL Server", "langSlug": "mssql", "code": "/* Write your T-SQL query statement below */\n", "__typename": "CodeSnippetNode" }, { "lang": "Oracle", "langSlug": "oraclesql", "code": "/* Write your PL/SQL query statement below */\n", "__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\n", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"79.2K\", \"totalSubmission\": \"91.6K\", \"totalAcceptedRaw\": 79225, \"totalSubmissionRaw\": 91592, \"acRate\": \"86.5%\"}", "hints": [], "solution": { "id": "2014", "canSeeDetail": false, "paidOnly": true, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "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, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"mysql\": [\"MySQL\", \"
MySQL 8.0
.
mssql server 2019
.
Oracle Sql 11.2
.
Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0
\"], \"postgresql\": [\"PostgreSQL\", \"PostgreSQL 16
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }