0
0
PostgreSQLquery~5 mins

Subqueries in WHERE with IN in PostgreSQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the IN keyword do in a SQL WHERE clause?
The IN keyword checks if a value matches any value in a list or the result of a subquery. It helps filter rows where the column's value is inside a set of values.
Click to reveal answer
beginner
How does a subquery work inside a WHERE ... IN clause?
A subquery inside WHERE ... IN runs first and returns a list of values. The main query then checks if each row's value is in that list to decide if it should be included.
Click to reveal answer
beginner
Write a simple example of a query using WHERE ... IN with a subquery.
Example: <br> SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE location = 'NY'); <br> This finds employees who work in departments located in New York.
Click to reveal answer
intermediate
Why use a subquery with IN instead of multiple OR conditions?
Using a subquery with IN is cleaner and easier to read, especially when checking many values. It also lets the database handle the list dynamically, which is more flexible.
Click to reveal answer
intermediate
Can the subquery inside WHERE ... IN return duplicate values? What happens then?
Yes, the subquery can return duplicates. The IN clause treats the list as a set, so duplicates do not affect the result. The main query checks if the value is present at least once.
Click to reveal answer
What does the following query do? <br> SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE city = 'Paris');
AFinds customers who made orders in Paris.
BFinds orders made by customers living in Paris.
CFinds orders shipped to Paris.
DFinds customers who live in cities with orders.
Which of these is a valid use of IN with a subquery?
ASELECT * FROM products WHERE price IN (SELECT price FROM products);
BSELECT * FROM products WHERE price = IN (SELECT price FROM products);
CSELECT * FROM products WHERE IN price (SELECT price FROM products);
DSELECT * FROM products WHERE price IN SELECT price FROM products;
What happens if the subquery in WHERE ... IN returns no rows?
AThe main query returns NULL values.
BThe main query returns all rows.
CThe main query throws an error.
DThe main query returns no rows.
Can the subquery in WHERE ... IN reference columns from the outer query?
ANo, subqueries cannot reference outer query columns.
BOnly if the outer query uses GROUP BY.
CYes, this is called a correlated subquery.
DOnly if the subquery uses DISTINCT.
Which keyword can be used instead of IN to check if a value is NOT in the subquery result?
ANOT IN
BEXISTS
CANY
DALL
Explain how a subquery inside a WHERE ... IN clause works and why it is useful.
Think about the order of execution and how the main query uses the subquery's result.
You got /5 concepts.
    Describe a real-life example where using WHERE ... IN with a subquery would be helpful.
    Imagine you want to find all items related to a certain group or category.
    You got /4 concepts.