<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>
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.
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;
COUNT(DISTINCT t.subject_id) ensures only unique subject_ids are counted for each teacher, regardless of the department.GROUP BY t.teacher_id groups the data by teacher_id to calculate the unique subject count for each teacher.