![]() ![]() The following is an example showing both a subquery SELECT and a join SELECT that return the same result set and execution plan: USE AdventureWorks2016 In such cases, a join approach would yield better results. Otherwise, the nested query must be processed for each result of the outer query to ensure elimination of duplicates. For architectural information on how SQL Server processes queries, see SQL statement processing.However, in some cases where existence must be checked, a join yields better performance. In Transact-SQL, there is usually no performance difference between a statement that includes a subquery and a semantically equivalent version that does not. Other questions can be posed only with subqueries. Many Transact-SQL statements that include subqueries can be alternatively formulated as joins. WHERE Ord.SalesOrderID = OrdDet.SalesOrderID) AS MaxUnitPriceĪ subquery is also called an inner query or inner select, while the statement containing a subquery is also called an outer query or outer select. In this example a subquery is used as a column expression named MaxUnitPrice in a SELECT statement. The samples in this article use the AdventureWorks2016 database available for download at AdventureWorks sample databases.Ī subquery can be used anywhere an expression is allowed.
0 Comments
Leave a Reply. |