Interview Question, MS SQL, MS SQL

1. INNER, LEFT, RIGHT and CROSS Result Count

What is result when we are joining these two table (INNER, LEFT, RIGHT and CROSS Joins)

INNER JOIN
Matches only where both tables have the same non-NULL value.
Matching values:
Table1 has 1 twice → matches Table2’s 1 once → 2 rows
No other matching values (NULL does not match NULL)

All rows from Table1 + matching rows from Table2
(Unmatched rows from Table1 become NULL on right side)


LEFT JOIN
Breakdown:

Table1 valueMatching Table2 rowsCount
11 match × occurs 2 times2
201
501
NULLNULL does not match NULL → 02

LEFT JOIN Result Count = 6
(because Table1 has 6 rows total)


RIGHT JOIN

All rows from Table2 + matching rows from Table1
(Unmatched Table2 rows get NULL on left side)

Breakdown:

Table2 valueMatching Table1 rowsCount
302
402
1Table1 has 1 twice → 22
NULLno match1

Total = 2 + 2 + 2 + 1 = 7
RIGHT JOIN Result Count = 7


CROSS JOIN

All possible combinations of Table1 × Table2

  • Table1 rows = 6
  • Table2 rows = 6

CROSS JOIN Count = 6 × 6 = 36

Leave a Reply

Prabhakaran Jayaraman