0
0
PostgreSQLquery~10 mins

Join algorithms (nested loop, hash, merge) in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to perform a nested loop join between tables employees and departments on department_id.

PostgreSQL
SELECT e.name, d.name FROM employees e [1] JOIN departments d ON e.department_id = d.department_id;
Drag options to blanks, or click blank then click option'
AMERGE
BNESTED LOOP
CHASH
DINNER
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to specify the join algorithm directly in the SQL query.
Using unsupported keywords like NESTED LOOP in the JOIN clause.
2fill in blank
medium

Complete the EXPLAIN command to check the query plan for a hash join between employees and departments.

PostgreSQL
EXPLAIN [1] SELECT e.name, d.name FROM employees e JOIN departments d ON e.department_id = d.department_id;
Drag options to blanks, or click blank then click option'
AANALYZE
BVERBOSE
CCOSTS
DBUFFERS
Attempts:
3 left
💡 Hint
Common Mistakes
Using EXPLAIN without ANALYZE to see actual join algorithms.
Using unrelated EXPLAIN options that don't run the query.
3fill in blank
hard

Fix the error in the query that tries to force a merge join by setting the join type incorrectly.

PostgreSQL
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;
Drag options to blanks, or click blank then click option'
AINNER
BMERGE
CHASH
DNESTED LOOP
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to write MERGE JOIN as a join type in the SQL query.
Confusing join algorithm with join type keywords.
4fill in blank
hard

Fill both blanks to create a hash join query that selects employee names and department names where department_id matches.

PostgreSQL
SELECT e.name, d.name FROM employees e [1] JOIN departments d ON e.department_id [2] d.department_id;
Drag options to blanks, or click blank then click option'
AINNER
B=
C<>
DLEFT
Attempts:
3 left
💡 Hint
Common Mistakes
Using LEFT JOIN when INNER JOIN is needed.
Using <> instead of = in the join condition.
5fill in blank
hard

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.

PostgreSQL
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;
Drag options to blanks, or click blank then click option'
Aon
Boff
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'true' or 'false' instead of 'on' or 'off'.
Not disabling both hash and merge joins.