CS FundamentalsVery hardGATE CS
E(id, dept) = (1, 10), (2, 10), (3, 20), (4, 30), (5, NULL), (6, 20), (7, 40) and D(did, loc) = (10, X), (20, Y), (20, X), (30, Y), (50, X). Let Q1 = SELECT * FROM E LEFT JOIN D ON E.dept = D.did AND D.loc = 'X' and Q2 = SELECT * FROM E LEFT JOIN D ON E.dept = D.did WHERE D.loc = 'X'. How many rows do Q1 and Q2 return, respectively?
- A. 7 and 4
- B. 4 and 7
- C. 9 and 4
- D. 7 and 9
Correct answer: A. 7 and 4
A condition in ON only decides which D rows match, and unmatched E rows are still kept with NULLs (Q1 = 7); the same condition in WHERE removes the NULL-padded rows after the join (Q2 = 4). Plain LEFT JOIN would give 9 rows.
More DBMS and SQL questions
- S(v) holds the nine rows 10, 20, 20, 30, 30, 30, 40, 40, 50. The query SELECT SUM(v) OVER (ORDER BY v) FROM S uses the default frame. What…
- Emp(id, dept, sal) = (1, A, 50), (2, A, 70), (3, A, NULL), (4, B, 40), (5, B, 60), (6, B, 60), (7, NULL, 90), (8, C, 30). How many rows…
- Ed(a, b) = (1, 2), (1, 3), (2, 4), (3, 4), (4, 5), (4, 6), (5, 7), (6, 7), (2, 5) is a directed acyclic graph. The query WITH RECURSIVE…
- O(cust, item) = (c1, x), (c1, x), (c1, y), (c2, z), (c2, NULL), (c3, NULL), (c3, NULL), (c4, w), (c4, w), (c5, p), (c5, q), (c5, p). How…
- R(x) = (1, 2, 2, 3, NULL, NULL, 5), S(x) = (2, 3, 3, NULL, 6), T(x) = (2, 2, 3, NULL, 5, 7) (one column each). How many rows does (SELECT…
- P(g, v) = (a, 5), (a, 7), (a, 7), (b, 3), (b, NULL), (b, 9), (c, 4), (c, 4), (d, NULL), (NULL, 1), (NULL, 2). How many rows does SELECT…