You are on page 1of 14

Database Development (BAIT4/INF4/IXD6/iDA8)

Exam Assignments
Hua Lu
2nd of June 2017

Full Name:

Study number:

E-mail
at student.aau.dk:

This exam consists of a number of exercises and there are three (3) hours to solve
them. When answering the questions write directly in the provided fields on this paper if
possible. Remember also to put your name and your study number on any additional
sheets of paper you will use.

 Read carefully the text of each exercise before solving it!


 Please make an effort to use a readable handwriting and to present your
solutions neatly. This will make it easier to understand your answers.
During the exam you are allowed to consult books and notes. The use of computer is
also permitted only for the electronic text book, slides, and notes! You are not allowed to
use any communication, i.e., WiFi MUST be switched off! You are not allowed to use
MySQL or any other software on your computer for helping solve the exercises.
Null Values [6 points in total]
1. Fill in the empty fields with the values TRUE, FALSE, or NULL. [0.5 point per cell]
Boolean expression Value of Boolean expression
FALSE
NULL AND FALSE
NULL
NULL AND TRUE
NULL
NULL OR NULL
NULL
NOT NULL
TRUE
(NULL OR TRUE) OR NULL
NULL
(FALSE OR NULL) OR NULL

2. Assume that table kids currently contains the following rows.


name gender age
John Male 18
Kate Female NULL
Sebastian Male 16
Mads Male NULL
Emil Female 18
Kelly Female 20

Write down the result, in a table format, of the following SQL statement. [3 points]
SELECT gender, COUNT(age) AS count, AVG(age) AS average
FROM kids GROUP BY gender;

gender count average

Male 2 17

Female 2 19

1
Inner Join [7 points in total]
Refer to the following two tables, r and s respectively, to answer the subsequent questions.

r F G s G H
a b a b
a c b f
c d c d

1. What is the result of the SQL statement? (Clearly circle your choice.) [3 points]
SELECT * FROM r INNER JOIN s ON r.G = s.G;

F G H F G G H
a b f a b a b
a c d c d c d

A B
F G G H
F G G H a b a b
a b b f a c a b
a c c d c d c d

C D
2. Write down the result of the SQL statement in table format. [4 points]
SELECT F, G FROM r NATURAL JOIN s;

F G

a b

a c

2
Outer Join [8 points in total]
Refer to the following two tables, r and s respectively, to answer the subsequent questions.

r rid s sid value


1 1 5
2 3 15
3 5 25

1. What is the result of the SQL statement? (Clearly circle your choice.) [4 points]
SELECT * FROM r LEFT OUTER JOIN s ON rid = sid;

A B C D
rid sid value rid sid value rid sid value rid sid value
1 1 5 1 1 5 1 1 5 1 1 5
3 3 15 2 NULL NULL 3 3 15 2 NULL NULL
3 3 15 NULL 5 25 3 3 15
NULL 5 25

2. What is the result of the SQL statement? (Clearly circle your choice.) [4 points]
SELECT * FROM r RIGHT OUTER JOIN s ON rid = sid;

A B C D
rid sid value rid sid value rid sid value rid sid value
1 1 5 1 1 5 1 1 5 1 1 5
3 3 15 2 NULL NULL 3 3 15 2 NULL NULL
3 3 15 NULL 5 25 3 3 15
NULL 5 25

3
Set Operations [8 points in total]
Refer to the following three tables, r, s, and t respectively.

r s t
id value id value id value
1 100 1 200 1 100
2 200 2 400 2 400
3 600

1. What is the result of the SQL statement? (Clearly circle your choice.) [2 points]
SELECT * FROM r UNION SELECT * FROM s UNION SELECT * FROM t;
A B C D
id value id value id value id value
1 100 1 100 1 100 1 100
2 200 2 200 2 200 2 200
1 200 1 200 1 200 1 200
2 400 2 400 2 400 2 400
3 600 1 100 3 600 3 600
2 400 1 100 1 100
2 400

2. Write down the result of the SQL statement in the table format. [3 points]
SELECT * FROM r INTERSECT SELECT * FROM t;

id value
1 100

3. Write down the result of the SQL statement in the table format. [3 points]
SELECT * FROM s EXCEPT SELECT * FROM t;
id value
1 200
3 600

