0
0
PostgreSQLquery~10 mins

Subqueries in WHERE with IN in PostgreSQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Subqueries in WHERE with IN
Start Query
Execute Subquery
Get List of Values
Main Query WHERE checks if value IN list
Include
Return Rows
End
The main query runs a subquery first to get a list of values. Then it checks each row if a column's value is in that list. Rows matching are returned.
Execution Sample
PostgreSQL
SELECT name FROM employees
WHERE department_id IN (
  SELECT id FROM departments WHERE location = 'NY'
);
This query finds employees who work in departments located in NY by using a subquery inside WHERE with IN.
Execution Table
StepActionSubquery ResultMain Query RowCondition CheckInclude Row?
1Run subquery[1, 3]N/AN/AN/A
2Check employee 1N/Adepartment_id=11 IN [1,3] = TrueYes
3Check employee 2N/Adepartment_id=22 IN [1,3] = FalseNo
4Check employee 3N/Adepartment_id=33 IN [1,3] = TrueYes
5Check employee 4N/Adepartment_id=44 IN [1,3] = FalseNo
6EndN/AN/AN/AN/A
💡 All employees checked; rows included only if department_id is in subquery result [1,3].
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
subquery_resultempty[1,3][1,3][1,3][1,3][1,3][1,3]
current_employee_department_idN/AN/A1234N/A
include_rowN/AN/AYesNoYesNoN/A
Key Moments - 3 Insights
Why do we run the subquery first before checking each employee?
The subquery runs first to get the list of department IDs in NY. Then the main query checks if each employee's department_id is in that list (see execution_table step 1).
What happens if an employee's department_id is not in the subquery result?
If the department_id is not in the list, the condition is false and that employee is excluded (see execution_table steps 3 and 5).
Can the subquery return multiple values?
Yes, the subquery returns a list of values (like [1,3]) which the IN operator uses to check membership (see execution_table step 1).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the subquery result after step 1?
A[1, 3]
B[2, 4]
C[1, 2, 3]
D[]
💡 Hint
Check the 'Subquery Result' column in execution_table row with Step 1.
At which step does the condition check fail for an employee?
AStep 2
BStep 3
CStep 4
DStep 6
💡 Hint
Look at the 'Condition Check' and 'Include Row?' columns in execution_table.
If the subquery returned [2,4] instead, which employee would be included at step 3?
AEmployee with department_id=1
BEmployee with department_id=3
CEmployee with department_id=2
DEmployee with department_id=4
💡 Hint
Refer to variable_tracker for department_id values and subquery_result.
Concept Snapshot
Subqueries in WHERE with IN:
- Subquery runs first, returns list of values.
- Main query checks if column value is IN that list.
- Rows matching condition are included.
- Useful to filter based on related table values.
- Syntax: WHERE column IN (subquery).
Full Transcript
This visual execution shows how a SQL query with a subquery in the WHERE clause using IN works. First, the subquery runs and returns a list of department IDs located in NY. Then, the main query checks each employee's department_id to see if it is in that list. If yes, the employee's name is included in the result. The execution table traces each step: running the subquery, checking each employee, and deciding inclusion. The variable tracker shows how the subquery result and current employee's department_id change during execution. Key moments clarify why the subquery runs first, what happens when the condition fails, and that the subquery can return multiple values. The quiz tests understanding of the subquery result, condition checks, and how changing the subquery affects included rows.