Relational Joins — Returning Only Matching Rows When you want a query result set to include only rows that have matching values in both tables, which join type is the standard choice?
-
AEqui-join
-
BNatural join
-
COuter join
-
DAll of the above.
-
ECross join
Answer
Correct Answer: Equi-join
Explanation
Introduction:Most day-to-day reporting and OLTP queries need rows that match on a key in both tables. Knowing which join type expresses this intent ensures predictable results and good performance with proper indexing.
Given Data / Assumptions:
- Two tables share a logical key for matching rows.
- We are not trying to retain unmatched rows.
- Standard SQL join semantics apply.
Concept / Approach:An inner join returns only rows that satisfy the join condition. An equi-join is the most common inner join, using equality conditions such as T1.key = T2.key. A natural join is also an inner join but automatically uses all identically named columns, which can be risky. In contrast, outer joins are chosen when you need to retain non-matching rows from one or both sides.
Step-by-Step Solution:Identify the matching key(s) across the two tables.Write an inner equi-join using explicit ON clauses for clarity.Confirm that only rows meeting the equality condition are returned.Avoid outer joins here because they preserve non-matching rows.
Verification / Alternative check:Compare an INNER JOIN to a LEFT OUTER JOIN on the same data; the inner join will have fewer or equal rows because it filters out non-matching records.
Why Other Options Are Wrong:
- Natural join is an inner join but relies on column-name matching, which is error-prone; equi-join is the clearer standard answer.
- Outer join includes non-matching rows, which the question does not want.
- All of the above is not precise because outer joins behave differently.
- Cross join produces a Cartesian product without a matching condition.
Common Pitfalls:Using NATURAL JOIN and accidentally joining on unintended columns with the same name. Prefer explicit equi-joins with ON clauses.
Final Answer:Equi-join