SQL Server Interview Question #70
What is a subquery?
Joins, Subqueries, CTEs & Set Operators Mid-Level Intermediate
Detailed Explanation
A subquery is a query nested inside another SQL statement. It can appear in SELECT, FROM, WHERE, HAVING, INSERT, UPDATE, DELETE, and other contexts.
Subqueries can be scalar, multi-row, correlated, or used as derived tables. The optimizer may transform logically equivalent joins and subqueries into similar execution strategies, so performance should be evaluated from the actual plan rather than syntax alone.
Code Example
SELECT Id, Name, Price
FROM dbo.Products
WHERE Price >
(
SELECT AVG(Price)
FROM dbo.Products
);