Sei sulla pagina 1di 6

.

MODULE : EDB3

DAY : Monday

DATE : May 13th, 2019

TIME : 16:15 – 18:15

Module EDB3

Lecturer FRN, GPT, KHST, LPPR

Supervisor KHST

Room 2.40, 2.70, 2.72, 2.84

Distribution of points:

Exercise 1 2 3 4 5 6 7 8 Total
Points 18 12 12 12 12 12 12 10 100

Number of pages: 6

Remarks:

- All exercises refer to the Presidents database as described in appendix 1

- Copy your solutions into a (one) text file.

- Name the text file: “yourstudentnumber_yourname_EDB3”

- Do not forget to state your name, student number and class inside the text file!

- For SQL queries that do not run successfully in SQL developer (i.e. generating errors),
no points will be given.

- All code must be properly indented. Points will be withdrawn for each solution that
isn’t indented!

Success!

Page 1 of 6
EDB3 Exam, May 13th 2019
Exercise 1 to 7:

Write an SQL statement for each of the following queries.


The queries operate on the ‘Presidents’ database. A description of this database
is provided in Appendix 1 on pages 4, 5 and 6.

1. [18p] Create a view candidatesWithNarrowWin that gives the election years, the
candidates that won the elections in those years and the number of gained votes of
the winning candidates, for those election years where there were exactly two
candidates and the difference between the number of gained votes of the winner
and the number of gained votes of the loser was smaller than 10. Rename the
columns of the view to electionYear, winningCandidateName, winningNrOfVotes.

2. [12p] Give the president names, parties, years of service and death ages of the
presidents, that served the smallest amount of years of presidents of their party, and
that died younger than the average age of death of presidents of their party.

3. [12p] Give the administration numbers, president names and years of inauguration
of the presidents that were inaugurated after 1900 and were both president and vice
president in administrations with the same administration number (for example
president COOLIDGE C was first vice president and later president in
administrations with administration number 34). The results must be in ascending
order of administration number.

4. [12p] Give the president names, years of birth, states of birth and the years their
states of birth entered the United States. Give all these details only when the year
entered for the state was 1845 or later and the president won all the elections he
was candidate for.

5. [12p] Give the president names and the hobbies of the presidents that served for at
least 5 years, and were married two or more times and had children from all the
marriages. If the president does not have hobbies “no hobbies” must be given.

6. [12p] Give the president names and years of service, for the presidents that were
born in the state(s) where the most presidents were born.

Page 2 of 6
EDB3 Exam, May 13th 2019
7. [12p] Create a query that displays the following output: for each age of the president
at the marriage, the number of presidents married at that age, the number of
presidents married at that age with a spouse with the same age and the number of
presidents married at that age with a spouse with a different age must be given.

For example:

PRES_AGE NR_PRES NR_PRES_SAME_AGE_SPOUSE NR_PRES_DIFF_AGE_SPOUSE

18 1 0 1
20 2 0 2
21 1 0 1
22 2 0 2
23 2 0 2
24 2 0 2
25 3 0 3
26 6 2 4
27 3 0 3
28 7 0 7
29 3 0 3
30 2 0 2
31 2 2 0
33 2 0 2
35 2 0 2
36 1 0 1
41 1 0 1
43 1 0 1
49 1 0 1
54 1 0 1
58 2 0 2
62 1 0 1

Exercise 8 [10p]

a. [3p] Is it allowed to perform DML operations on the view candidatesWithNarrowWin


from exercise 1? Explain your answer.
b. [4p] Give two advantages of views. Explain each advantage briefly.
c. [3p] Explain in plain English what is being returned after executing the following
statement:

SELECT e.last_name, m.last_name

FROM employees e LEFT JOIN employees m

ON e.manager_id = m.employee_id;

Page 3 of 6
EDB3 Exam, May 13th 2019
Appendix 1: The “Presidents” Database

The Create Table statements for this database and the Alter Table statements for creation of
the foreign key constraints for this database are provided on the next pages.

Page 4 of 6
EDB3 Exam, May 13th 2019
Attribute Datatype Description

CREATE TABLE PRESIDENT(


PRES_NAME VARCHAR2(20) NOT NULL, name of the president
BIRTH_YR NUMBER(4), calendar year of birth
YRS_SERV NUMBER(2), number of presidency years
DEATH_AGE NUMBER(2), age of death when president is deceased
PARTY VARCHAR2(10), political party
STATE_BORN VARCHAR2(15), native state (birth state)

CONSTRAINT PRIM_KEY_PRES PRIMARY KEY (PRES_NAME)


);

