UGC-NET Aug 2016 · SQL Queries

MCQ · 2 marks · Problem DBMS-SQL-0007

Consider the following ORACLE relations :One $(x, y) = \{<2, 5>, <1, 6>, <1, 6>, <1, 6>, <4, 8>, <4, 8> \}$ Two $(x, y) = \{<2, 55>, <1, 1>, <4, 4>, <1, 6>, <4, 8>, <4, 8>, <9, 9>, <1, 6> \}$Consider the following two $SQL$ queries $SQ1$ and $SQ2$ :SQ1 :      SELECT * FROM One)               EXCEPT     (SELECT * FROM Two);SQ2 :        SELECT * FROM One)             EXCEPT ALL        (SELECT * FROM Two);For each of the SQL queries, what is the cardinality (number of rows) of the result obtained when applied to the instances above ?
  1. A
    2 and 1 respectively
  2. B
    1 and 2 respectively
  3. C
    2 and 2 respectively
  4. D
    1 and 1 respectively

Solve it on GATE DA practice →More Relational Model & SQL problems

Show the worked solutionEXCEPT removes duplicates (set semantics); EXCEPT ALL subtracts multiplicities (bag semantics). As a set, One is \(\{(2,5),(1,6),(4,8)\}\). Since \((1,6)\) and \((4,8)\) occur in Two, SQ1 returns \(\{(2,5)\}\), which is 1 row. For SQ2, One has \((2,5)\times1,\ (1,6)\times3,\ (4,8)\times2\), and Two has \((1,6)\times2,\ (4,8)\times2\).\[(2,5)\times(1-0),\quad(1,6)\times(3-2),\quad(4,8)\times(2-2)\]This leaves 2 rows.Answer: B

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com