0% found this document useful (0 votes)
3 views25 pages

SQL Exam Answer

The document outlines the take-home exam for DM576 - Database Systems, focusing on tasks related to designing a music database, executing SQL queries, and relational algebra. It includes detailed sections on database design, SQL code for various queries, and relational algebra expressions. The exam is supervised by Panagiotis Tampakis and is to be submitted by Benjamin Benedict Ahlefeldt-Laurvig by June 8th, 2024.

Uploaded by

benjaminlaurvig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views25 pages

SQL Exam Answer

The document outlines the take-home exam for DM576 - Database Systems, focusing on tasks related to designing a music database, executing SQL queries, and relational algebra. It includes detailed sections on database design, SQL code for various queries, and relational algebra expressions. The exam is supervised by Panagiotis Tampakis and is to be submitted by Benjamin Benedict Ahlefeldt-Laurvig by June 8th, 2024.

Uploaded by

benjaminlaurvig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

DM576 - Database Systems

Artificial Intelligence
Take-home Exam

Submission Date: June 8th, 2024

Supervisor:
Panagiotis Tampakis

Submitted By:
Benjamin Benedict Ahlefeldt-Laurvig
[email protected]
beahl20 30/03-1999
497323
2nd Semester of AI 2024 DM576 Exam June 2024

Abstract

The report contains the tasks given in the exam of DM576 - Database Systems of June 2024.
Each section is designed to envelop the tasks given and their completion with the relevant SQL
code and figures.

Page 1
2nd Semester of AI 2024 DM576 Exam June 2024

Contents

1 Design, Models, and Diagrams 3

2 SQL Queries 7

3 Relational Algebra 15

4 Normalization 18

5 Indexing 20

6 Appendix 21

Page 2
2nd Semester of AI 2024 DM576 Exam June 2024

1 Design, Models, and Diagrams

The subsequent section will describe the decisions made for the first segment of the exam. A
company wants to make a database of music using the following objects in the system:

• Track: Possesses a length and a title

• Album: Has a title, year, a list of tracks, type of album like CD or vinyl, a bit-rate, and
the file format of the album

• Artist: Has a name, telephone number, email address, and a list of albums

• Concert: Has a location, a date, an artist, and a list of tracks

Additionally, an entity ’Concert Track’ was implemented to allow multiple tracks to be associ-
ated with a concert and vice versa.

Figure 1: Entity Relationship Diagram

Page 3
2nd Semester of AI 2024 DM576 Exam June 2024

The design in Figure 1 showcases the Foreign- and Primary Keys. An artist connects the
bridge between albums and tracks to the concert entity, meaning that the artist creates tracks
put in albums, followed by the artist holding concerts using the tracks created. By then, the
concert can add tracks to its venue and advertise an artist is playing these specific tracks at this
concert.

The following schemas were made:

Listing 1: Music Database Tables

CREATE TABLE Artist (


ArtistID SERIAL PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
TelephoneNumber VARCHAR(50),
EmailAddress VARCHAR(255),
AlbumList TEXT[] -- Assuming an array of album titles
);

CREATE TABLE Album (


AlbumID SERIAL PRIMARY KEY,
Title VARCHAR(255) NOT NULL,
Year INT,
Type VARCHAR(50),
Format VARCHAR(50),
Bitrate INT,
ArtistID INT REFERENCES Artist(ArtistID)
);

CREATE TABLE Track (


TrackID SERIAL PRIMARY KEY,
Title VARCHAR(255) NOT NULL,
Length INT,
AlbumID INT REFERENCES Album(AlbumID)
);

CREATE TABLE Concert (


ConcertID SERIAL PRIMARY KEY,

Page 4
2nd Semester of AI 2024 DM576 Exam June 2024

Location VARCHAR(255) NOT NULL,


Date DATE,
ArtistID INT REFERENCES Artist(ArtistID)
);

