Thread: Query regarding Intersect clause

Query regarding Intersect clause

From
Ajit Pradnyavant
Date:
Sir,

I have created the following tables,

Create table abc (srno int, name varchar(32))
Create table def (srno int, name varchar(32))

    abc
srno    name
1    Aaaa
2    Bbbb

    def
srno    name
1    Aaaa
2    Cccc

each having two tuples.

If I run the following query I get the following result.
(Select name from abc) intersect (select name from def)

Srno    Name
1    Aaaa

If I run the following query I get the following result.
(Select name from abc) intersect all (select name from def)

Srno    Name
1    Aaaa

I think result of INTERSECT ALL query may be :

Srno    Name
1    Aaaa
1    Aaaa

Because  intersect all clause returns the duplicate values.
Please tell me I am wrong or correct.

Thank you in anticipation.

--
Sincerely,
Ajit Pradnyavant.
Email:- ajit.pradnyavant@gmail.com
Phone No. 7385758326

Re: Query regarding Intersect clause

From
Tom Lane
Date:
Ajit Pradnyavant <ajit.pradnyavant@gmail.com> writes:
> I think result of INTERSECT ALL query may be :

> Srno    Name
> 1    Aaaa
> 1    Aaaa

> Because  intersect all clause returns the duplicate values.

No; per the documentation at
http://www.postgresql.org/docs/9.1/static/sql-select.html#SQL-INTERSECT

    The result of INTERSECT does not contain any duplicate rows unless the
    ALL option is specified. With ALL, a row that has m duplicates in the
    left table and n duplicates in the right table will appear min(m,n)
    times in the result set.

So a single instance of Aaaa is appropriate for your example.  (This
definition is per the SQL standard btw.)

            regards, tom lane