Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Union and intersection

327 views

Published on

  • Be the first to comment

  • Be the first to like this

Union and intersection

  1. 1. Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  2. 2. Union and IntersectionZabeeb anwarzabeebanwar@gmail.comwww.fb.com/zabeebanwartwitter.com/zabeebanwarin/linkedin.com/in/zabeeb9895599689
  3. 3. unionAUB- AUB(OR) means the union of sets A and Bcontains all of the elements of both A and B.
  4. 4. IntersectionAÇB(AND) means the intersection of sets Aand B. This contains all of the elements whichare in both A and B.
  5. 5. Union and Intersection with empty
  6. 6. Union and Intersection in Psql
  7. 7. Union- A hot startup is holding a special event andwants to send out invites for my marriage()to some of my best clients and also to someVIPs.- Some of the VIPs are actually very supportiveof the site and are clients too.- What query will provide the complete set ofpeople to invite avoiding duplicate records?- Here is our data.
  8. 8. 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);
  9. 9. Union• hotstartup=# select * from clients union select *from vips;name----------------SharanVineeshBalaSheethalReshmiAnupaAshwathy(7 rows)
  10. 10. Union All• hotstartup=# select * from clients union all select * from vips;name----------------SharanVineeshBalaSheethalReshmisheethalAnupaAshwathy(8 rows)
  11. 11. Intersect• if I want to get the list of people who are bothclients and VIP we can use INTERSECT.• hotstartup=# select * fromclients intersect select * from vips;name----------------Sheethal(1 row)
  12. 12. Intersect All• Lets insert a Sheethal(duplicate name) into VIP’s.name----------------SharanVineeshBalaSheethalSheethal(4 rows)-select * from client intersect all select * from vips;
  13. 13. Intersect Allname----------------sheethalsheethal(2 rows)- Sheethal appears in both tables twice so wefind two matching pairs for her and hence tworows appears in the results.
  14. 14. Except• I want everyone on the clients list EXCEPT those onthe VIP list.• select * from clients except select * from vips;name----------------SharanVineeshBala
  15. 15. Except All• Lets insert a Sheethal(duplicate name) into clients.name----------------SharanVineeshBalaSheethalSheethal(4 rows)-select * from clients except all select * from vips;
  16. 16. Except Allname----------------SharanVineeshBalaSheethal(4 rows)
  17. 17. Where Clause• select * from Clients_Year;name | Birth year------------------+------Sharan | 1976Vineesh | 1977Bala | 1978Ashwathy | 1983Reshmi | 199 3sheethal | 1996Anupa | 1997
  18. 18. Where Clause• select * from clients_year where yearbetween 1970 and 1979 union select * fromceos where year=1977;name | Birth year------------------+------Sharan | 1976Vineesh | 1977Bala | 1978
  19. 19. Do not• select * from clients where year between1970 and 1979 union select name from clientswhere year=1977;ERROR: each UNION query must have thesame number of columns
  20. 20. Union and intersection in Python
  21. 21. Example>>>engineers = Set([John, Jane, Jack,Janice])>>>programmers = Set([Jack, Sam, Susan,Janice])>>>managers = Set([Jane, Jack, Susan,Zack])
  22. 22. Union>>>employees = engineers | programmers |managers>>>print “employees”Set([Jane, Janice, John’,Jack’,’susan’,’Zack’,’sam’])
  23. 23. Intersection>>>engineers = Set([John, Jane, Jack,Janice])>>>managers = Set([Jane, Jack, Susan,Zack])>>>engineering_management = engineers &managers>>>print “engineering_management”Set([Jane, Jack’])
  24. 24. Questions
  25. 25. If this presentation helped you, please visit ourpage facebook.com/baabtra and like it.Thanks in advance.www.baabtra.com | www.massbaab.com |www.baabte.com
  26. 26. Contact Us

×