CREATE TABLE Concert_Track (


ConcertID INT,
TrackID INT,
PRIMARY KEY (ConcertID, TrackID),
FOREIGN KEY (ConcertID) REFERENCES Concert(ConcertID),
FOREIGN KEY (TrackID) REFERENCES Track(TrackID)
);

The artist table defines its attributes, the album table stores details of each album and also
referencing the artist who produced it, the track table stores details of each track and referencing
the album it belongs to, the concert table represents details of each concert and referencing which
artist held it, and the concert track table acts as a bridge to manage the relationship between
concerts and tracks.

Figure 2: Relational Model

Page 5
2nd Semester of AI 2024 DM576 Exam June 2024

To accommodate for a new decision made such that multiple artist can produce an album, the
ER Diagram in Figure 1 must be updated. The ’Album Artist’ entity represents the association
table to handle the relationship between albums and artists, so that each record in the table is
an established connection. If an album is produced by multiple artists, there will be multiple
records in this table linking the album to each respective artist. Likewise, if an artist has
contributed to multiple albums, there will be multiple records associating the artist with each
album in Figure 3.

Figure 3: Relational Model

Page 6
2nd Semester of AI 2024 DM576 Exam June 2024

2 SQL Queries

For the second part of the exam, specific SQL queries must be successfully run in order to extract
specific information in three tables containing data for courses and students. The tables with
example data used for the SQL queries can be seen in Figure 4.

Figure 4: Figure 1 taken from the exam document provided on digitalEksamen.sdu.dk

Page 7
2nd Semester of AI 2024 DM576 Exam June 2024

To implement these tables into the database, the following SQL query was run:

Listing 2: SDU Course and Student Tables

CREATE TABLE Courses (


CourseID SERIAL PRIMARY KEY,
CourseName VARCHAR(255) NOT NULL,
Semester VARCHAR(50) NOT NULL
);

CREATE TABLE Students (


StudentID SERIAL PRIMARY KEY,
StudentName VARCHAR(255) NOT NULL,
EnrollYear INT NOT NULL
);

CREATE TABLE StudentCourses (


StudentID INT,
CourseID INT,
Semester VARCHAR(50),
Grade INT,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID),
PRIMARY KEY (StudentID, CourseID, Semester) -- Assuming a student can take a
course only once in a semester
);

INSERT INTO Courses (CourseID, CourseName, Semester)


VALUES
(1, ’Intro to CS’, ’Spring’),
(2, ’Data Structures’, ’Fall’),
(3, ’Algorithms’, ’Spring’),
(4, ’Databases’, ’Fall’),
(5, ’Programming’, ’Fall’),
(6, ’Computer Architecture’, ’Spring’),
(7, ’Networks’, ’Spring’),
(8, ’Discrete Math’, ’Fall’);

Page 8
2nd Semester of AI 2024 DM576 Exam June 2024

Listing 3: SDU Course and Student Tables

INSERT INTO Students (StudentID, StudentName, EnrollYear)


VALUES
(1, ’Maxim Jacobson’, 2022),
(2, ’Anabella Spencer’, 2021),
(3, ’Avery Carr’, 2022),
(4, ’Nikolai Osborne’, 2022),
(5, ’Mike Russo’, 2021),
(6, ’Deanna Pitts’, 2020);

INSERT INTO StudentCourses (StudentID, CourseID, Semester, Grade)


VALUES
(1, 6, ’Spring23’, 10),
(1, 4, ’Fall23’, 7),
(2, 3, ’Spring23’, 4),
(2, 4, ’Fall23’, 12),
(3, 6, ’Spring23’, 7),
(3, 1, ’Spring23’, 4),
(4, 2, ’Fall23’, 7),
(5, 8, ’Fall23’, 10);

Page 9
2nd Semester of AI 2024 DM576 Exam June 2024

Following these listings, we have the requests for SQL queries in order and the query written to
fulfill the criteria of the tasks given.

Listing 4: a) Write an SQL query (without subqueries), which returns the minimum EnrollYear
of students that got the Discrete Math course in the Spring23 semester