CREATE TABLE PRES_MARRIAGE (


PRES_NAME VARCHAR2(20) NOT NULL, name of the president
SPOUSE_NAME VARCHAR2(20) NOT NULL, name of the spouse
PR_AGE NUMBER(2) age of the president at the wedding
SP_AGE NUMBER(2), age of the spouse at the wedding
NR_CHILDREN NUMBER(2), the number of children who were
born in the marriage
MAR_YEAR NUMBER(4), calendar year in which the wedding
took place

CONSTRAINT PRIM_KEY_PRESMAR PRIMARY KEY (PRES_NAME,SPOUSE_NAME)


);

CREATE TABLE STATE(


STATE_NAME VARCHAR2(15) NOT NULL, the name of the state
PRESNAME_ENTERED VARCHAR2(20), the name of the president at the
moment when the state entered the
federation
YEAR_ENTERED NUMBER(4), the calendar year when the state
entered the federation

CONSTRAINT PRIM_KEY_STATE PRIMARY KEY (STATE_NAME)


);

CREATE TABLE ELECTION (


ELECTION_YEAR NUMBER(4) NOT NULL, year of the election
CANDIDATE VARCHAR2(20) NOT NULL, candidate name
VOTES NUMBER(3), number of gained votes
WINNER_LOSER_INDIC CHAR(1) ‘W’ if the candidate won the
election, ‘L’ if the candidate lost.

CONSTRAINT CHECK_WIN_LOS
CHECK (WINNER_LOSER_INDIC = 'W' OR WINNER_LOSER_INDIC = 'L'),
CONSTRAINT PRIM_KEY_ELEC PRIMARY KEY (ELECTION_YEAR,CANDIDATE)
);

Page 5 of 6
EDB3 Exam, May 13th 2019
Attribute Datatype Description

CREATE TABLE ADMINISTRATION (


ADMIN_NR NUMBER(2) NOT NULL, administration number
PRES_NAME VARCHAR2(20) NOT NULL, name of the president
YEAR_INAUGURATED NUMBER(4), inauguration year of the president

CONSTRAINT PRIM_KEY_ADM PRIMARY KEY (ADMIN_NR, PRES_NAME)


);

CREATE TABLE ADMIN_PR_VP(


ADMIN_NR NUMBER(2) NOT NULL, administration number
PRES_NAME VARCHAR2(20) NOT NULL, name of the president
VICE_PRES_NAME VARCHAR2(20) NOT NULL, name of the vice president

CONSTRAINT PRIM_KEY_ADMVP PRIMARY KEY (ADMIN_NR, PRES_NAME, VICE_PRES_NAME)

);

CREATE TABLE PRES_HOBBY(


PRES_NAME VARCHAR2(20) NOT NULL, name of the president
HOBBY VARCHAR2(20) NOT NULL, hobby of the president

CONSTRAINT PRIM_KEY_HOBBY PRIMARY KEY (PRES_NAME,HOBBY)


);

ALTER TABLE STATE


ADD CONSTRAINT FOR_KEY_STATE
FOREIGN KEY (ADMIN_ENTERED,PRESNAME_ENTERED)
REFERENCES ADMINISTRATION(ADMIN_NR, PRES_NAME);

ALTER TABLE PRESIDENT


ADD CONSTRAINT FOR_KEY_PRES
FOREIGN KEY (STATE_BORN) REFERENCES STATE(STATE_NAME);

ALTER TABLE PRES_MARRIAGE


ADD CONSTRAINT FOR_KEY_PRESMAR
FOREIGN KEY (PRES_NAME) REFERENCES PRESIDENT(PRES_NAME);

ALTER TABLE ADMINISTRATION


ADD CONSTRAINT FOR_KEY_ADMIN
FOREIGN KEY (PRES_NAME) REFERENCES PRESIDENT(PRES_NAME);

ALTER TABLE ADMIN_PR_VP


ADD CONSTRAINT FOR_KEY_ADMVP
FOREIGN KEY (ADMIN_NR,PRES_NAME)
REFERENCES ADMINISTRATION(ADMIN_NR,PRES_NAME);

ALTER TABLE PRES_HOBBY


ADD CONSTRAINT FOR_KEY_HOBBY
FOREIGN KEY (PRES_NAME) REFERENCES PRESIDENT(PRES_NAME);

END of EXAM
Page 6 of 6
EDB3 Exam, May 13th 2019