Challenge - 5 Problems
Subquery Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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');
Attempts:
2 left
💡 Hint
Think about how the subquery filters departments by location and how IN matches dept_id.
✗ Incorrect
The subquery selects dept_id values for departments in New York. The main query returns employees whose dept_id matches any of those values, so only employees in New York departments are listed.
📝 Syntax
intermediate2: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:
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');
Attempts:
2 left
💡 Hint
Check the placement of parentheses around the subquery.
✗ Incorrect
Option D is missing parentheses around the subquery, which is required syntax for IN with a subquery. Option C has an invalid '= IN' operator combination. Option B is missing quotes around the string literal 'Electronics'. Only option A is correct syntax.
❓ optimization
advanced2:00remaining
Optimize query using IN with subquery for better performance
You have a query:
Which option is the best optimization to improve performance in PostgreSQL?
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');
Attempts:
2 left
💡 Hint
Consider how JOINs can be more efficient than IN subqueries in some cases.
✗ Incorrect
Option C uses a JOIN which often performs better than IN with a subquery because it allows the database to optimize the join operation. Option C with EXISTS can be efficient but may not always outperform JOIN. Option C adds an index on order_id which is not relevant for filtering by customer_id. Option C changes logic and is incorrect.
🔧 Debug
advanced2:00remaining
Debug why query returns empty result with IN subquery
Given the query:
The query returns no rows, but you know students exist in classes taught by Mr. Smith. What is the most likely cause?
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');
Attempts:
2 left
💡 Hint
Think about string matching and how trailing spaces affect equality.
✗ Incorrect
If the teacher names have trailing spaces, the equality condition in the subquery fails to match 'Mr. Smith' exactly, so the subquery returns no rows. This causes the main query to return no students. Option A is unlikely because NULLs in subquery do not cause empty results with IN. Option A would cause a syntax error. Option A is false because the query is valid.
🧠 Conceptual
expert2:00remaining
Understanding NULL behavior in IN subqueries
Consider the query:
What will be the result if the subquery returns some NULL values along with valid category_ids?
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);
Attempts:
2 left
💡 Hint
Remember how NULLs are treated in IN conditions in SQL.
✗ Incorrect
In SQL, NULL values in the list for IN are ignored when matching. The query returns products whose category_id matches any non-NULL category_id returned by the subquery. It does not return all products or fail due to NULLs. No error is raised.