SELECT MIN(EnrollYear) AS Min_EnrollYear


FROM Students
JOIN StudentCourses ON Students.StudentID = StudentCourses.StudentID
JOIN Courses ON StudentCourses.CourseID = Courses.CourseID
WHERE Courses.CourseName = ’Discrete Math’ AND StudentCourses.Semester = ’Spring23’;

The query for task a) selects the smallest EnrollYear integer of the students assigned to
Discrete Math for the semester of Spring23 but since no students are assigned, according
to the data from the tables, to Discrete Math in Spring2023, the query will return NULL
because there are no records matching the criteria.

Figure 5: 2a query result

Page 10
2nd Semester of AI 2024 DM576 Exam June 2024

For b), the following query was executed:

Listing 5: b) Write an SQL SELECT query, which lists the StudentNames that have taken up
less than 3 courses

SELECT Students.StudentName
FROM Students
LEFT JOIN StudentCourses ON Students.StudentID = StudentCourses.StudentID
GROUP BY Students.StudentID, Students.StudentName
HAVING COUNT(StudentCourses.CourseID) < 3;

The query for task b) selects the student names from students who have taken up less than
3 courses. For this instance, every student in the table of students have taken up less than 3
courses as seen in Figure 5

Figure 6: 2b query result

Page 11
2nd Semester of AI 2024 DM576 Exam June 2024

Task c) asks to list all the CourseNames that are taught both in the spring and the fall semester.
Before writing the query, the table did not contain any courses that were taught in both spring
and fall, so the addition of a new insertion of Discrete Math into spring was added to give the
query something to work with. The new spring course for Discrete Math was added as a new
course, having ′ 9′ as its ID, so we can expect two instances of Discrete Math to show up after
executing the query.

Listing 6: c) Write an SQL SELECT query with subqueries, which lists all CourseNames that
are taught both in the Spring and the Fall semester

SELECT Students.StudentName
FROM Students
LEFT JOIN StudentCourses ON Students.StudentID = StudentCourses.StudentID
GROUP BY Students.StudentID, Students.StudentName
HAVING COUNT(StudentCourses.CourseID) < 3;

Figure 7: 2c query result

Without adding the new course, the query would simple return NULL because there are only 8
unique courses and no one is repeated or have two attributes containing information in regards
to them being in both fall and spring.

Page 12
2nd Semester of AI 2024 DM576 Exam June 2024

Listing 7: d) Write an SQL SELECT query, which lists for each student all the CourseNames
(in descending alphabetic order) where they scored higher than their average Grade

SELECT Students.StudentName, Courses.CourseName


FROM Students
JOIN StudentCourses ON Students.StudentID = StudentCourses.StudentID
JOIN Courses ON StudentCourses.CourseID = Courses.CourseID
WHERE StudentCourses.Grade > (
SELECT AVG(Grade)
FROM StudentCourses AS SC
WHERE SC.StudentID = Students.StudentID
)
ORDER BY Students.StudentName, Courses.CourseName DESC;

Figure 8: 2d query result

The SELECT AVG(Grade) calculates the average grade for each student and the sub-query is
correlated with the outer query. The statement WHERE StudentCourses.Grade > (subquery)
ensures that only the courses where the grade is higher than the student’s average grade is
selected.

Page 13
2nd Semester of AI 2024 DM576 Exam June 2024

Listing 8: e) Write an SQL SELECT query, which lists all the StudentNames that took courses
in the Fall23 semester and took no course in the Spring23 semester

SELECT DISTINCT Students.StudentName


FROM Students
JOIN StudentCourses AS FallCourses ON Students.StudentID = FallCourses.StudentID
WHERE FallCourses.Semester = ’Fall23’
AND NOT EXISTS (
SELECT 1
FROM StudentCourses AS SpringCourses
WHERE SpringCourses.StudentID = Students.StudentID
AND SpringCourses.Semester = ’Spring23’
);

