0
0
PostgreSQLquery~20 mins

Subqueries in WHERE with IN in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subquery Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find employees working in departments located in 'New York'
Given the tables employees(emp_id, emp_name, dept_id) and departments(dept_id, dept_name, location), what is the output of the following query?

SELECT emp_name FROM employees WHERE dept_id IN (SELECT dept_id FROM departments WHERE location = 'New York');
PostgreSQL
SELECT emp_name FROM employees WHERE dept_id IN (SELECT dept_id FROM departments WHERE location = 'New York');
AList of employee names who work in departments located in New York
BList of all employee names regardless of location
CEmpty result set
DSyntax error due to subquery
Attempts:
2 left
💡 Hint
Think about how the subquery filters departments by location and how IN matches dept_id.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in subquery with IN
Which option contains a syntax error in the use of a subquery with IN in PostgreSQL?

Consider the query:
SELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Electronics');
PostgreSQL
SELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Electronics');
ASELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Electronics');
BSELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = Electronics);
CSELECT product_name FROM products WHERE category_id = IN (SELECT category_id FROM categories WHERE category_name = 'Electronics');
DSELECT product_name FROM products WHERE category_id IN SELECT category_id FROM categories WHERE category_name = 'Electronics';
Attempts:
2 left
💡 Hint
Check the placement of parentheses around the subquery.
optimization
advanced
2:00remaining
Optimize query using IN with subquery for better performance
You have a query:
SELECT order_id FROM orders WHERE customer_id IN (SELECT customer_id FROM customers WHERE region = 'West');

Which option is the best optimization to improve performance in PostgreSQL?
PostgreSQL
SELECT order_id FROM orders WHERE customer_id IN (SELECT customer_id FROM customers WHERE region = 'West');
AAdd an index on orders.order_id
BRewrite using EXISTS:<br>SELECT order_id FROM orders WHERE EXISTS (SELECT 1 FROM customers WHERE customers.customer_id = orders.customer_id AND region = 'West');
CRewrite using JOIN:<br>SELECT orders.order_id FROM orders JOIN customers ON orders.customer_id = customers.customer_id WHERE customers.region = 'West';
DRewrite using NOT IN instead of IN
Attempts:
2 left
💡 Hint
Consider how JOINs can be more efficient than IN subqueries in some cases.
🔧 Debug
advanced
2:00remaining
Debug why query returns empty result with IN subquery
Given the query:
SELECT student_name FROM students WHERE class_id IN (SELECT class_id FROM classes WHERE teacher = 'Mr. Smith');

The query returns no rows, but you know students exist in classes taught by Mr. Smith. What is the most likely cause?
PostgreSQL
SELECT student_name FROM students WHERE class_id IN (SELECT class_id FROM classes WHERE teacher = 'Mr. Smith');
AThe teacher name in classes table is stored with trailing spaces
BThe subquery returns NULL values causing IN to fail
CThe students table has no class_id column
DThe query syntax is invalid
Attempts:
2 left
💡 Hint
Think about string matching and how trailing spaces affect equality.
🧠 Conceptual
expert
2:00remaining
Understanding NULL behavior in IN subqueries
Consider the query:
SELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM categories WHERE discontinued IS NULL);

What will be the result if the subquery returns some NULL values along with valid category_ids?
PostgreSQL
SELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM categories WHERE discontinued IS NULL);
AThe query raises an error due to NULL values in subquery
BThe query returns no rows because NULL in subquery causes IN to fail
CThe query returns all products regardless of category_id
DThe query returns all products with category_id matching non-NULL category_ids only
Attempts:
2 left
💡 Hint
Remember how NULLs are treated in IN conditions in SQL.