0
0
SQLquery~20 mins

Subquery with EXISTS 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 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);
AReturns customers who have no orders.
BReturns all customers regardless of orders.
CSyntax error due to missing alias.
DReturns all customers who have at least one order.
Attempts:
2 left
💡 Hint
EXISTS checks if the subquery returns any rows for each customer.
query_result
intermediate
2: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);
ASyntax error due to missing NOT keyword.
BReturns products that have been ordered at least once.
CReturns products that have never been ordered.
DReturns all products regardless of orders.
Attempts:
2 left
💡 Hint
NOT EXISTS returns true when the subquery finds no matching rows.
📝 Syntax
advanced
2: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);
ASELECT EmployeeID FROM Employees WHERE EXISTS (SELECT 1 FROM Orders WHERE Orders.EmployeeID = Employees.EmployeeID);
BSELECT EmployeeID FROM Employees WHERE EXISTS (SELECT FROM Orders WHERE Orders.EmployeeID = Employees.EmployeeID);
CSELECT EmployeeID FROM Employees WHERE EXISTS (SELECT * FROM Orders WHERE Orders.EmployeeID = Employees.EmployeeID);
DSELECT EmployeeID FROM Employees WHERE EXISTS (SELECT OrderID FROM Orders WHERE Orders.EmployeeID = Employees.EmployeeID);
Attempts:
2 left
💡 Hint
The subquery in EXISTS must select at least one column or a constant.
optimization
advanced
2:00remaining
Optimize query with EXISTS vs IN
Which query is generally more efficient to find customers with orders, assuming Orders has many rows?
ASELECT CustomerID FROM Customers WHERE EXISTS (SELECT 1 FROM Orders WHERE Orders.CustomerID = Customers.CustomerID);
BSELECT CustomerID FROM Customers WHERE CustomerID IN (SELECT CustomerID FROM Orders);
CSELECT CustomerID FROM Customers JOIN Orders ON Customers.CustomerID = Orders.CustomerID;
DSELECT DISTINCT CustomerID FROM Orders;
Attempts:
2 left
💡 Hint
EXISTS stops searching after finding the first match, IN may scan all matches.
🧠 Conceptual
expert
3:00remaining
Understanding EXISTS with correlated subqueries
Consider the query:
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?
ADepartments that have at least one employee earning more than 50000.
BAll departments regardless of employee salaries.
CDepartments with no employees earning more than 50000.
DEmployees earning more than 50000 in any department.
Attempts:
2 left
💡 Hint
EXISTS checks for each department if the subquery finds any employee with salary > 50000.