Leetcode Problem 2356. Number of Unique Subjects Taught by Each Teacher

2356. Number of Unique Subjects Taught by Each Teacher

Leetcode Solutions

Counting Unique Subjects Taught by Each Teacher

Algorithm

  1. Use the GROUP BY clause to group records by teacher_id.
  2. Apply the COUNT(DISTINCT subject_id) function to count the unique subjects taught by each teacher.
  3. Select teacher_id and the count from step 2 as cnt to match the expected output format.
  4. Execute the SQL query to obtain the result.

For the Pandas approach:

  1. Group the DataFrame by teacher_id using the groupby() function.
  2. Apply the nunique() function to the subject_id column to count unique subjects.
  3. Rename the resulting series to cnt to match the expected output format.
  4. Reset the index of the DataFrame to turn the grouped data back into a DataFrame.

erDiagram
    Teacher {
        int teacher_id
        int subject_id
        int dept_id
        subject_id PK ""
        dept_id PK ""
    }

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...