Challenge - 5 Problems
Subquery Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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?Attempts:
2 left
💡 Hint
Think about how to compare each employee's salary to the average salary of their own department using a subquery.
✗ Incorrect
Option A correctly uses a correlated subquery to calculate the average salary for the employee's department and compares the employee's salary to it. Option A is invalid syntax. Option A compares salary to the average salary of all employees, not per department. Option A compares salary to all salaries in the department, which is not the average.
❓ query_result
intermediate2: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?Attempts:
2 left
💡 Hint
Use a subquery to compare each product's price to all prices in category 5.
✗ Incorrect
Option C correctly uses ALL to ensure the product's price is greater than every price in category 5. Option C uses ANY, which means greater than at least one price, so it returns more rows. Option C compares to the maximum price, which is equivalent to ALL but uses a scalar subquery; however, the question asks for a subquery in WHERE clause with ALL. Option C compares to the minimum price, which is incorrect.
📝 Syntax
advanced2:00remaining
Identify the syntax error in subquery usage
Which option contains a syntax error when using a subquery in the WHERE clause?
Attempts:
2 left
💡 Hint
Check the placement of the IN keyword in the WHERE clause.
✗ Incorrect
Option D is invalid syntax because it uses '= IN' together, which is not allowed. IN is a standalone operator and should not be combined with '='. Options A, B, and D are syntactically valid.
❓ optimization
advanced2: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?
Attempts:
2 left
💡 Hint
Try to calculate averages once per department and join instead of using a correlated subquery.
✗ Incorrect
Option B precomputes average salaries per department and joins, avoiding repeated subquery execution for each employee. Option B uses a correlated subquery executed for each row, which is less efficient. Options C and D misuse ALL and ANY with aggregate results and do not produce correct filtering.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Think about how to find customers who have shipped orders and then select all their orders.
✗ Incorrect
Option A correctly selects orders where the customer has at least one shipped order by checking if the customer_id is in the set of customers with shipped orders. Option A unnecessarily filters orders to shipped ones and includes all customers, which is redundant. Option A is valid but equivalent to A; however, = ANY is less common and can be confusing. Option A requires the customer_id to match all shipped orders, which is logically incorrect.