Discover how a simple subquery can save you hours of tedious checking!
Why Subqueries in WHERE with IN in PostgreSQL? - Purpose & Use Cases
Imagine you have a big list of customers and a separate list of orders. You want to find all customers who have made at least one order. Doing this by hand means checking each customer against every order one by one.
Manually comparing each customer to every order is slow and tiring. It's easy to miss some matches or make mistakes, especially when the lists are large. This wastes time and causes errors.
Using a subquery with IN lets you ask the database to find all customers whose IDs appear in the orders list automatically. This saves time and avoids mistakes by letting the database do the hard work.
SELECT * FROM customers WHERE id = 1 OR id = 2 OR id = 3;
SELECT * FROM customers WHERE id IN (SELECT customer_id FROM orders);
This lets you quickly find related data across tables without writing long, complicated checks.
A store manager wants to see all customers who placed orders this month. Instead of checking each customer manually, they use a subquery with IN to get the list instantly.
Manually checking matches is slow and error-prone.
Subqueries with IN let the database find matches automatically.
This makes queries simpler, faster, and more reliable.
Practice
IN keyword do in a SQL query when used with a subquery in the WHERE clause?Solution
Step 1: Understand the role of
TheINwith subqueriesINkeyword checks if a value exists in a list of values returned by the subquery.Step 2: Identify the filtering effect
Rows are kept only if their column value matches any value from the subquery's result.Final Answer:
It filters rows where a column's value matches any value returned by the subquery. -> Option AQuick Check:
INfilters rows by subquery values [OK]
- Confusing IN with JOIN
- Thinking IN updates or deletes rows
- Assuming IN returns a single value only
IN in PostgreSQL?Solution
Step 1: Check the correct use of parentheses
The subquery must be enclosed in parentheses afterIN.Step 2: Verify the subquery returns a single column
The subquery should return one column of values to compare against.Final Answer:
SELECT * FROM employees WHERE department_id IN (SELECT id FROM departments); -> Option BQuick Check:
Subquery in parentheses with single column [OK]
- Using = IN instead of just IN
- Omitting parentheses around subquery
- Selecting multiple columns in subquery
employees(id, name, department_id)departments(id, name)What will this query return?
SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE name = 'Sales');
Solution
Step 1: Understand the subquery filtering departments
The subquery selects department IDs where the department name is 'Sales'.Step 2: Filter employees by matching department IDs
The main query returns employee names whose department_id matches those IDs.Final Answer:
Names of employees who work in the Sales department. -> Option DQuick Check:
IN filters employees by Sales department IDs [OK]
- Thinking it returns all employees
- Confusing NOT IN with IN
- Assuming subquery causes error with multiple rows
SELECT * FROM employees WHERE department_id IN SELECT id FROM departments WHERE name = 'HR';
Solution
Step 1: Check syntax for subquery with IN
The subquery must be enclosed in parentheses after the IN keyword.Step 2: Identify missing parentheses causing syntax error
The query lacks parentheses around the subquery, causing invalid syntax.Final Answer:
Missing parentheses around the subquery after IN. -> Option AQuick Check:
Subquery must be in parentheses after IN [OK]
- Omitting parentheses around subquery
- Confusing IN with EXISTS syntax
- Assuming subquery returns multiple columns
customers(id, name)orders(id, customer_id, product_id)products(id, name, category)Which query correctly uses a subquery with
IN to get these customers?Solution
Step 1: Identify the correct subquery for products in 'Electronics'
The innermost subquery selects product IDs where category is 'Electronics'.Step 2: Use these product IDs to find matching orders
The middle subquery selects customer IDs from orders with those product IDs.Step 3: Select customer names whose IDs match those customer IDs
The main query selects customer names where their ID is in the list from the orders subquery.Final Answer:
SELECT name FROM customers WHERE id IN (SELECT customer_id FROM orders WHERE product_id IN (SELECT id FROM products WHERE category = 'Electronics')); -> Option CQuick Check:
Nested IN subqueries filter customers by product category [OK]
- Using = IN instead of IN
- Mixing up customer_id and product_id columns
- Selecting wrong columns in subqueries
Ready to prove it?
Pass the test and this pattern moves unknown
