To retrieve each school's ID and name along with the counts of related students, employees, and classes in Transact-SQL (T-SQL), you can use several approaches. However, it's crucial to handle joins carefully to avoid incorrect inflated counts due to multiple LEFT JOIN operations. Below are two effective methods to achieve this:
-
Using Separate Subqueries:
This method involves querying each child table separately to count related records and then joining these counts to the parent table (Schools). This approach ensures accurate counts without the risk of row multiplication.
SELECT
s.SchoolID,
s.Name,
ISNULL(st.StudentCount, 0) AS StudentCount,
ISNULL(e.EmployeeCount, 0) AS EmployeeCount,
ISNULL(c.ClassCount, 0) AS ClassCount
FROM
Schools s
LEFT JOIN (
SELECT
SchoolID,
COUNT(*) AS StudentCount
FROM
Students
GROUP BY
SchoolID
) st ON s.SchoolID = st.SchoolID
LEFT JOIN (
SELECT
SchoolID,
COUNT(*) AS EmployeeCount
FROM
Employees
GROUP BY
SchoolID
) e ON s.SchoolID = e.SchoolID
LEFT JOIN (
SELECT
SchoolID,
COUNT(*) AS ClassCount
FROM
Classes
GROUP BY
SchoolID
) c ON s.SchoolID = c.SchoolID
ORDER BY
s.SchoolID;
Explanation:
- Main Table (
Schools): Start with the Schools table as the primary source.
- Subqueries:
- Students (
st): Counts the number of students per SchoolID.
- Employees (
e): Counts the number of employees per SchoolID.
- Classes (
c): Counts the number of classes per SchoolID.
- LEFT JOINs: Each subquery is left-joined to ensure that schools without related records still appear with a count of zero.
- ISNULL Function: Replaces
NULL values with 0 for schools that have no related records in a child table.
- ORDER BY: Sorts the results by
SchoolID for better readability.
-
Using OUTER APPLY with Aggregations:
Alternatively, you can use the OUTER APPLY operator to perform correlated subqueries for each child table. This method is equally effective and can sometimes offer performance benefits depending on the database schema and indexes.
SELECT
s.SchoolID,
s.Name,
ISNULL(st.StudentCount, 0) AS StudentCount,
ISNULL(e.EmployeeCount, 0) AS EmployeeCount,
ISNULL(c.ClassCount, 0) AS ClassCount
FROM
Schools s
OUTER APPLY (
SELECT COUNT(*) AS StudentCount
FROM Students st
WHERE st.SchoolID = s.SchoolID
) st
OUTER APPLY (
SELECT COUNT(*) AS EmployeeCount
FROM Employees e
WHERE e.SchoolID = s.SchoolID
) e
OUTER APPLY (
SELECT COUNT(*) AS ClassCount
FROM Classes c
WHERE c.SchoolID = s.SchoolID
) c
ORDER BY
s.SchoolID;
Explanation:
OUTER APPLY: Acts similarly to a LEFT JOIN but allows you to reference the outer query (Schools s) within the subquery.
- Subqueries: Each
OUTER APPLY performs a count of related records in the respective child tables based on the current SchoolID.
- ISNULL Function: Ensures that if a subquery returns
NULL (i.e., no related records), it is displayed as 0.
- ORDER BY: Sorts the results by
SchoolID.
-
Using COUNT with Conditional Aggregation:
If performance is a concern and you prefer a single scan through each child table, you can use conditional aggregation with LEFT JOINs. However, be cautious as this method can become complex with more child tables and might still face issues with row multiplication.
SELECT
s.SchoolID,
s.Name,
COUNT(DISTINCT st.StudentID) AS StudentCount,
COUNT(DISTINCT e.EmployeeID) AS EmployeeCount,
COUNT(DISTINCT c.ClassID) AS ClassCount
FROM
Schools s
LEFT JOIN
Students st ON s.SchoolID = st.SchoolID
LEFT JOIN
Employees e ON s.SchoolID = e.SchoolID
LEFT JOIN
Classes c ON s.SchoolID = c.SchoolID
GROUP BY
s.SchoolID, s.Name
ORDER BY
s.SchoolID;
Caveat:
- Row Multiplication: Joining multiple child tables directly can lead to an inflated number of rows due to the Cartesian product of related records. Using
COUNT(DISTINCT ...) helps mitigate this but can impact performance, especially with large datasets.
This result set provides each school's ID and name along with the respective counts of students, employees, and classes.