Challenge - 5 Problems
Subquery Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of EXISTS with matching subquery
Given the tables employees and departments, what is the output of the following query?
SELECT e.name FROM employees e WHERE EXISTS (SELECT 1 FROM departments d WHERE d.id = e.department_id AND d.location = 'New York');
MySQL
CREATE TABLE employees (id INT, name VARCHAR(50), department_id INT); CREATE TABLE departments (id INT, location VARCHAR(50)); INSERT INTO employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', 10); INSERT INTO departments VALUES (10, 'New York'), (20, 'Chicago');
Attempts:
2 left
💡 Hint
EXISTS returns true if the subquery finds any matching row.
✗ Incorrect
The subquery checks if the employee's department is located in New York. Departments with id 10 are in New York, so employees Alice and Charlie match.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in EXISTS subquery
Which option contains a syntax error in the use of EXISTS in this query?
SELECT name FROM employees WHERE EXISTS (SELECT * FROM departments WHERE id = department_id);
Attempts:
2 left
💡 Hint
EXISTS requires a subquery enclosed in parentheses.
✗ Incorrect
Option B is missing parentheses around the subquery after EXISTS, causing a syntax error.
❓ optimization
advanced2:00remaining
Optimizing EXISTS subquery for performance
Which query is generally more efficient when checking existence of related rows in orders for each customers row?
A) SELECT c.id FROM customers c WHERE EXISTS (SELECT 1 FROM orders o WHERE o.customer_id = c.id); B) SELECT c.id FROM customers c JOIN orders o ON o.customer_id = c.id;
Attempts:
2 left
💡 Hint
EXISTS stops searching after finding the first matching row.
✗ Incorrect
EXISTS returns true as soon as it finds one matching row, so it can be faster than JOIN which may return multiple rows per customer.
🔧 Debug
advanced2:00remaining
Why does this EXISTS query return no rows?
Given the tables products and sales, why does this query return no rows?
Data:
products: (1, 'Pen'), (2, 'Notebook')
sales: (1, 500), (2, 800)
SELECT p.name FROM products p WHERE EXISTS (SELECT 1 FROM sales s WHERE s.product_id = p.id AND s.quantity > 1000);
Data:
products: (1, 'Pen'), (2, 'Notebook')
sales: (1, 500), (2, 800)
Attempts:
2 left
💡 Hint
EXISTS returns true only if the subquery finds rows matching all conditions.
✗ Incorrect
The subquery filters sales with quantity > 1000, but none exist, so EXISTS returns false for all products, resulting in no rows.
🧠 Conceptual
expert3:00remaining
Understanding EXISTS with correlated subqueries
What does the following query return?
Assuming departments and employees tables with relevant data.
SELECT d.name FROM departments d WHERE EXISTS (SELECT 1 FROM employees e WHERE e.department_id = d.id AND e.salary > 70000);
Assuming departments and employees tables with relevant data.
Attempts:
2 left
💡 Hint
EXISTS checks if the subquery finds any employee in the department with salary > 70000.
✗ Incorrect
The query returns department names where the subquery finds at least one employee with salary greater than 70000, filtering departments accordingly.