4
View and Security [9 points in total]
Consider table students that is created as follows for the relevant questions on this page.
CREATE TABLE students (
id INT PRIMARY KEY,
age INT NOT NULL DEFAULT 22,
height INT NOT NULL,
grade INT);
1. View vw is created as follows.
CREATE VIEW vw AS SELECT id, age, height FROM students;
Which of the following insert statements will insert data into the table students? (Clearly
circle the numbers of your choices.) [0.5 point per each]
1. INSERT INTO vw VALUES (10, 10, 10);
2. INSERT INTO vw VALUES (10, NULL, 10);
3. INSERT INTO vw VALUES (10, 10, NULL);
4. INSERT INTO vw (id, age, height) VALUES (20, 20, 20);
5. INSERT INTO vw (id, age) VALUES (10, 10);
6. INSERT INTO vw (id, height) VALUES (10, 10);
2. Suppose Tom and John are two database users and the following SQL statement is run
successfully:
GRANT SELECT(id, age), DELETE ON students TO Tom WITH GRANT OPTION;
Which of the following SQL statements can Tom run successfully? [1 point per each]
1. SELECT * FROM students WHERE age > 20;
2. DELETE FROM students WHERE age=20;
3. GRANT DELETE ON students TO John;
4. GRANT SELECT ON students TO John;

3. Suppose the following SQL statement has been run successfully:


GRANT ALL ON students TO Tom WITH GRANT OPTION;
Write an SQL statement such that Tom can no longer update the table students. [2 points]
REVOKE UPDATE ON students FROM Tom;

5
SQL Queries [7 points in total]
Consider the following two tables about employees and departments in a company. In the employees table,
emp_id is the primary key. In the department table, dpt_id is the primary key.
employees departments
emp_id name gender dpt_id dpt_id dept_name location
001 Kris Jensen F 01 01 Sales SLV300
002 Morten Nielsen M 02 02 Logistics FVJ7A
003 Kristian Jul M 01 03 Finance FVJ7B
004 Morten Nielsen M 03 04 Planning FVJ6A
005 Sarah Jul F 04 05 Hiring FVJ7C

1. How many records are there in the result of the following SQL statement? [2 points]
SELECT * FROM departments NATURAL JOIN employees;
5

2. What is the result of the following SQL statement? [2 points]


SELECT emp_id FROM employees
WHERE name = (SELECT name FROM employees WHERE emp_id= ‘004’);
emp_id
002
004

3. What is the result of the following SQL statement? [3 points]


SELECT d.dept_name FROM departments d
WHERE d.location LIKE ‘FVJ7%’
AND NOT EXISTS (SELECT * FROM employees e WHERE d.dpt_id = e.dpt_id);

dept_name
Hiring

6
Create an ER Diagram [15 points in total]
NordFilm is a company that manages a large number of cinemas in Nordic countries. Each
particular cinema is located at a specific address in a city. The company needs to know the start
year of each cinema. Each cinema consists of a number halls. A hall is a show room where there
are a number of seats for audience. For a hall, the company needs to know how many seats there
are, if it is air conditioned, if the screen is 2D or 3D, and when the hall was recently renovated. A
hall is managed by one manager who is employed by NordFilm. The manager’s name, gender,
age, and mobile phone number must be in the company’s database for management purpose. A
manager is responsible for only one hall and she or he does not work for more than one cinema.

Create an ER diagram for NordFilm. Clearly indicate the participation (total or partial) and
cardinality (1-to-1, 1-to-many, and many-to-many) constraints if any. Make it explicit which
attributes form a primary key. List any other assumptions you make.

7
Map an ER Diagram to Tables [15 points in total]
There are many student clubs at Aalborg University. Each club focuses on a particular theme
such as drama, dance, film, and photography. Each clubs has a number of members who must be
students registered at Aalborg University, and a student at the university can participate in
different clubs. Each member is from a particular department and identified by her or his CPR
number. Each club has an ID that serves as the primary key, a name that is easy for people to
remember, and a theme of its activities. Regarding the membership of a student in a club, there is
a start date for the membership, and the student must have a role (namely chair, vice chair, or
ordinary member) in each club she/he participates in.

Map the ER-diagram to tables using SQL CREATE TABLE statements. Remember to include
all relevant constraints such as primary and foreign keys whenever they are needed. Make sure
you associate tables through foreign keys correctly.

CREATE TABLE member (


cpr CHAR(10) PRIMARY KEY,
name VARCHAR(30),
gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)),
department VARCHAR(30));

CREATE TABLE club (


id INT PRIMARY KEY,
name VARCHAR(30),
theme VARCHAR(10));

