Question: what are cte used for?
Answer: A CTE (Common Table Expression) defines a temporary result set which you can then use in a SELECT statement. It becomes a convenient way to manage complicated queries. Common Table Expressions are defined within the statement using the WITH operator. You can define one or more common table expression in this fashion.