By reviewing the data in the tables, we expect to see Mike Russo and Nikolai Osborne, as
they both took courses in fall but not in the spring. By executing the query, we get:

Figure 9: 2e query result

Page 14
2nd Semester of AI 2024 DM576 Exam June 2024

3 Relational Algebra

We have two relations S and R (Figure 10) to consider.

Figure 10: Relational Algebra Relations

The following five expressions are for task a). For the expressions, the results are given in as
similar manner to the ones provided.

Expression 1: σ(A>B) (R)

This expression selects the tuples from relation R where the value of A is greater than the value
of B. The resulting relation is:
A B C
9 3 8
T
Expression 2: Π(A,B) (R) Π(A,B) (S)

This expression selects distinct values of A and B that are common in both relations R and S.
The resulting relation is:
A B

Empty relation

Expression 3: S ▷◁ RS

This expression performs a natural join between relations S and R, keeping only the attributes
from S. The resulting relation is:
A B D
2 3 1
3 1 3
5 3 4

Page 15
2nd Semester of AI 2024 DM576 Exam June 2024

Expression 4: σ(C<D) (S × R)

This expression first performs a Cartesian product between relations S and R, then selects rows
where the value of C in S is less than the value of D in R. The resulting relation is:

A B C A1 B 1 D
1 2 3 9 3 8
2 3 1 9 3 8
3 1 3 9 3 8

Expression 5: γ(A,B,count(D)) (RS ▷◁ R)

This expression first performs a natural join between relations R and S, then groups the result
by attributes A and B, and finally counts the distinct values of D for each group. The resulting
relation:
A B count(D)

For task b), we are given SQL statements that should be rewritten as an extended relational
algebra equivalent to the SQL statement by using the actual tuples present in R and S.

Listing 9: Statement 1:

SELECT R.A, S.B, C, D FROM R LEFT OUTER JOIN S ON C = D AND R.A > S.A;

The equivalent algebra expression is:

R(σ(C=D∧R.A>S.A) (S))

In the SQL statement, we perform a left outer join between relations R and S based on a con-
dition. In the expression includes the selection operator σ to filter tuples from relation S based
on the condition given.

Page 16
2nd Semester of AI 2024 DM576 Exam June 2024

Listing 10: Statement 2:

SELECT DISTINCT A, B FROM R WHERE C = 3 OR B = 3;

The equivalent algebra expression for the second statement is:

Π(A,B) (σ(C=3∨B=3) (R))

The expression starts with the selection operator σ, which filters tuples from relation R based
on the condition set. Next, the projection operator Π selects only attributes A and B from the
filtered tuples. The DISTINCT keyword in SQL is implied by the projection operator in relational
algebra, ensuring that only distinct tuples are retained. In the SQL statement, we select distinct
values of attributes A and B from relation R with a condition. Therefore, the statement and
expression are equivalent.

Listing 11: Statement 3:

SELECT R.A, AVG(S.C) FROM R NATURAL JOIN S GROUP BY R.A;

The equivalent algebra expression for the third statement is:

γ(R.A,AVG(S.C)) (R ▷◁ S)

The third SQL statement performs a joining of relations R and S using a natural join based
on their common attributes. It groups the resulting tuples by the attribute R.A and calculates
the average value of attribute S.C for each group. The algebra expression represents the same
operation because (R ▷◁ S) represents the natural join between the relations and ensuring that
only the tuples with common values in their shared attributes are retained. The aggregation
operator groups the tuples by the attribute R.A and calculates the average value of attribute
S.C for each group.

Page 17
2nd Semester of AI 2024 DM576 Exam June 2024

4 Normalization

In collaboration with the first segment of the exam, we consider the relation:

R(AlbumN ame, AlbumY ear, AlbumN ame, ArtistAddress, T rackT itle, T rackLength)

Task a): List all of the functional dependencies based on the description of Section 1.

1. AlbumTitle, AlbumYear =⇒ AlbumFormat, AlbumType, FileFormat, Bitrate

