0
0
MySQLquery~20 mins

Subqueries in WHERE clause in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subquery Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find employees with salary above department average
Given a table employees with columns id, name, department_id, and salary, which query returns all employees whose salary is greater than the average salary of their department?
ASELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = employees.department_id);
BSELECT name FROM employees WHERE salary > AVG(salary) FROM employees GROUP BY department_id;
CSELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees);
DSELECT name FROM employees WHERE salary > ALL (SELECT salary FROM employees WHERE department_id = employees.department_id);
Attempts:
2 left
💡 Hint
Think about how to compare each employee's salary to the average salary of their own department using a subquery.
query_result
intermediate
2:00remaining
List products with price higher than any product in category 5
Given a table products with columns product_id, category_id, and price, which query returns products whose price is higher than the price of every product in category 5?
ASELECT product_id FROM products WHERE price > ANY (SELECT price FROM products WHERE category_id = 5);
BSELECT product_id FROM products WHERE price > (SELECT MIN(price) FROM products WHERE category_id = 5);
CSELECT product_id FROM products WHERE price > ALL (SELECT price FROM products WHERE category_id = 5);
DSELECT product_id FROM products WHERE price > (SELECT MAX(price) FROM products WHERE category_id = 5);
Attempts:
2 left
💡 Hint
Use a subquery to compare each product's price to all prices in category 5.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in subquery usage
Which option contains a syntax error when using a subquery in the WHERE clause?
ASELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE city = 'Paris');
BSELECT * FROM orders WHERE customer_id = (SELECT id FROM customers WHERE city = 'Paris');
CSELECT * FROM orders WHERE customer_id = ANY (SELECT id FROM customers WHERE city = 'Paris');
DSELECT * FROM orders WHERE customer_id = IN (SELECT id FROM customers WHERE city = 'Paris');
Attempts:
2 left
💡 Hint
Check the placement of the IN keyword in the WHERE clause.
optimization
advanced
2:00remaining
Optimize query with subquery in WHERE clause
You have a query that selects employees with salaries above the average salary of their department using a subquery in the WHERE clause. Which rewritten query is more efficient by avoiding repeated subquery execution?
ASELECT name FROM employees WHERE salary > (SELECT AVG(salary) FROM employees WHERE department_id = employees.department_id);
BSELECT e.name FROM employees e JOIN (SELECT department_id, AVG(salary) AS avg_salary FROM employees GROUP BY department_id) d ON e.department_id = d.department_id WHERE e.salary > d.avg_salary;
CSELECT name FROM employees WHERE salary > ALL (SELECT AVG(salary) FROM employees GROUP BY department_id);
DSELECT name FROM employees WHERE salary > ANY (SELECT AVG(salary) FROM employees GROUP BY department_id);
Attempts:
2 left
💡 Hint
Try to calculate averages once per department and join instead of using a correlated subquery.
🧠 Conceptual
expert
3:00remaining
Understanding NULL behavior in subqueries within WHERE clause
Consider a table orders with columns order_id, customer_id, and shipped_date which can be NULL if not shipped. Which query correctly returns all orders where the customer has at least one shipped order?
ASELECT * FROM orders WHERE customer_id IN (SELECT customer_id FROM orders WHERE shipped_date IS NOT NULL);
BSELECT * FROM orders WHERE shipped_date IS NOT NULL AND customer_id IN (SELECT customer_id FROM orders);
CSELECT * FROM orders WHERE customer_id = ANY (SELECT customer_id FROM orders WHERE shipped_date IS NOT NULL);
DSELECT * FROM orders WHERE customer_id = ALL (SELECT customer_id FROM orders WHERE shipped_date IS NOT NULL);
Attempts:
2 left
💡 Hint
Think about how to find customers who have shipped orders and then select all their orders.