Complete the code to perform a nested loop join between tables employees and departments on department_id.
SELECT e.name, d.name FROM employees e [1] JOIN departments d ON e.department_id = d.department_id;In SQL, the keyword INNER JOIN is used to join tables. The actual join algorithm (nested loop, hash, merge) is chosen by the database engine internally and is not specified in the query.
Complete the EXPLAIN command to check the query plan for a hash join between employees and departments.
EXPLAIN [1] SELECT e.name, d.name FROM employees e JOIN departments d ON e.department_id = d.department_id;The EXPLAIN ANALYZE command runs the query and shows the actual execution plan including timing and join algorithms like hash join.
Fix the error in the query that tries to force a merge join by setting the join type incorrectly.
SET join_collapse_limit = 1; SET enable_hashjoin = off; SET enable_nestloop = off; SET enable_mergejoin = on; SELECT * FROM employees e [1] JOIN departments d ON e.department_id = d.department_id;
The join type in the SQL query should be INNER JOIN. The database decides the join algorithm (merge join here) based on the settings, not the join keyword.
Fill both blanks to create a hash join query that selects employee names and department names where department_id matches.
SELECT e.name, d.name FROM employees e [1] JOIN departments d ON e.department_id [2] d.department_id;
The correct join type is INNER JOIN and the join condition uses the equality operator = to match department IDs.
Fill all three blanks to write a query that forces a nested loop join by disabling other join types and selecting employee and department names.
SET enable_hashjoin = [1]; SET enable_mergejoin = [2]; SET enable_nestloop = [3]; SELECT e.name, d.name FROM employees e INNER JOIN departments d ON e.department_id = d.department_id;
To force a nested loop join, disable hash join and merge join by setting them to off, and enable nested loop join by setting it to on.