Challenge - 5 Problems
Subquery Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Find customers with orders
Given two tables Customers and Orders, which query returns all customers who have placed at least one order?
SQL
SELECT CustomerID, CustomerName FROM Customers WHERE EXISTS (SELECT 1 FROM Orders WHERE Orders.CustomerID = Customers.CustomerID);
Attempts:
2 left
💡 Hint
EXISTS checks if the subquery returns any rows for each customer.
✗ Incorrect
The EXISTS operator returns true if the subquery finds any matching rows. Here, it checks if there is at least one order for each customer.
❓ query_result
intermediate2:00remaining
Identify products never ordered
Which query returns products that have never been ordered from the Products and OrderDetails tables?
SQL
SELECT ProductID, ProductName FROM Products WHERE NOT EXISTS (SELECT 1 FROM OrderDetails WHERE OrderDetails.ProductID = Products.ProductID);
Attempts:
2 left
💡 Hint
NOT EXISTS returns true when the subquery finds no matching rows.
✗ Incorrect
The query uses NOT EXISTS to find products without any matching order details, meaning never ordered.
📝 Syntax
advanced2:00remaining
Detect syntax error in EXISTS subquery
Which option contains a syntax error in the use of EXISTS with a subquery?
SQL
SELECT EmployeeID FROM Employees WHERE EXISTS (SELECT FROM Orders WHERE Orders.EmployeeID = Employees.EmployeeID);
Attempts:
2 left
💡 Hint
The subquery in EXISTS must select at least one column or a constant.
✗ Incorrect
Option B is missing the column or constant after SELECT, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimize query with EXISTS vs IN
Which query is generally more efficient to find customers with orders, assuming Orders has many rows?
Attempts:
2 left
💡 Hint
EXISTS stops searching after finding the first match, IN may scan all matches.
✗ Incorrect
EXISTS is often more efficient because it stops checking once a match is found, while IN may process all matching rows.
🧠 Conceptual
expert3:00remaining
Understanding EXISTS with correlated subqueries
Consider the query:
What does this query return?
SELECT DepartmentID FROM Departments d WHERE EXISTS (SELECT 1 FROM Employees e WHERE e.DepartmentID = d.DepartmentID AND e.Salary > 50000);
What does this query return?
Attempts:
2 left
💡 Hint
EXISTS checks for each department if the subquery finds any employee with salary > 50000.
✗ Incorrect
The query returns departments where the subquery finds at least one employee with salary above 50000.