Advertisement
heavenriver

ex02.sql

Oct 23rd, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.41 KB | None | 0 0
  1. -- initialisation: creates tables
  2. use omnibox;
  3. drop table if exists member;
  4. drop table if exists practice;
  5. create table if not exists member
  6.     (person varchar(20) primary key,
  7.      club varchar(20),
  8.      firstyear integer(4));
  9. create table if not exists practice
  10.     (person varchar(20),
  11.      sport varchar(10));
  12.  
  13. -- populates tables
  14. insert into member values
  15.     ('Aaron', 'chess', 2007),
  16.     ('Bradley', 'poker', 2004),
  17.     ('Catherine', 'animal rights', 2010),
  18.     ('Daniel', 'astronomy', 2000),
  19.     ('Eugene', 'chess', 2005),
  20.     ('Flannery', 'poker', 2012),
  21.     ('Gordon', 'astronomy', 2003),
  22.     ('Heather', 'animal rights', 2009),
  23.     ('Ivan', 'poker', 2002),
  24.     ('Jason', 'astronomy', 2006),
  25.     ('Kate', 'sewing', 2011),
  26.     ('Leroy', 'bungee jumping', 2008),
  27.     ('Martin', 'astronomy', 2013);
  28.  
  29. insert into practice values
  30.     ('Aaron', 'surf'),
  31.     ('Bradley', 'tennis'),
  32.     ('Bradley', 'running'),
  33.     ('Cyrus', 'tennis'),
  34.     ('Cyrus', 'gym'),
  35.     ('Cyrus', 'skating'),
  36.     ('Daniel', 'running'),
  37.     ('Flannery', 'surf'),
  38.     ('Flannery', 'skating'),
  39.     ('Gordon', 'tennis'),
  40.     ('Gordon', 'skating'),
  41.     ('Ivan', 'running'),
  42.     ('Josh', 'football'),
  43.     ('Kate', 'tennis'),
  44.     ('Kate', 'gym');
  45.  
  46. -- EXERCISE 2.1
  47. -- shows groups with 2+ members, of which 1+ member practices 1+ sports
  48. select distinct m1.club
  49.     from member m1, member m2, practice s
  50.     where m1.club = m2.club
  51.           and m1.person <> m2.person          -- clubs that have at least two members
  52.           and m1.person = s.person;           -- and that are attended by at least one member who does sports
  53.  
  54. -- EXERCISE 2.2
  55. -- for each group, selects the person(s) who has (have) joined most recently
  56. create view maxyears
  57.     as select club, max(firstyear) as maxyear
  58.         from member
  59.         group by club;
  60. select m.person
  61.     from member m, maxyears y
  62.     where m.club = y.club
  63.           and m.firstyear = y.maxyear;
  64. drop view if exists maxyears;
  65.  
  66. -- EXERCISE 2.3
  67. -- selects homogeneous groups: groups of people practising the same sport
  68. create view allsports
  69.     as select distinct sport
  70.         from practice;
  71. select p.sport, p.person
  72.     from practice p, allsports s
  73.     where p.sport = s.sport
  74.     order by sport;
  75. drop view if exists allsports;
  76.  
  77. -- EXERCISE 2.4
  78. -- shows the people who do not practise sports
  79. select m.person
  80.     from member m
  81.     where m.person not in (select distinct person
  82.                                 from practice);
  83.  
  84. -- removes data
  85. drop view if exists maxyears;
  86. drop view if exists allsports;
  87. drop table if exists member;
  88. drop table if exists practice;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement