Bird
Raised Fist0
MySQLquery~20 mins

Subqueries vs JOINs comparison in MySQL - Practice Questions

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following best describes the main difference between a JOIN and a subquery in MySQL?
easy
A. JOIN runs inside another query; subqueries combine tables side-by-side.
B. JOIN is used only for filtering; subqueries are used only for combining tables.
C. JOIN combines tables side-by-side; subqueries run inside another query to filter or find values.
D. JOIN and subqueries are exactly the same in functionality and performance.

Solution

  1. Step 1: Understand the role of JOIN

    A JOIN combines rows from two or more tables side-by-side based on a related column between them.
  2. Step 2: Understand the role of subqueries

    A subquery is a query nested inside another query, often used to filter or find specific values before the main query runs.
  3. Final Answer:

    JOIN combines tables side-by-side; subqueries run inside another query to filter or find values. -> Option C
  4. Quick Check:

    JOIN = combine tables, Subquery = nested query [OK]
Hint: JOINs combine tables; subqueries nest queries inside [OK]
Common Mistakes:
  • Confusing JOINs as nested queries
  • Thinking subqueries combine tables side-by-side
  • Believing JOINs and subqueries have identical performance always
2. Which of the following is the correct syntax to write a JOIN between tables employees and departments on the column department_id?
easy
A. SELECT * FROM employees JOIN departments USING employees.department_id;
B. SELECT * FROM employees JOIN departments WHERE employees.department_id = departments.department_id;
C. SELECT * FROM employees, departments ON employees.department_id = departments.department_id;
D. SELECT * FROM employees JOIN departments ON employees.department_id = departments.department_id;

Solution

  1. Step 1: Identify correct JOIN syntax

    The correct JOIN syntax uses the ON keyword to specify the matching columns between tables.
  2. Step 2: Check each option

    SELECT * FROM employees JOIN departments ON employees.department_id = departments.department_id; uses ON correctly. SELECT * FROM employees JOIN departments WHERE employees.department_id = departments.department_id; incorrectly uses WHERE instead of ON. SELECT * FROM employees, departments ON employees.department_id = departments.department_id; uses comma join but incorrectly adds ON. SELECT * FROM employees JOIN departments USING employees.department_id; misuses USING with full column name.
  3. Final Answer:

    SELECT * FROM employees JOIN departments ON employees.department_id = departments.department_id; -> Option D
  4. Quick Check:

    JOIN requires ON for matching columns [OK]
Hint: Use ON to specify join condition, not WHERE [OK]
Common Mistakes:
  • Using WHERE instead of ON for JOIN condition
  • Using comma join with ON clause
  • Incorrect USING syntax with full column names
3. Given the tables orders(order_id, customer_id, amount) and customers(customer_id, name), what will the following query return?
SELECT name FROM customers WHERE customer_id IN (SELECT customer_id FROM orders WHERE amount > 100);
medium
A. Names of customers who have orders with amount greater than 100.
B. All customer names regardless of orders.
C. Names of customers who have no orders.
D. Syntax error due to subquery.

Solution

  1. Step 1: Analyze the subquery

    The subquery selects customer_id from orders where amount is greater than 100.
  2. Step 2: Analyze the main query

    The main query selects names from customers where customer_id is in the list returned by the subquery.
  3. Final Answer:

    Names of customers who have orders with amount greater than 100. -> Option A
  4. Quick Check:

    Subquery filters customers by order amount [OK]
Hint: IN with subquery filters matching rows [OK]
Common Mistakes:
  • Assuming all customers are returned
  • Thinking it returns customers without orders
  • Believing subquery causes syntax error
4. Identify the error in the following query that tries to list all employees with their department names:
SELECT e.name, d.department_name FROM employees e JOIN departments d WHERE e.department_id = d.department_id;
medium
A. Using WHERE instead of HAVING for join condition.
B. Missing ON keyword before the join condition.
C. Incorrect table aliases used.
D. No error; query is correct.

Solution

  1. Step 1: Check JOIN syntax

    JOIN requires the ON keyword to specify the join condition between tables.
  2. Step 2: Identify the mistake

    The query uses WHERE instead of ON to specify the join condition, which is incorrect syntax.
  3. Final Answer:

    Missing ON keyword before the join condition. -> Option B
  4. Quick Check:

    JOIN needs ON, not WHERE, for conditions [OK]
Hint: JOIN conditions must use ON, not WHERE [OK]
Common Mistakes:
  • Using WHERE instead of ON for JOIN
  • Confusing HAVING with JOIN condition
  • Assuming aliases cause error
5. You want to find all customers who have placed orders but only want to use a JOIN instead of a subquery. Which query correctly replaces this subquery?
SELECT name FROM customers WHERE customer_id IN (SELECT customer_id FROM orders);
hard
A. SELECT DISTINCT c.name FROM customers c JOIN orders o ON c.customer_id = o.customer_id;
B. SELECT c.name FROM customers c JOIN orders o WHERE c.customer_id = o.customer_id;
C. SELECT c.name FROM customers c, orders o WHERE c.customer_id = o.customer_id;
D. SELECT name FROM customers LEFT JOIN orders ON customer_id = customer_id;

Solution

  1. Step 1: Understand the original subquery

    The subquery selects customer_ids from orders, filtering customers who placed orders.
  2. Step 2: Translate to JOIN

    Using JOIN on customer_id between customers and orders returns matching customers. DISTINCT is needed to avoid duplicates.
  3. Step 3: Evaluate options

    SELECT DISTINCT c.name FROM customers c JOIN orders o ON c.customer_id = o.customer_id; uses JOIN with ON and DISTINCT correctly. SELECT c.name FROM customers c JOIN orders o WHERE c.customer_id = o.customer_id; misses ON keyword. SELECT c.name FROM customers c, orders o WHERE c.customer_id = o.customer_id; uses old-style join but is valid but may return duplicates. SELECT name FROM customers LEFT JOIN orders ON customer_id = customer_id; uses LEFT JOIN but ON condition is ambiguous.
  4. Final Answer:

    SELECT DISTINCT c.name FROM customers c JOIN orders o ON c.customer_id = o.customer_id; -> Option A
  5. Quick Check:

    JOIN with ON and DISTINCT matches subquery result [OK]
Hint: Use JOIN with ON and DISTINCT to replace IN subquery [OK]
Common Mistakes:
  • Omitting ON keyword in JOIN
  • Not using DISTINCT causing duplicates
  • Using ambiguous ON conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard