Challenge - 5 Problems
Subqueries vs JOINs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of subquery filtering
Given the tables employees and departments, what is the output of this query?
SELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE location = 'NY');
MySQL
CREATE TABLE employees (id INT, name VARCHAR(50), department_id INT); CREATE TABLE departments (id INT, name VARCHAR(50), location VARCHAR(50)); INSERT INTO employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', 10); INSERT INTO departments VALUES (10, 'Sales', 'NY'), (20, 'HR', 'LA');
Attempts:
2 left
💡 Hint
Think about which departments are located in NY and which employees belong to those departments.
✗ Incorrect
The subquery selects department IDs where location is 'NY' (id=10). Employees with department_id 10 are Alice and Charlie.
❓ query_result
intermediate2:00remaining
Output of JOIN with filtering
What is the output of this JOIN query?
SELECT e.name, d.location FROM employees e JOIN departments d ON e.department_id = d.id WHERE d.location = 'LA';
MySQL
CREATE TABLE employees (id INT, name VARCHAR(50), department_id INT); CREATE TABLE departments (id INT, name VARCHAR(50), location VARCHAR(50)); INSERT INTO employees VALUES (1, 'Alice', 10), (2, 'Bob', 20), (3, 'Charlie', 10); INSERT INTO departments VALUES (10, 'Sales', 'NY'), (20, 'HR', 'LA');
Attempts:
2 left
💡 Hint
Look at the JOIN condition and the WHERE clause filtering by location.
✗ Incorrect
The JOIN matches employees to departments by department_id. Only Bob belongs to department 20 which is located in LA.
🧠 Conceptual
advanced2:00remaining
Performance difference between subqueries and JOINs
Which statement about performance is generally true when comparing subqueries and JOINs in MySQL?
Attempts:
2 left
💡 Hint
Think about how databases optimize queries involving JOINs versus subqueries.
✗ Incorrect
JOINs let the database engine combine tables in a single operation, often using indexes efficiently, while subqueries may be executed repeatedly or less efficiently.
📝 Syntax
advanced2:00remaining
Identify the syntax error in subquery usage
Which option contains a syntax error in using a subquery in the WHERE clause?
Attempts:
2 left
💡 Hint
Check if the subquery returns a single column when used with '=' operator.
✗ Incorrect
Option B's subquery returns two columns, which is invalid when using '=' expecting a single value.
❓ optimization
expert3:00remaining
Optimizing query with subquery vs JOIN
You want to list all employees who have made sales. The sales table records sales with employee_id. Which query is generally more efficient in MySQL for large datasets?
Attempts:
2 left
💡 Hint
Consider how JOINs and subqueries scale with large tables and indexing.
✗ Incorrect
JOINs typically allow the database to use indexes and optimize the query plan better than IN or EXISTS subqueries on large datasets.