SELECT Name FROM Customers
WHERE CustomerId IN (SELECT CustomerId FROM Orders WHERE Amount > 1000);
Topics
43
Aggregate Functions & GROUP BY
Always On Availability Groups
Backup & Recovery
Change Data Capture (CDC)
Columnstore Indexes
Common Table Expressions (CTEs)
Constraints (Primary Key, Foreign Key, Check & Unique)
Cursors
Data Types & Schema Design
Deadlocks
Dynamic Data Masking
Dynamic SQL
Error Handling with TRY CATCH
Full Text Search
Hash Indexes & Hash Join Operations
Indexes
In-Memory OLTP (Memory-Optimized Tables)
Isolation & Locking
Joins
JSON Support in SQL Server
Linked Servers
Merge Statement (Upsert)
Normalization
Partitioning
Pivoting & Unpivoting Data
Query Optimization & Plans
Query Store
Replication
SQL Server Agent & Job Scheduling
SQL Server Architecture & Editions
SQL Server Profiler & Extended Events
SQL Server Security & Permissions
Stored Procedures & Functions
Subqueries
Temporary Tables & Table Variables
Transactions & ACID
Transparent Data Encryption
Triggers
T-SQL Fundamentals & Syntax
User Defined Functions
Views
Window Functions
XML Data Type & Querying
Subqueries
7 questions found
A subquery is a query nested inside another query, often used within a WHERE clause to filter results based on the outcome of that inner query, within a SELECT list to calculate a related value, or within a FROM clause to treat its result as if it were a temporary table for the outer query to work with.
Real-world example
A marketing report finds all customers who have placed at least one order over a thousand dollars, using a subquery to first identify the relevant customer ids before selecting their names in the outer query.
Joins;Common Table Expressions (CTEs)
A non correlated subquery runs independently and only once, producing a fixed result that the outer query then uses, while a correlated subquery references a column from the outer query, meaning it effectively runs once for each row processed by the outer query, since its result depends on the current row being evaluated.
-- Non correlated: runs once, independent of outer query
SELECT * FROM Customers WHERE CustomerId IN (SELECT CustomerId FROM Orders);
-- Correlated: references the outer query's current row
SELECT * FROM Customers c
WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.CustomerId = c.CustomerId);
Real-world example
A report finds customers with at least one order using a correlated subquery with EXISTS, which checks for a matching order specifically tied to each individual customer row being evaluated by the outer query.
Joins;Query Optimization & Plans
What is the difference between using EXISTS and IN when checking for the presence of related data with a subquery?
IntermediateEXISTS checks only whether the subquery returns any rows at all, stopping as soon as it finds a match, which can be more efficient for large data sets, while IN compares a specific value against the full list of values returned by the subquery, and can behave unexpectedly if that list happens to contain a NULL value, sometimes leading to surprisingly no results at all.
-- EXISTS often performs better and avoids NULL related surprises
SELECT * FROM Customers c
WHERE EXISTS (SELECT 1 FROM Orders o WHERE o.CustomerId = c.CustomerId);
Real-world example
A developer switches a slow query using IN with a large subquery to use EXISTS instead, both improving performance and avoiding a subtle bug caused by an unexpected NULL value in the subquery's results.
Joins;Data Types & Schema Design
How do you use a subquery within the SELECT list to calculate a related aggregate value alongside each row of the outer query?
IntermediateYou place a correlated subquery directly inside the SELECT list, referencing the outer query's current row, which calculates and returns a related value, such as a customer's total number of orders, computed individually for each row of the outer result set.
SELECT c.Name,
(SELECT COUNT(*) FROM Orders o WHERE o.CustomerId = c.CustomerId) AS OrderCount
FROM Customers c;
Real-world example
A customer report shows each customer's name alongside their total number of orders, calculated using a subquery in the SELECT list that runs once for each customer row in the outer query.
Aggregate Functions & GROUP BY;Joins
How would you rewrite a slow correlated subquery as an equivalent, potentially faster JOIN based query?
AdvancedYou identify the relationship being checked by the correlated subquery, and express that same logic using a JOIN combined with either GROUP BY for aggregation needs or DISTINCT for simple existence checks, which often allows SQL Server's optimizer to process the entire operation as a single efficient set based operation instead of repeatedly executing the subquery for each outer row.
-- Correlated subquery approach
SELECT c.Name, (SELECT COUNT(*) FROM Orders o WHERE o.CustomerId = c.CustomerId) AS OrderCount FROM Customers c;
-- Equivalent JOIN based approach
SELECT c.Name, COUNT(o.OrderId) AS OrderCount
FROM Customers c LEFT JOIN Orders o ON c.CustomerId = o.CustomerId
GROUP BY c.Name;
Real-world example
A team rewrites a slow report that used a correlated subquery to count each customer's orders into an equivalent LEFT JOIN with GROUP BY, cutting the report's execution time significantly on their large customer table.
Joins;Query Optimization & Plans
How does the SQL Server query optimizer typically handle and potentially transform a subquery internally when generating an execution plan?
AdvancedThe optimizer often transforms certain types of subqueries internally into equivalent join operations behind the scenes, especially simple non correlated ones, meaning the way you write a query does not always dictate exactly how it will be physically executed, though complex or correlated subqueries sometimes limit the optimizer's ability to make these kinds of beneficial transformations.
-- Examine the actual execution plan to see how
-- a subquery was physically implemented
SET STATISTICS XML ON;
SELECT * FROM Customers WHERE CustomerId IN (SELECT CustomerId FROM Orders);
Real-world example
A database administrator explains to a junior developer that a subquery they wrote was automatically converted into a join internally by the optimizer, which is why its performance was actually just as good as writing the join explicitly by hand.
Query Optimization & Plans;Joins
What are common mistakes developers make when writing subqueries that can lead to incorrect results or poor performance?
IntermediateCommon mistakes include using NOT IN with a subquery that might return NULL values, leading to unexpectedly empty results, writing a correlated subquery when a simple join would perform significantly better, and forgetting that a subquery returning multiple rows will cause an error if used in a context expecting only a single scalar value.
-- Problematic if OrderId can contain NULL
SELECT * FROM Customers WHERE CustomerId NOT IN (SELECT CustomerId FROM Orders WHERE CustomerId IS NOT NULL);
Real-world example
A developer debugging a report that mysteriously returned zero results discovers a NOT IN subquery was silently failing due to an unexpected NULL value, and fixes it by explicitly filtering out NULLs or switching to NOT EXISTS instead.
Data Types & Schema Design;Query Optimization & Plans