Skip to main content

GATE CSE 2018Databases › SQLVerified

GATE CSE 2018 PYQ: Consider the following two tables and four queries in SQL. Book (isbn, bname), Stock(isbn, copies) Query 1:…

Consider the following two tables and four queries in SQL.

Book (isbn, bname), Stock(isbn, copies)

Query 1: 

SELECT B.isbn, S.copies FROM Book B INNER JOIN Stock S ON B.isbn=S.isbn;

Query 2:  

SELECT B.isbn, S.copies FROM Book B LEFT OUTER JOIN Stock S ON B.isbn=S.isbn;

Query 3: 

SELECT B.isbn, S,copies FROM Book B RIGHT OUTER JOIN Stock S ON B.isbn=S.isbn

Query 4: 

SELECT B.isbn, S.copies FROM Book B FULL OUTER JOIN Stock S ON B.isbn=S.isbn

Which one of the queries above is certain to have an output that is a superset of the outputs of the other three queries?

  1. A
    Query 1
  2. B
    Query 2
  3. C
    Query 3
  4. D
    Query 4
    Correct answer

Answer

(D) Query 4

Approach

Compare the row sets produced by INNER JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN, and FULL OUTER JOIN on the same join condition. A FULL OUTER JOIN preserves matched rows plus unmatched rows from both tables, so it contains every row produced by the other three join types.

A FULL OUTER JOIN is the union of the matching rows, the left-only unmatched rows, and the right-only unmatched rows. Since Query 1, Query 2, and Query 3 each produce only some of these categories, Query 4 is certain to have an output that is a superset of all three.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.