Performance
Readability
One of the Common Table Expressions (CTEs) that the query defines is not used anywhere else in the query.
Example
The CTE sum_students has been defined but is not referenced in other parts of the query. Only avg_students has been referenced in the query
WITH avg_students AS(
SELECT district_id
,AVG(fee) AS average_students
FROM schools
GROUP BY district_id)
,sum_students AS(
SELECT district_id
,SUM(fee) AS sum_students
FROM schools
GROUP BY district_id)
SELECT s.school_name
,s.district_id
,avg.average_students
FROM schools s
JOIN avg_students avgs
ON s.district_id=avgs.district_id
There are two issues:
Remove CTEs that aren’t being used, or at least comment them out for future use.
None.