0
0
SQLquery~10 mins

Subquery with EXISTS operator in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subquery with EXISTS operator
Start Outer Query
For each row in Outer Table
Run Subquery
Does Subquery return any row?
Yes No
Include row
Next row or End
The outer query checks each row and runs the subquery. If the subquery finds any matching row, EXISTS returns true and includes the outer row.
Execution Sample
SQL
SELECT c.customer_id
FROM customers c
WHERE EXISTS (
  SELECT 1 FROM orders o WHERE o.customer_id = c.customer_id
);
Select customers who have at least one order by checking if the subquery finds matching orders.
Execution Table
StepOuter customer_idSubquery ResultEXISTS ResultOuter Query Action
1101Returns rows (orders found)TRUEInclude customer 101
2102Returns no rows (no orders)FALSEExclude customer 102
3103Returns rows (orders found)TRUEInclude customer 103
4104Returns no rows (no orders)FALSEExclude customer 104
5105Returns rows (orders found)TRUEInclude customer 105
6---No more customers, query ends
💡 All customers checked; only those with orders included because EXISTS was TRUE
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
customer_id (outer)101102103104105--
EXISTS ResultTRUEFALSETRUEFALSETRUE--
Key Moments - 2 Insights
Why does the EXISTS subquery only check for presence of rows, not their content?
EXISTS only cares if the subquery returns any row at all. It returns TRUE if at least one row exists, regardless of what data is inside. See execution_table rows 1 and 2 where subquery returns rows or no rows.
What happens if the subquery returns multiple rows for one outer row?
EXISTS still returns TRUE as soon as it finds the first matching row. It does not count or return multiple rows. This is why in execution_table row 1, multiple orders still result in TRUE.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the EXISTS result for customer_id 104?
ATRUE
BFALSE
CNo result
DError
💡 Hint
Check the row where Outer customer_id is 104 in the execution_table under EXISTS Result column
At which step does the outer query exclude a customer because no orders exist?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look for 'Exclude customer' in the Outer Query Action column in execution_table
If the subquery always returned rows, what would the EXISTS result column look like?
AAll FALSE
BAlternating TRUE and FALSE
CAll TRUE
DNo values
💡 Hint
EXISTS returns TRUE if subquery returns any rows; see variable_tracker for EXISTS Result values
Concept Snapshot
Subquery with EXISTS operator:
- Syntax: WHERE EXISTS (subquery)
- Checks if subquery returns any rows
- Returns TRUE if at least one row exists
- Used to filter outer query rows
- Efficient for existence checks without data retrieval
Full Transcript
This visual execution shows how the SQL EXISTS operator works with a subquery. For each customer in the outer query, the subquery checks if there are any orders for that customer. If the subquery returns any rows, EXISTS returns TRUE and the customer is included in the result. If no rows are found, EXISTS returns FALSE and the customer is excluded. The execution table traces each customer_id, the subquery result, the EXISTS boolean, and the action taken. The variable tracker shows how customer_id and EXISTS result change step-by-step. Key moments clarify that EXISTS only checks for presence of rows, not their content, and that multiple rows still result in TRUE. The quiz tests understanding of these steps and outcomes. This helps beginners see how EXISTS filters rows based on subquery existence.