2. TrackTitle =⇒ TrackLength

3. AlbumTitle, AlbumYear =⇒ ArtistName

4. ConcertLocation, ConcertDate =⇒ ArtistName

5. ArtistName =⇒ ArtistTelephone, ArtistEmail

Task b): List all keys of R and argue why there can be no other keys.

We need to analyze the attributes and functional dependencies to list all keys in R and argue
why there can be no other keys:

1. The key of R is determined by AlbumTitle, AlbumYear, and ArtistName.

2. This key uniquely identifies each tuple in R because each album produced by an artist in
a specific year is distinct.

Therefore, there are no other keys because any subset of attributes will not uniquely identify
the tuples in R.

Page 18
2nd Semester of AI 2024 DM576 Exam June 2024

Task c): Analyze whether R is BCNF, if it is, show that there are no BCNF violations. If it is
not, show that there is at least one BCNF violation and decompose R until it is in BCFN.

To determine whether R is in BCNF, we need to check for violations. Using the functional
dependencies from task a), we can determine that the key for R is (AlbumTitle, AlbumYear,
ArtistName), which uniquely identifies each tuple because each album produced by an artist in
a specific year is distinct.

Looking at the functional dependencies, we don’t have any non-trivial dependencies where the
left-hand side is not a super-key. Therefore, R is in BCNF.

Task d): What would change if we would consider 3NF instead of BCNF? Explain your answer.

Considering the change to 3NF instead of BCNF, we have to consider the difference between
the two. BCNF eliminates all non-trivial dependencies where the left-hand is not a super-key
and 3NF allows transitive dependencies but eliminates partial dependencies. The implication
of using 3NF means we would have to ensure that each non-key attribute is fully functionally
dependent on the primary key. This might result in additional decomposition compared to
BCNF to eliminate transitive dependencies, which could potentially lead to more relations in
the database schema.

Page 19
2nd Semester of AI 2024 DM576 Exam June 2024

5 Indexing

The last two tasks are to create indexes for schemas presented in Section 2 and to see if we can
speed up the execution of the queries. For each table in the schema, we’ll create an index on
the primary key:

Listing 12: Indexes

-- Index for Courses table


CREATE INDEX idx_courses_courseid ON Courses(CourseID);
CREATE INDEX idx_courses_studentid ON Students(StudentID);
CREATE INDEX idx_courses_studentcourses ON StudentCourses(StudentID, CourseID);

Query a): Find all students enrolled in a specific course in a specific semester We create the
index:

Listing 13: Query a)

-- Index creation for Query (a)


CREATE INDEX idx_course_semester ON StudentCourses(CourseID, Semester);

This index includes the columns CourseID and Semester, which are used in the WHERE clause
of the query. By the creation of this index, the database can efficiently retrieve the rows corre-
sponding to the specified course and semester combination.

Query b): Find all courses taken by a specific student We create the index:

Listing 14: Query b)

-- Index creation for Query (b)


CREATE INDEX idx_student_courses ON StudentCourses(StudentID);

This index includes the StudentID column, which is used in the WHERE clause of the query.
The database can now efficiently retrieve the rows corresponding to the specified student.

Query c): Find all students enrolled in a specific course in a specific semester The query involves
aggregating data and does not involve filtering based on specific columns, so creating an index
would not improve the execution time of the query.

Page 20
2nd Semester of AI 2024 DM576 Exam June 2024

6 Appendix

In the subsequent section, all the SQL code written to run queries can be found in the listing
below.

Listing 15: Everything written in the SQL query

CREATE TABLE Courses (


CourseID SERIAL PRIMARY KEY,
CourseName VARCHAR(255) NOT NULL,
Semester VARCHAR(50) NOT NULL
);

CREATE TABLE Students (


StudentID SERIAL PRIMARY KEY,
StudentName VARCHAR(255) NOT NULL,
EnrollYear INT NOT NULL
);

