28

Union and intersection

Embed Size (px)

DESCRIPTION

Union and intersection

Citation preview

Page 1: Union and intersection
Page 2: Union and intersection

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 3: Union and intersection

Union and Intersection

Zabeeb anwar [email protected] www.fb.com/zabeebanwar twitter.com/zabeebanwar

in/linkedin.com/in/zabeeb 9895599689

Page 4: Union and intersection

union

AUB- AUB(OR) means the union of sets A and B

contains all of the elements of both A and B.

Page 5: Union and intersection

Intersection

AÇB(AND) means the intersection of sets A and B. This contains all of the elements which are in both A and B.

Page 6: Union and intersection

Union and Intersection with empty

Page 7: Union and intersection

Union and Intersection in Psql

Page 8: Union and intersection

Union

- A hot startup is holding a special event and wants to send out invites for my marriage() to some of my best clients and also to some VIPs.

- Some of the VIPs are actually very supportive of the site and are clients too.

- What query will provide the complete set of people to invite avoiding duplicate records?

- Here is our data.

Page 9: Union and intersection

Union

• create database hotstartup;• create table clients(name varchar);

insert into clients values(‘Sharan');insert into clients values(‘Vineesh');insert into clients values(‘Bala');insert into clients values(‘Sheethal');

• create table vips(name varchar);insert into vips values(‘Reshmi');insert into vips values(‘Sheethal');insert into vips values(‘Anupa');insert into vips values(‘Ashwathy');

Page 10: Union and intersection

Union• hotstartup=# select * from clients union select *

from vips; name

---------------- Sharan Vineesh Bala Sheethal

Reshmi Anupa Ashwathy(7 rows)

Page 11: Union and intersection

Union All• hotstartup=# select * from clients union all select * from vips;

name ----------------SharanVineeshBalaSheethal

Reshmi sheethal Anupa

Ashwathy (8 rows)

Page 12: Union and intersection

Intersect

• if I want to get the list of people who are both clients and VIP we can use INTERSECT.

• hotstartup=# select * from clients intersect select * from vips; name ---------------- Sheethal

(1 row)

Page 13: Union and intersection

Intersect All• Let's insert a Sheethal(duplicate name) into VIP’s. name

----------------SharanVineeshBalaSheethal

Sheethal (4 rows)

-select * from client intersect all select * from vips;

Page 14: Union and intersection

Intersect All

name ----------------

sheethal sheethal

(2 rows) - Sheethal appears in both tables twice so we

find two matching pairs for her and hence two rows appears in the results.

Page 15: Union and intersection

Except• I want everyone on the clients list EXCEPT those on

the VIP list.• select * from clients except select * from vips; name

----------------SharanVineeshBala

Page 16: Union and intersection

Except All• Let's insert a Sheethal(duplicate name) into clients. name

----------------SharanVineeshBalaSheethal

Sheethal (4 rows)-select * from clients except all select * from vips;

Page 17: Union and intersection

Except All

name ----------------

SharanVineeshBala

Sheethal (4 rows)

Page 18: Union and intersection

Where Clause

• select * from Clients_Year; name | Birth year ------------------+------ Sharan | 1976 Vineesh | 1977 Bala | 1978 Ashwathy | 1983 Reshmi | 199 3 sheethal | 1996 Anupa | 1997

Page 19: Union and intersection

Where Clause

• select * from clients_year where year between 1970 and 1979 union select * from ceos where year=1977;

name | Birth year ------------------+------ Sharan | 1976 Vineesh | 1977 Bala | 1978

Page 20: Union and intersection

Do not

• select * from clients where year between 1970 and 1979 union select name from clients where year=1977;ERROR: each UNION query must have the same number of columns

Page 21: Union and intersection

Union and intersection in Python

Page 22: Union and intersection

Example

>>>engineers = Set(['John', 'Jane', 'Jack', 'Janice'])

>>>programmers = Set(['Jack', 'Sam', 'Susan', 'Janice'])

>>>managers = Set(['Jane', 'Jack', 'Susan',

'Zack'])

Page 23: Union and intersection

Union

>>>employees = engineers | programmers | managers

>>>print “employees”Set(['Jane', 'Janice', 'John’,

'Jack’,’susan’,’Zack’,’sam’])

Page 24: Union and intersection

Intersection

>>>engineers = Set(['John', 'Jane', 'Jack', 'Janice'])

>>>managers = Set(['Jane', 'Jack', 'Susan', 'Zack'])

>>>engineering_management = engineers & managers

>>>print “engineering_management”Set(['Jane', 'Jack’])

Page 25: Union and intersection

Questions

Page 26: Union and intersection
Page 27: Union and intersection

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 28: Union and intersection

Contact Us