0
0
MySQLquery~20 mins

Subqueries with IN operator in MySQL - 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, 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);
ASELECT emp_name FROM employees WHERE dept_id = ANY (SELECT dept_id FROM employees GROUP BY dept_id HAVING COUNT(*) > 3);
BSELECT emp_name FROM employees WHERE dept_id IN (SELECT dept_id FROM employees GROUP BY dept_id HAVING COUNT(*) > 3);
CSELECT emp_name FROM employees WHERE dept_id NOT IN (SELECT dept_id FROM employees GROUP BY dept_id HAVING COUNT(*) > 3);
DSELECT emp_name FROM employees WHERE dept_id IN (SELECT dept_id FROM departments WHERE dept_id > 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.
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 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);
ASELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM products GROUP BY category_id HAVING COUNT(*) >= 5);
BSELECT product_name FROM products WHERE category_id IN (SELECT category_id FROM categories WHERE category_id >= 5);
CSELECT product_name FROM products WHERE category_id NOT IN (SELECT category_id FROM products GROUP BY category_id HAVING COUNT(*) >= 5);
DSELECT product_name FROM products WHERE category_id = (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.
📝 Syntax
advanced
2: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';
ASELECT customer_name FROM customers WHERE customer_id IN (SELECT customer_id FROM orders WHERE order_date > '2023-01-01');
B;)'10-10-3202' > etad_redro EREHW sredro MORF di_remotsuc TCELES( NI di_remotsuc EREHW sremotsuc MORF eman_remotsuc TCELES
CSELECT customer_name FROM customers WHERE customer_id IN SELECT customer_id FROM orders WHERE order_date > '2023-01-01';
DSELECT customer_name FROM customers WHERE customer_id = ANY (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.
optimization
advanced
2: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)
ASELECT emp_name FROM employees WHERE dept_id IN (SELECT dept_id FROM departments WHERE location = 'New York');
BSELECT emp_name FROM employees WHERE dept_id = ANY (SELECT dept_id FROM departments WHERE location = 'New York');
CSELECT emp_name FROM employees WHERE EXISTS (SELECT 1 FROM departments WHERE departments.dept_id = employees.dept_id AND location = 'New York');
DSELECT emp_name FROM employees JOIN departments ON employees.dept_id = departments.dept_id WHERE location = 'New York';
Attempts:
2 left
💡 Hint
Consider how JOINs can be optimized by the database engine compared to subqueries.
🧠 Conceptual
expert
3: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?
AThe query will return products only where category_id matches non-NULL category_ids from the subquery; rows with NULL category_id will not match.
BThe query will return no products because NULL in the subquery causes the IN condition to evaluate to UNKNOWN for all rows.
CThe query will return all products because NULL is treated as a match in IN operator.
DThe query will raise a runtime error due to NULL values in the subquery.
Attempts:
2 left
💡 Hint
Remember how SQL treats NULL in IN conditions: NULL means unknown, not true or false.