0
0
MySQLquery~20 mins

Subqueries vs JOINs comparison in MySQL - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Subqueries vs JOINs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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');
A[{"name": "Bob"}]
B[]
C[{"name": "Alice"}, {"name": "Bob"}, {"name": "Charlie"}]
D[{"name": "Alice"}, {"name": "Charlie"}]
Attempts:
2 left
💡 Hint
Think about which departments are located in NY and which employees belong to those departments.
query_result
intermediate
2: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');
A[{"name": "Alice", "location": "LA"}, {"name": "Bob", "location": "LA"}, {"name": "Charlie", "location": "LA"}]
B[{"name": "Alice", "location": "NY"}, {"name": "Charlie", "location": "NY"}]
C[{"name": "Bob", "location": "LA"}]
D[]
Attempts:
2 left
💡 Hint
Look at the JOIN condition and the WHERE clause filtering by location.
🧠 Conceptual
advanced
2:00remaining
Performance difference between subqueries and JOINs
Which statement about performance is generally true when comparing subqueries and JOINs in MySQL?
AJOINs often perform better than subqueries because they allow the database to optimize data retrieval more efficiently.
BSubqueries always perform better than JOINs because they reduce the number of rows processed.
CJOINs and subqueries always have the same performance in all cases.
DSubqueries are faster because they execute once and cache results automatically.
Attempts:
2 left
💡 Hint
Think about how databases optimize queries involving JOINs versus subqueries.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in subquery usage
Which option contains a syntax error in using a subquery in the WHERE clause?
ASELECT name FROM employees WHERE department_id IN (SELECT id FROM departments WHERE location = 'NY');
BSELECT name FROM employees WHERE department_id = (SELECT id, location FROM departments WHERE location = 'NY');
CSELECT name FROM employees WHERE department_id = ANY (SELECT id FROM departments WHERE location = 'NY');
DSELECT name FROM employees WHERE department_id = (SELECT id FROM departments WHERE location = 'NY');
Attempts:
2 left
💡 Hint
Check if the subquery returns a single column when used with '=' operator.
optimization
expert
3: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?
ASELECT e.name FROM employees e JOIN sales s ON e.id = s.employee_id;
BSELECT e.name FROM employees e WHERE EXISTS (SELECT 1 FROM sales s WHERE s.employee_id = e.id);
CSELECT e.name FROM employees e WHERE e.id = (SELECT employee_id FROM sales LIMIT 1);
DSELECT e.name FROM employees e WHERE e.id IN (SELECT employee_id FROM sales);
Attempts:
2 left
💡 Hint
Consider how JOINs and subqueries scale with large tables and indexing.