0
0
PostgreSQLquery~20 mins

Join algorithms (nested loop, hash, merge) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Join Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Nested Loop Join

Which of the following best describes how a nested loop join works in PostgreSQL?

AIt scans each row of the first table and for each row, scans the entire second table to find matching rows.
BIt uses an index on both tables to directly find matching rows without scanning.
CIt creates a hash table from the smaller table and probes it with rows from the larger table.
DIt sorts both tables and then merges them by matching sorted keys.
Attempts:
2 left
💡 Hint

Think about the simplest join method that compares every row from one table to every row of the other.

query_result
intermediate
2:00remaining
Output of Hash Join Query

Given two tables employees and departments, which query output matches the use of a hash join in PostgreSQL?

Assume employees has 1000 rows and departments has 10 rows.

PostgreSQL
EXPLAIN (ANALYZE, BUFFERS) SELECT e.name, d.name FROM employees e JOIN departments d ON e.department_id = d.id;
AThe query plan shows a Bitmap Heap Scan without any join operation.
BThe query plan shows a Nested Loop Join scanning all rows of both tables.
CThe query plan shows a Merge Join after sorting both tables on join keys.
DThe query plan shows a Hash Join with a hash built on the smaller table 'departments'.
Attempts:
2 left
💡 Hint

Hash joins are efficient when one table is much smaller and can be hashed in memory.

📝 Syntax
advanced
2:00remaining
Correct SQL for Merge Join Usage

Which SQL query is most likely to trigger a merge join in PostgreSQL?

ASELECT * FROM orders o JOIN customers c ON o.customer_id = c.id ORDER BY o.customer_id, c.id;
BSELECT * FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.amount > 100;
CSELECT * FROM orders o LEFT JOIN customers c ON o.customer_id = c.id;
DSELECT * FROM orders o JOIN customers c ON o.customer_id = c.id;
Attempts:
2 left
💡 Hint

Merge joins require both tables to be sorted on the join keys.

optimization
advanced
2:00remaining
Choosing the Best Join Algorithm

You have two tables: big_sales with 1 million rows and small_regions with 100 rows. Which join algorithm will PostgreSQL most likely choose for SELECT * FROM big_sales JOIN small_regions ON big_sales.region_id = small_regions.id; and why?

AMerge join, because it sorts both tables before joining.
BNested loop join, because it scans each row of the big table and matches with the small table.
CHash join, because it builds a hash table on the smaller table for fast lookups.
DCross join, because it multiplies all rows from both tables.
Attempts:
2 left
💡 Hint

Think about which join is efficient when one table is very small.

🔧 Debug
expert
3:00remaining
Diagnosing Unexpected Join Algorithm

You wrote this query:

SELECT * FROM products p JOIN categories c ON p.category_id = c.id;

PostgreSQL uses a nested loop join instead of a hash or merge join, causing slow performance. Which of the following is the most likely reason?

AThe query uses SELECT *, which disables hash and merge joins.
BThere is no index on the join keys, so PostgreSQL cannot efficiently build a hash or merge join.
CThe tables are too small, so PostgreSQL always uses nested loop joins regardless of indexes.
DPostgreSQL does not support hash or merge joins for JOIN operations.
Attempts:
2 left
💡 Hint

Indexes help PostgreSQL choose efficient join methods.