0
0
SQLquery~20 mins

Subquery with IN operator in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subquery Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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?
ASELECT name FROM Employees WHERE dept_id NOT IN (SELECT dept_id FROM Employees GROUP BY dept_id HAVING COUNT(emp_id) > 3);
BSELECT name FROM Employees WHERE emp_id IN (SELECT emp_id FROM Employees GROUP BY dept_id HAVING COUNT(emp_id) > 3);
CSELECT name FROM Employees WHERE dept_id = (SELECT dept_id FROM Employees GROUP BY dept_id HAVING COUNT(emp_id) > 3);
DSELECT name FROM Employees WHERE dept_id IN (SELECT dept_id FROM Employees GROUP BY dept_id HAVING COUNT(emp_id) > 3);
Attempts:
2 left
💡 Hint
Use a subquery to find departments with more than 3 employees, then select employees in those departments.
query_result
intermediate
2: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?
ASELECT product_name FROM Products WHERE category_id IN (SELECT category_id FROM Products GROUP BY category_id HAVING COUNT(product_id) >= 5);
BSELECT product_name FROM Products WHERE category_id NOT IN (SELECT category_id FROM Products GROUP BY category_id HAVING COUNT(product_id) >= 5);
CSELECT product_name FROM Products WHERE category_id = (SELECT category_id FROM Products GROUP BY category_id HAVING COUNT(product_id) >= 5);
DSELECT product_name FROM Products WHERE product_id IN (SELECT product_id FROM Products GROUP BY category_id HAVING COUNT(product_id) >= 5);
Attempts:
2 left
💡 Hint
Use a subquery to find categories with at least 5 products, then select products in those categories.
📝 Syntax
advanced
2: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?
ASELECT name FROM Employees WHERE dept_id IN SELECT dept_id FROM Employees GROUP BY dept_id HAVING COUNT(emp_id) > 2;
BSELECT name FROM Employees WHERE dept_id IN (SELECT dept_id FROM Employees);
CSELECT name FROM Employees WHERE dept_id IN (SELECT dept_id FROM Employees WHERE emp_id > 10);
DSELECT name FROM Employees WHERE dept_id IN (SELECT dept_id FROM Employees GROUP BY dept_id HAVING COUNT(emp_id) > 2);
Attempts:
2 left
💡 Hint
Check if the subquery is properly enclosed in parentheses after IN.
optimization
advanced
2: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'?
ASELECT name FROM Customers WHERE customer_id IN (SELECT customer_id FROM Orders WHERE region IN ('North', 'East'));
BSELECT name FROM Customers JOIN Orders ON Customers.customer_id = Orders.customer_id WHERE region IN ('North', 'East');
CSELECT name FROM Customers WHERE EXISTS (SELECT 1 FROM Orders WHERE Orders.customer_id = Customers.customer_id AND region IN ('North', 'East'));
DSELECT name FROM Customers WHERE customer_id = (SELECT customer_id FROM Orders WHERE region IN ('North', 'East'));
Attempts:
2 left
💡 Hint
EXISTS can be more efficient than IN when checking for existence.
🧠 Conceptual
expert
2: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?
AThe query will return employees with NULL dept_id because NULL matches NULL in IN operator.
BThe query will return all employees except those with NULL dept_id because NULLs are ignored in IN comparisons.
CThe query will return no rows because NULL in the subquery causes the IN condition to always evaluate to false.
DThe query will raise a runtime error due to NULL values in the subquery.
Attempts:
2 left
💡 Hint
Remember how SQL treats NULLs in IN operator comparisons.