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, name, dept_id) and Departments(dept_id, dept_name), which query returns the names of employees who work in departments having more than 3 employees?
Attempts:
2 left
💡 Hint
Use a subquery to find departments with more than 3 employees, then select employees in those departments.
✗ Incorrect
Option D correctly uses a subquery with IN to find all dept_id values where the count of employees is greater than 3, then selects employees in those departments. Option D incorrectly filters by emp_id, which doesn't relate to department counts. Option D uses '=' which expects a single value but the subquery returns multiple rows, causing an error. Option D selects employees NOT in those departments, which is the opposite of the requirement.
❓ 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 lists product names where their category has 5 or more products?
Attempts:
2 left
💡 Hint
Use a subquery to find categories with at least 5 products, then select products in those categories.
✗ Incorrect
Option A correctly uses IN with a subquery that groups by category_id and filters categories having 5 or more products. Option A incorrectly filters by product_id which is unrelated to category counts. Option A uses '=' which fails if multiple categories meet the condition. Option A selects products not in those categories, opposite of the requirement.
📝 Syntax
advanced2:00remaining
Identify the syntax error in the subquery with IN operator
Which option contains a syntax error in the SQL query using a subquery with the IN operator?
Attempts:
2 left
💡 Hint
Check if the subquery is properly enclosed in parentheses after IN.
✗ Incorrect
Option A is missing parentheses around the subquery after IN, causing a syntax error. Options A, B, and C correctly use parentheses enclosing the subquery.
❓ optimization
advanced2:00remaining
Optimize query to find customers who placed orders in specific regions
Given tables Customers(customer_id, name), Orders(order_id, customer_id, region), which query is the most efficient to find names of customers who placed orders in regions 'North' or 'East'?
Attempts:
2 left
💡 Hint
EXISTS can be more efficient than IN when checking for existence.
✗ Incorrect
Option C uses EXISTS which stops searching once a matching order is found, improving efficiency. Option C uses IN which may scan all matching customer_ids. Option C returns duplicates if customers have multiple orders in those regions and may require DISTINCT. Option C uses '=' which fails if multiple orders match, causing an error.
🧠 Conceptual
expert2:00remaining
Understanding behavior of IN operator with NULL values in subquery
Consider the query:
SELECT name FROM Employees WHERE dept_id IN (SELECT dept_id FROM Employees WHERE dept_id IS NOT NULL);
What will be the effect if the subquery returns some NULL values and why?
SELECT name FROM Employees WHERE dept_id IN (SELECT dept_id FROM Employees WHERE dept_id IS NOT NULL);
What will be the effect if the subquery returns some NULL values and why?
Attempts:
2 left
💡 Hint
Remember how SQL treats NULLs in IN operator comparisons.
✗ Incorrect
In SQL, NULL values in the list returned by a subquery in IN are ignored for matching. The condition dept_id IN (subquery) returns true for matching non-NULL dept_id values. Employees with NULL dept_id do not match any value and are excluded. No error occurs. NULL does not equal NULL in SQL comparisons.