<aside> 💡

Project Overview:

This project demonstrates the ability to use aggregate functions and grouping to analyze relational data. The objective was to determine how many unique subjects each teacher teaches across all departments in a university.

</aside>


Problem Description:

The Teacher table contains the following information:

The combination of subject_id and dept_id forms the primary key, ensuring that a subject taught in a specific department is unique.

The task was to calculate the number of distinct subjects taught by each teacher across all departments and return the results in any order.


Solution:

The query aggregates data to calculate the count of distinct subjects (subject_id) for each teacher (teacher_id).

SELECT t.teacher_id, COUNT(DISTINCT t.subject_id) AS cnt
FROM Teacher t
GROUP BY t.teacher_id;


Explanation:

  1. Counting Unique Subjects:
  2. Grouping:

Example Input and Output:

Input: