0
0
SQLquery~20 mins

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

Choose your learning style9 modes available
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.