Bird
Raised Fist0
SQLquery~20 mins

Subquery vs JOIN performance trade-off in SQL - Practice Questions

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Subquery vs JOIN Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of JOIN vs Subquery with filtering

Given two tables employees and departments, which query returns the names of employees who work in the 'Sales' department?

Table employees: (id, name, dept_id)

Table departments: (id, dept_name)

SQL
SELECT e.name FROM employees e JOIN departments d ON e.dept_id = d.id WHERE d.dept_name = 'Sales';
ASELECT e.name FROM employees e JOIN departments d ON e.dept_id = d.id WHERE d.dept_name = 'Sales';
BSELECT name FROM employees WHERE dept_id = (SELECT id FROM departments WHERE dept_name = 'Sales');
CSELECT name FROM employees WHERE dept_id IN (SELECT id FROM departments WHERE dept_name = 'Sales');
DSELECT name FROM employees WHERE dept_id = (SELECT dept_id FROM departments WHERE dept_name = 'Sales');
Attempts:
2 left
💡 Hint

Think about how JOIN matches rows and how subqueries filter results.

🧠 Conceptual
intermediate
1:30remaining
Performance difference between JOIN and Subquery

Which statement best describes the typical performance difference between using a JOIN and a subquery in SQL when retrieving related data?

AJOINs often perform better because they allow the database optimizer to use indexes and combine data efficiently.
BSubqueries are always faster because they filter data before joining.
CJOINs are always slower because they combine tables before filtering.
DThere is no performance difference; both always execute the same way.
Attempts:
2 left
💡 Hint

Consider how databases optimize queries and use indexes.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in subquery usage

Which SQL query has a syntax error when trying to select employees working in the 'Marketing' department?

SQL
SELECT name FROM employees WHERE dept_id = (SELECT id FROM departments WHERE dept_name = 'Marketing');
ASELECT name FROM employees WHERE dept_id = (SELECT id FROM departments WHERE dept_name = 'Marketing');
BSELECT name FROM employees WHERE dept_id IN (SELECT id FROM departments WHERE dept_name = 'Marketing');
CSELECT name FROM employees WHERE dept_id = ANY (SELECT id FROM departments WHERE dept_name = 'Marketing');
DSELECT name FROM employees WHERE dept_id = (SELECT dept_id FROM departments WHERE dept_name = 'Marketing');
Attempts:
2 left
💡 Hint

Check if the column names in the subquery exist in the referenced table.

optimization
advanced
2:30remaining
Optimizing query with JOIN vs Subquery

You want to list all employees with their department names. Which query is generally more efficient on large datasets?

ASELECT e.name, (SELECT dept_name FROM departments d WHERE d.id = e.dept_id) AS department FROM employees e;
BSELECT name, dept_name FROM employees, departments WHERE employees.dept_id = departments.id;
CSELECT e.name, d.dept_name FROM employees e JOIN departments d ON e.dept_id = d.id;
DSELECT name, dept_name FROM employees LEFT JOIN departments ON employees.dept_id = departments.id WHERE departments.dept_name IS NOT NULL;
Attempts:
2 left
💡 Hint

Consider how JOINs and subqueries execute on large tables.

🔧 Debug
expert
3:00remaining
Diagnose performance issue with correlated subquery

A query uses a correlated subquery to count orders per customer:

SELECT c.customer_id, c.name, (SELECT COUNT(*) FROM orders o WHERE o.customer_id = c.customer_id) AS order_count FROM customers c;

It runs very slowly on large data. What is the main reason?

AThe query lacks a GROUP BY clause, causing aggregation errors.
BThe subquery runs once per customer, causing many repeated scans of the orders table.
CThe JOIN between customers and orders is missing, causing incomplete results.
DThe COUNT(*) function is not supported in subqueries.
Attempts:
2 left
💡 Hint

Think about how correlated subqueries execute for each row.

Practice

(1/5)
1. Which statement best describes the performance difference between a JOIN and a subquery in SQL?
easy
A. JOINs generally perform better because they combine tables in a single step.
B. Subqueries always perform better because they run separately.
C. JOINs and subqueries have the same performance in all cases.
D. Subqueries are faster because they use less memory.

Solution

  1. Step 1: Understand how JOINs work

    JOINs combine rows from two or more tables in one operation, which is often optimized by the database engine.
  2. Step 2: Compare with subqueries

    Subqueries run separately and then feed results to the main query, which can be slower especially with large data.
  3. Final Answer:

    JOINs generally perform better because they combine tables in a single step. -> Option A
  4. Quick Check:

    JOIN performance > Subquery performance [OK]
Hint: JOINs usually run faster than subqueries [OK]
Common Mistakes:
  • Thinking subqueries always run faster
  • Assuming JOINs and subqueries are always equal
  • Believing subqueries use less memory
