If you use queries in SQL, you could involve complex statements that could cause some maintenance or reading problems. You can still do them, but you can choose a new way, CTE
.
What is CTE?
The common table expression (CTE) is a powerful construct in SQL that helps simplify a query.
With CTE, you can split the query into different temporary tables and use them.
Example
If I want to create a report about the total amount of the user’s order, I could use CTE to simplify it.
[dbo].[Orders]
1 | OrderNumber UserId Amount |
[dbo].[Users]
1 | Id Name Age |
Then I use the CTE to create a temp table and join them.
Result:
1 | Id Name Age OrderTotalAmount |
SQL:
1 | USE OrderDB |
Now, if I need to create complex reports, summaries or stored procedures , I will use CTE to help me.