Advertisement
lawliet89

Coursework 1

Feb 12th, 2012
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.34 KB | None | 0 0
  1. -- Q1
  2. CREATE TABLE monarch
  3.        (name VARCHAR(35) NOT NULL,
  4.         house VARCHAR(8),
  5.         accession DATE NOT NULL,
  6.         coronation DATE,
  7.         CONSTRAINT monarch_pkey PRIMARY KEY (name),
  8.         CONSTRAINT monarch_name_fkey FOREIGN KEY (name) REFERENCES person
  9.         );
  10.  
  11.  
  12. -- Q2
  13.  
  14. SELECT DISTINCT
  15.         personA.name, personB.name, personA.born_in
  16.         FROM person AS personA
  17.         JOIN person AS personC ON personA.name = personC.father
  18.         JOIN person AS personB     ON personB.name = personC.mother
  19.                                 AND personA.born_in = personB.born_in;
  20.  
  21.  
  22. -- Q3
  23. SELECT name
  24.     FROM monarch
  25.     WHERE house IS NOT NULL AND coronation IS NULL;
  26.  
  27. -- Q4
  28. SELECT personA.name, personB.name AS father, personC.name AS mother
  29.     FROM person AS personA
  30.         JOIN person AS personB ON personA.dod < personB.dod AND personA.father = personB.name
  31.         JOIN person AS personC ON personA.dod < personC.dod AND personA.mother = personC.name;
  32.  
  33. -- Q5
  34.  
  35.     SELECT name          
  36.     FROM prime_minister
  37. UNION
  38.     SELECT name
  39.     FROM monarch
  40.     WHERE monarch.house IS NOT NULL;
  41.  
  42. -- Q6
  43. SELECT DISTINCT monarch1.name            -- Distinct because an abdicated monarch can outlive the accession of more than one future monarchs and will be returned twice
  44.     FROM monarch AS monarch1 JOIN person ON monarch1.name = person.name
  45.         JOIN monarch AS monarch2     ON monarch2.accession > monarch1.accession             -- Monarch2 is to come after monarch1
  46.                                     AND monarch2.accession < person.dod -- And then monarch2 has an accession before monarch1 has died
  47.                                     AND monarch1.house IS NOT NULL;
  48.  
  49.  
  50. -- Q7
  51. SELECT personA.name
  52.     FROM person AS personA
  53.     WHERE NOT EXISTS    (
  54.                         SELECT personB.name
  55.                             FROM person AS personB
  56.                             WHERE    personB.father = personA.name
  57.                                     OR personB.mother = personA.name
  58.                         );
  59.  
  60. -- Q8
  61. SELECT monarch_list.monarch_name, pm_list.pm_name
  62. FROM
  63.     (SELECT monarch1.name AS monarch_name, monarch1.accession AS reign_start,
  64.         CASE WHEN
  65.             monarch2.accession <> monarch1.accession
  66.         THEN
  67.             monarch2.accession
  68.         ELSE
  69.             NULL        -- if monarch is living, set as NULL for comparsion purpose for the outer query
  70.                         -- if we can use CURRENT_DATE (which is standard SQL unsupported by MS SQL), will only create four cases for the outer query
  71.         END AS reign_end        
  72.     FROM monarch AS monarch1 JOIN monarch AS monarch2
  73.         ON monarch2.accession = (SELECT MIN(accession) FROM monarch WHERE monarch.accession > monarch1.accession)
  74.         -- the OR clause will cause monarch2 to match monarch1 in case monarch1 is still reigning. Then we will set reign_end accordingly with the CASE clause in SELECT
  75.         OR ( monarch2.accession > ALL (SELECT accession FROM monarch WHERE name <> monarch2.name) AND (SELECT MIN(accession) FROM monarch WHERE monarch.accession > monarch1.accession) IS NULL)
  76.     WHERE monarch1.house IS NOT NULL)
  77.     AS monarch_list
  78. JOIN
  79.     (SELECT pm1.name AS pm_name, pm1.entry AS term_start,
  80.         CASE WHEN
  81.             pm2.entry <> pm1.entry
  82.         THEN
  83.             pm2.entry
  84.         ELSE
  85.             NULL        -- if the PM is still in office, set as NULL for outer query purposes
  86.                         -- if we can use CURRENT_DATE (which is standard SQL unsupported by MS SQL), will only create four cases for the outer query
  87.         END AS term_end
  88.     FROM prime_minister AS pm1 JOIN prime_minister AS pm2
  89.         ON pm2.entry = (SELECT MIN(entry) FROM prime_minister WHERE prime_minister.entry > pm1.entry)
  90.         -- the OR clause will cause pm2 to match pm1 in case pm1 is still in office. Then we will set term_end accordingly with the CASE clause in SELECT
  91.         OR ( pm2.entry > ALL (SELECT entry FROM prime_minister WHERE name <> pm2.name AND entry <> pm2.entry) AND (SELECT MIN(entry) FROM prime_minister WHERE prime_minister.entry > pm1.entry) IS NULL)
  92.     ) AS pm_list
  93.     -- Five mutually exclusive cases --
  94.     ON (pm_list.term_start BETWEEN monarch_list.reign_start AND monarch_list.reign_end) -- PM term began within monarch reign and ended either during or after reign
  95.     OR (pm_list.term_start < monarch_list.reign_start
  96.         AND pm_list.term_end BETWEEN monarch_list.reign_start AND monarch_list.reign_end)    -- PM term began before monarch reign but continues during the monarch reign
  97.     OR (pm_list.term_start < monarch_list.reign_start
  98.         AND pm_list.term_end > monarch_list.reign_end)    -- PM term is more than monarch reign i.e. Edward VIII
  99.     OR (pm_list.term_start >= monarch_list.reign_start
  100.         AND monarch_list.reign_end IS NULL)               -- monarch is currently reigning and PM term begins during monarch reign and has either ended or still in office
  101.     OR (pm_list.term_start < monarch_list.reign_start
  102.         AND monarch_list.reign_end IS NULL
  103.         AND (pm_list.term_end > monarch_list.reign_start
  104.                 OR pm_list.term_end IS NULL))             -- PM's term began before currently reigning monarch and has either ended or still in office
  105.        
  106. ORDER BY monarch_list.reign_start, pm_list.term_start; -- Human readability
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement