0
0
PostgreSQLquery~10 mins

Subqueries with EXISTS in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subqueries with EXISTS
Start Outer Query
For each row in Outer Table
Run Subquery
Check if Subquery returns any row
Include Outer Row
Next Outer Row or End
The outer query checks each row, runs the subquery, and includes the row only if the subquery finds any matching rows.
Execution Sample
PostgreSQL
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 any matching orders.
Execution Table
StepOuter Row (customer_id)Subquery RunSubquery ResultEXISTS ConditionOuter Row Included
1101SELECT 1 FROM orders WHERE customer_id=101Returns rowsTrueYes
2102SELECT 1 FROM orders WHERE customer_id=102Returns no rowsFalseNo
3103SELECT 1 FROM orders WHERE customer_id=103Returns rowsTrueYes
4104SELECT 1 FROM orders WHERE customer_id=104Returns no rowsFalseNo
5105SELECT 1 FROM orders WHERE customer_id=105Returns rowsTrueYes
6End----
💡 All outer rows checked; query ends.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
customer_idNone101102103104105End
EXISTS ResultNoneTrueFalseTrueFalseTrueEnd
Key Moments - 2 Insights
Why does the EXISTS subquery only check for presence of rows, not their content?
EXISTS returns True as soon as the subquery finds any row, so it doesn't matter what the row contains, only if it exists (see execution_table rows 1 and 2).
What happens if the subquery returns no rows for a customer?
The EXISTS condition is False, so the outer query skips that customer (see execution_table rows 2 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the EXISTS condition for customer_id 103?
AFalse
BNo rows returned
CTrue
DNot checked
💡 Hint
Check the row where Outer Row is 103 in the execution_table.
At which step does the subquery return no rows, causing the outer row to be skipped?
AStep 2
BStep 1
CStep 3
DStep 5
💡 Hint
Look for 'Returns no rows' in the Subquery Result column.
If the subquery always returned rows, what would happen to the outer query results?
AOnly some outer rows would be included
BAll outer rows would be included
CNo outer rows would be included
DQuery would error
💡 Hint
EXISTS True means include the outer row; if always True, all rows included.
Concept Snapshot
Subqueries with EXISTS:
- Syntax: WHERE EXISTS (subquery)
- Checks if subquery returns any rows
- Returns True if at least one row found
- Outer row included only if EXISTS is True
- Efficient for existence checks without fetching data
Full Transcript
This visual execution shows how a SQL query with EXISTS works. For each customer in the outer query, the subquery checks if there is at least one order for that customer. If the subquery finds any matching order, EXISTS returns True, and the customer is included in the result. If no orders are found, EXISTS returns False, and the customer is skipped. The execution table traces each customer_id, the subquery run, its result, and whether the customer is included. The variable tracker shows how customer_id and EXISTS result change step by step. Key moments clarify that EXISTS only cares about presence of rows, not their content, and that no rows means skipping the outer row. The quiz tests understanding of these steps and outcomes.