Let’s say you have a table called schools with the columns ```school_id```, ```school_name```, ```district_id```, and the number of students.
You need to write a query to display a list of schools alongside their district ID and the average number of students per school in that district.

Your logic might be as follows:

    Create a table with the list of districts and the corresponding average number of students per school.
    Join this table with the list of schools and display the required information.
    Drop the table with the average number of students per school for each district.

If you use a CTE (Common Table Expression), you don’t need to create and drop a table. You can simply reference the temporary result set created by the "WITH" query, like you see below:

```
WITH avg_students AS (
SELECT district_id, AVG(students) as average_students
FROM schools
GROUP BY district_id)
SELECT s.school_name, s.district_id, avg.average_students
FROM schools s
JOIN avg_students avg
ON s.district_id = avg.district_id;
```

So, here you start by defining the temporary result set ```avg_students``` in the "WITH" query. In the brackets, there is a "SELECT" statement that defines this result set; it contains a list of districts and the corresponding average number of students per school. After the closing parenthesis, you start the main "SELECT" statement. Notice that you reference the temporary result set just like an ordinary table, using its assigned name (```avg_students```). The output will include the school name, the ID of its district, and the average number of students in this district. 


| school_name | district_id | average_students |
| --- | --- | --- |
| Happy Kid | 2 | 238 |
| Smart | 2 | 238 |
| Sun | 5 | 176 |
| Montessori | 5 | 176 |


In the sense that it can’t be used in any other SQL queries, the temporary result set is “dropped”. Of course, you can define it again if you need to.
spam fails_task lang_mismatch pii not_appropriate hate_speech sexual_content quality toxicity humor helpfulness creativity violence