CREATE TABLE participates (


cpr CHAR(10) NOT NULL, cid INT NOT NULL,
start_date DATE,
role VARCHAR(10) CHECK (role IN (‘chair’, ‘vice chair’, ‘ordinary member’)),
FOREIGN KEY (cpr) REFERENCES member(cpr),
FOREIGN KEY (cid) REFERENCES club(id));

8
[Use this page if you need more space for writing the SQL statements.]

9
SQL Statements Part 1 [20 points in total]
The publishing house SLV300 maintains information on its authors, editors, and the books that it
publishes. The book table stores a unique ID, book title, and the publication date for each
published book. The author table stores a unique ID, the first name, middle name, last name,
birthday, and gender of each author. The writes table stores the author ID and the book ID. An
author may write many books, and a book may have multiple authors. The editor table stores the
same set of columns for each editor. The edits table stores the editor ID and the book ID, where
the former edits the later. A book may have multiple editors and only one of them is the
responsible and others are assistants.

CREATE TABLE book (


bid INT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
publication_date DATE
);
CREATE TABLE author (
aid INT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
middle_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
birthday DATE,
gender CHAR(1) CHECK (gender IN (‘F’, ‘M’))
);
CREATE TABLE writes (
aid INT REFERENCES author(aid),
bid INT REFERENCES book(bid)
);

CREATE TABLE editor (


eid INT PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
middle_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
birthday DATE,
gender CHAR(1) CHECK (gender IN (‘F’, ‘M’)),
);

CREATE TABLE edits (


eid INT REFERENCES editor(eid),
bid INT REFERENCES book(bid),
role CHAR(11) CHECK (role IN (‘responsible’, ‘assistant’))
);

Write SQL queries that answers the following questions.

1. List the complete information of all authors who are younger than 25 years old. [2 points]

10
2. List the full names of all the female authors that have a last name of Rowling. [2 points]
3. List the titles of all books from 2017 whose titles contain “Big Data”. [3 points]
4. List the titles of all books written by Stephen Hawking. [3 points]
5. List the titles of all books for which Kim Jensen worked as the responsible editor. [3 points]
6. Get the number of books published between 2001 and 2017 inclusively. [3 points]
7. List the number of books edited by female and male responsible editors, respectively. [4
points]

1. SELECT * FROM author WHERE birthday > ‘19920602’;

2. SELECT first_name, middle_name, last_name FROM author WHERE gender=’F’ AND


last_name = ‘Rowling’;

3. SELECT title FROM book WHERE publication_date >= ‘20170101’ AND title LIKE ‘%Big
Data%’;

4. SELECT title FROM book NATRUAL JOIN writes NATURAL JOIN author
WHERE first_name = ‘Stephen’ AND last_name=’Hawking’;

5. SELECT title FROM book NATRUAL JOIN edits NATURAL JOIN editor
WHERE first_name = ‘Kim’ AND last_name=’Jensen’ AND role=’responsible’;

6. SELECT COUNT(title) FROM book WHERE publication_date BETWEEN ‘20010101’ AND


‘20171231’;

or

SELECT COUNT(bid) FROM book WHERE publication_date BETWEEN ‘20010101’ AND


‘20171231’;

7. SELECT gender, COUNT(title) FROM book NATRUAL JOIN edits NATURAL JOIN editor
WHERE role=’responsible’
GROUP BY gender;

or

SELECT gender, COUNT(*) FROM edits NATURAL JOIN editor


WHERE role=’responsible’
GROUP BY gender;

11
[Use this page if you need more space for writing the SQL statements.]

12
SQL Statements Part 2 [5 points in total]
Kindergarten Sunshine surveyed the heights of their children. Part of the results are listed in the following
table named survey. Write an SQL statement to return the complete information of all children whose
height is below the average in their gender group. Make sure the records in the result are sorted
ascendingly on the height column.

name age gender height


Emma Jensen 5 Female 85

Mads Olsen 5 Male 90

Ole Thomsen 6 Male 100

Hans Poulsen 5 Male 98

Anne Nielsen 5 Female 89

Tim Larsen 4 Male 80

Pia Olsen 6 Female 91

SELECT *
FROM survey NATURAL JOIN (SELECT gender, AVG(height) AS avg_height FROM survey GROUP
BY gender ) AS x
WHERE survey.height < x.avg_height
ORDER BY survey.height;

13

You might also like