CREATE TABLE StudentCourses (


StudentID INT,
CourseID INT,
Semester VARCHAR(50),
Grade INT,
FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
FOREIGN KEY (CourseID) REFERENCES Courses(CourseID),
PRIMARY KEY (StudentID, CourseID, Semester) -- Assuming a student can take a
course only once in a semester
);

INSERT INTO Courses (CourseID, CourseName, Semester)


VALUES
(1, ’Intro to CS’, ’Spring’),
(2, ’Data Structures’, ’Fall’),
(3, ’Algorithms’, ’Spring’),
(4, ’Databases’, ’Fall’),
(5, ’Programming’, ’Fall’),
(6, ’Computer Architecture’, ’Spring’),
(7, ’Networks’, ’Spring’),

Page 21
2nd Semester of AI 2024 DM576 Exam June 2024

(8, ’Discrete Math’, ’Fall’),


(9, ’Discrete Math’, ’Spring’);

INSERT INTO Students (StudentID, StudentName, EnrollYear)


VALUES
(1, ’Maxim Jacobson’, 2022),
(2, ’Anabella Spencer’, 2021),
(3, ’Avery Carr’, 2022),
(4, ’Nikolai Osborne’, 2022),
(5, ’Mike Russo’, 2021),
(6, ’Deanna Pitts’, 2020);

INSERT INTO StudentCourses (StudentID, CourseID, Semester, Grade)


VALUES
(1, 6, ’Spring23’, 10),
(1, 4, ’Fall23’, 7),
(2, 3, ’Spring23’, 4),
(2, 4, ’Fall23’, 12),
(3, 6, ’Spring23’, 7),
(3, 1, ’Spring23’, 4),
(4, 2, ’Fall23’, 7),
(5, 8, ’Fall23’, 10);

SELECT MIN(EnrollYear) AS Min_EnrollYear


FROM Students
JOIN StudentCourses ON Students.StudentID = StudentCourses.StudentID
JOIN Courses ON StudentCourses.CourseID = Courses.CourseID
WHERE Courses.CourseName = ’Discrete Math’ AND StudentCourses.Semester = ’Spring23’;

SELECT Students.StudentName
FROM Students
LEFT JOIN StudentCourses ON Students.StudentID = StudentCourses.StudentID
GROUP BY Students.StudentID, Students.StudentName
HAVING COUNT(StudentCourses.CourseID) < 3;

SELECT CourseName
FROM Courses

Page 22
2nd Semester of AI 2024 DM576 Exam June 2024

WHERE CourseName IN (
SELECT CourseName
FROM Courses
WHERE Semester = ’Spring’
)
AND CourseName IN (
SELECT CourseName
FROM Courses
WHERE Semester = ’Fall’
);

SELECT Students.StudentName, Courses.CourseName


FROM Students
JOIN StudentCourses ON Students.StudentID = StudentCourses.StudentID
JOIN Courses ON StudentCourses.CourseID = Courses.CourseID
WHERE StudentCourses.Grade > (
SELECT AVG(Grade)
FROM StudentCourses AS SC
WHERE SC.StudentID = Students.StudentID
)
ORDER BY Students.StudentName, Courses.CourseName DESC;

SELECT Students.StudentName
FROM Students
JOIN StudentCourses AS FallCourses
ON Students.StudentID = FallCourses.StudentID
WHERE FallCourses.Semester = ’Fall23’
AND Students.StudentID NOT IN (
SELECT StudentID
FROM StudentCourses
WHERE Semester = ’Spring23’
);

-- Index for Courses table


CREATE INDEX idx_courses_courseid ON Courses(CourseID);
CREATE INDEX idx_courses_studentid ON Students(StudentID);
CREATE INDEX idx_courses_studentcourses ON StudentCourses(StudentID, CourseID);

Page 23
2nd Semester of AI 2024 DM576 Exam June 2024

-- Index creation for Query (a)


CREATE INDEX idx_course_semester ON StudentCourses(CourseID, Semester);

Page 24

You might also like