Challenge - 5 Problems
Subquery Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find employees working in departments with more than 3 employees
Given the tables employees(emp_id, emp_name, dept_id) and departments(dept_id, dept_name), which query returns the names of employees who work in departments that have more than 3 employees?
MySQL
SELECT emp_name FROM employees WHERE dept_id IN (SELECT dept_id FROM employees GROUP BY dept_id HAVING COUNT(*) > 3);
Attempts:
2 left
💡 Hint
Use a subquery with GROUP BY and HAVING to find departments with more than 3 employees, then filter employees by those departments.
✗ Incorrect
Option B correctly uses a subquery to find departments with more than 3 employees and filters employees working in those departments. Option B uses = ANY which is equivalent but less common. Option B selects employees NOT in those departments, which is the opposite. Option B filters departments by dept_id > 3, which is unrelated to employee count.
❓ query_result
intermediate2:00remaining
List products sold in categories with at least 5 products
Given tables products(product_id, product_name, category_id) and categories(category_id, category_name), which query returns the names of products that belong to categories having 5 or more products?
MySQL
SELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM products GROUP BY category_id HAVING COUNT(*) >= 5);
Attempts:
2 left
💡 Hint
Use a subquery with GROUP BY and HAVING to find categories with at least 5 products, then filter products by those categories.
✗ Incorrect
Option A correctly uses a subquery to find categories with 5 or more products and filters products accordingly. Option A filters categories by category_id >= 5, which is unrelated to product count. Option A selects products NOT in those categories, which is the opposite. Option A uses = with a subquery returning multiple rows, causing an error.
📝 Syntax
advanced2:00remaining
Identify the syntax error in the subquery with IN operator
Which option contains a syntax error in the use of the IN operator with a subquery?
MySQL
SELECT customer_name FROM customers WHERE customer_id IN SELECT customer_id FROM orders WHERE order_date > '2023-01-01';
Attempts:
2 left
💡 Hint
Remember that the subquery must be enclosed in parentheses when used with IN.
✗ Incorrect
Option C is missing parentheses around the subquery after IN, causing a syntax error. Options A, B, and C correctly use parentheses or valid syntax.
❓ optimization
advanced2:00remaining
Optimize query using IN operator with subquery for better performance
Which query is generally more efficient in MySQL when selecting employees who belong to departments located in 'New York'?
MySQL
Tables: employees(emp_id, emp_name, dept_id), departments(dept_id, dept_name, location)
Attempts:
2 left
💡 Hint
Consider how JOINs can be optimized by the database engine compared to subqueries.
✗ Incorrect
Option D uses a JOIN which is generally more efficient in MySQL for this case because it allows the optimizer to use indexes and avoid repeated subquery execution. Options A, B, and D use subqueries which may be less efficient.
🧠 Conceptual
expert3:00remaining
Understanding NULL behavior with IN operator and subqueries
Consider the query:
SELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Electronics' OR category_name IS NULL);
What will happen if the subquery returns NULL values in the list?
SELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM categories WHERE category_name = 'Electronics' OR category_name IS NULL);
What will happen if the subquery returns NULL values in the list?
Attempts:
2 left
💡 Hint
Remember how SQL treats NULL in IN conditions: NULL means unknown, not true or false.
✗ Incorrect
Option A is correct because NULL values in the subquery do not match any category_id in products. The IN operator returns TRUE only if the value matches a non-NULL value in the list. NULL causes the condition to be UNKNOWN, so those rows are excluded. Option A is incorrect because NULL is not treated as a match. Option A is incorrect because only rows with NULL category_id fail to match, not all rows. Option A is incorrect because NULLs do not cause runtime errors.