Great. Now, there is an alternative CTE syntax where we define the columns explicitly:
WITH some_name (cte_columns) AS ( your_cte )
In other words, we now have two pairs of parentheses. First, we provide the names of columns that our CTE will have. Second, we define the actual CTE based on the columns we provided.
WITH project_revenue (id, sum_amount) AS (
ON donation.project_id = project.id
GROUP BY project.id
The columns definition is not required. Why would we use it, then? For one thing, it increases the readability of your query. For another, while simple columns inside CTEs don't require aliases, aggregates and other function results do. Columns like
COUNT(project_id) must be given names so that you can refer to them outside CTEs. One way is to use the keyword
AS, just as we did previously. Another way is to provide the list of columns. Either method is fine, but if you use none – an error will occur.