In addition to subquery, we can use Common Table Expression (CTE) with the same result and also neater code.
1 2 3 4 5 6 7 8 |
CREATE TABLE #temp (sales INT) INSERT INTO #temp VALUES (100),(200),(300),(400),(500) SELECT * FROM #temp |
1 2 3 4 5 6 7 |
sales ----------- 100 200 300 400 500 |
1 2 3 4 5 6 |
WITH cte_table AS ( select * from #temp where sales >= 200 ) select * from cte_table |
1 2 3 4 5 6 |
sales ----------- 200 300 400 500 |
cte_table is unlike #temp in which you can recall anytime. With cte_table , the query that refers to it must be run at the same time as its creation. But… it does look better than subquery.
1 2 3 4 5 |
select * from ( select * from #temp where sales > = 200 ) as temp |