2. Which of the following SQL queries correctly uses a JOIN to get all customers and their orders?
easy
A. SELECT customers.name, orders.id FROM customers JOIN orders ON customers.id = orders.customer_id;
B. SELECT customers.name, orders.id FROM customers WHERE customers.id = orders.customer_id;
C. SELECT customers.name, orders.id FROM customers, orders WHERE customers.id == orders.customer_id;
D. SELECT customers.name, orders.id FROM customers JOIN orders ON customers.customer_id = orders.id;

Solution

  1. Step 1: Check JOIN syntax

    Correct JOIN syntax uses ON with matching keys: customers.id = orders.customer_id.
  2. Step 2: Validate each option

    SELECT customers.name, orders.id FROM customers JOIN orders ON customers.id = orders.customer_id; uses correct JOIN and ON condition. SELECT customers.name, orders.id FROM customers WHERE customers.id = orders.customer_id; uses WHERE without JOIN, which is invalid here. SELECT customers.name, orders.id FROM customers, orders WHERE customers.id == orders.customer_id; uses double equals (==) which is invalid in SQL. SELECT customers.name, orders.id FROM customers JOIN orders ON customers.customer_id = orders.id; reverses keys incorrectly.
  3. Final Answer:

    SELECT customers.name, orders.id FROM customers JOIN orders ON customers.id = orders.customer_id; -> Option A
  4. Quick Check:

    Correct JOIN syntax = SELECT customers.name, orders.id FROM customers JOIN orders ON customers.id = orders.customer_id; [OK]
Hint: JOIN uses ON with matching keys, not WHERE or == [OK]
Common Mistakes:
  • Using WHERE instead of ON for JOIN condition
  • Using == instead of = in SQL
  • Mixing up key columns in ON clause
3. Given the tables employees(id, name) and departments(id, name, manager_id), what will this query return?
SELECT e.name FROM employees e WHERE e.id IN (SELECT d.manager_id FROM departments d);
medium
A. Syntax error due to subquery.
B. Names of all employees regardless of department.
C. Names of employees who are not managers.
D. Names of employees who are managers of any department.

Solution

  1. Step 1: Understand the subquery

    The subquery SELECT d.manager_id FROM departments d returns all manager IDs from departments.
  2. Step 2: Analyze the main query

    The main query selects employee names where their ID is in the list of manager IDs, so it returns employees who manage departments.
  3. Final Answer:

    Names of employees who are managers of any department. -> Option D
  4. Quick Check:

    Subquery filters managers = Names of employees who are managers of any department. [OK]
Hint: IN with subquery filters matching IDs [OK]
Common Mistakes:
  • Thinking it returns all employees
  • Confusing managers with non-managers
  • Assuming syntax error in subquery
4. Identify the error in this SQL query that uses a JOIN:
SELECT c.name, o.amount FROM customers c JOIN orders o WHERE c.id = o.customer_id;
medium
A. Incorrect table aliases used.
B. Using WHERE instead of HAVING for condition.
C. Missing ON keyword before join condition.
D. No error; query is correct.

Solution

  1. Step 1: Review JOIN syntax

    JOIN requires an ON clause to specify join condition, not WHERE.
  2. Step 2: Check the query

    The query uses WHERE for join condition, which is incorrect syntax for explicit JOIN.
  3. Final Answer:

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

    JOIN needs ON, not WHERE [OK]
Hint: JOIN must have ON clause for conditions [OK]
Common Mistakes:
  • Using WHERE instead of ON for JOIN
  • Confusing HAVING with WHERE
  • Assuming aliases cause error
5. You want to list all products and their category names. The products table has category_id, and the categories table has id and name. Which approach is better for performance and why?

Options:
A) Use a JOIN to combine products and categories.
B) Use a subquery in SELECT to get category name for each product.
C) Use a subquery in WHERE to filter products by category name.
D) Use UNION to combine products and categories.
hard
A. Subquery in SELECT is better because it runs once per product.
B. JOIN is better because it retrieves all data in one step efficiently.
C. Subquery in WHERE is better because it filters early.
D. UNION is better because it merges tables.

Solution

  1. Step 1: Understand the data retrieval goal

    You want product info with category names, which requires combining data from two tables.
  2. Step 2: Compare approaches

    JOIN combines tables in one efficient operation. Subqueries in SELECT run once per row, causing slower performance. Subquery in WHERE filters but doesn't retrieve category names. UNION merges rows, not related here.
  3. Final Answer:

    JOIN is better because it retrieves all data in one step efficiently. -> Option B
  4. Quick Check:

    JOIN efficiency > subqueries for this task [OK]
Hint: JOIN combines tables efficiently for related data [OK]
Common Mistakes:
  • Using subquery in SELECT causing slow per-row lookup
  • Confusing UNION with JOIN
  • Using subquery in WHERE without retrieving needed data