• Non ci sono risultati.

SQL exercises Solutions Tania Cerquitelli

N/A
N/A
Protected

Academic year: 2021

Condividi "SQL exercises Solutions Tania Cerquitelli"

Copied!
14
0
0

Testo completo

(1)

DB M G

SQL exercises

Solutions

Tania Cerquitelli

(2)

DB M G

Exercise n. 1

Write the following query in SQL

A. For the authors who have exclusively presented articles with topic

’Data Mining’, show the code of the author, the surname of the author, her/his university, and the total number of articles presented by the author in each edition of every conference.

.

AUTHOR(AuthorCode, Name, Surname, Department, University) ARTICLE(ArticleCode, Title, Topic)

AUTHORS_OF_ARTICLE(ArticleCode, AuthorCode)

EDITIONS_OF_CONFERENCE(Conference, Edition, EditionName, StartDate, EndDate, Editor) AUTHOR_PRESENTS_ARTICLE(AuthorCode, Date, StartTime, EndTime, Room, ArticleCode,

Conference, Edition)

1. The following relations are given (primary keys are underlined):

(3)

DB M G

Solution Exercise n. 1 SQL

SELECT A.AuthorCode, Surname, University, COUNT(*) FROM Author A, Author_Presents_Article APA

WHERE A.AuthorCode NOT IN (SELECT AuthorCode

FROM Article AR, AUTHOR_PRESENTS_ARTICLE APA

WHERE APA.ArticleCode=AR.ArticleCode AND Topic<>‘Data Mining’ AND APA.AuthorCode=A.AuthorCode) GROUP BY A.AuthorCode, Conference, Edition,

Conference, Surname, University

A. For the authors who have exclusively presented articles with topic

’Data Mining’, show the code of the author, the surname of the author, her/his university, and the total number of articles presented by the author in each edition of every conference.

(4)

DB M G

Exercise n. 2

Write the following query in SQL

A. For each student who has delivered at least 3 assignments with score greater than 4, show the surname of the student, the total number of assignments delivered by the student, the average score of all delivered assignments, and the number of different teachers who evaluated their delivered assignments.

STUDENT (StudentID, Name, Surname, DegreeProgramme)

ASSIGNMENT_TO_BE_DELIVERED (ACode, Title, Topic, ScheduledExpirationDate) TEACHER (TeacherID, Name, Surname, Department)

EVALUATION_OF_DELIVERED_ASSIGNMENT (StudentID, ACode, TeacherID,DeliveryDate, EvaluationDate, Score)

2. The following relations are given (primary keys are underlined):

(5)

DB M G

Exercise n. 2 SQL

SELECT Surname, COUNT(*), AVG(Score), COUNT(DISTINCT TeacherId) FROM STUDENT S, EVALUATION_OF_DELIVERED_ASSIGNMENT EA1 WHERE S.StudentId IN (SELECT StudentId

FROM EVALUATION_OF_DEL_ASS EA WHERE Score > 4

GROUP BY StudentId HAVING COUNT(*)>=3) AND S.StudentId=EA1.StudentId

GROUP BY S.StudentId, Surname

For each student who has delivered at least 3 assignments with score greater than 4, show the surname of the student, the total number of assignments delivered by the student, the average score of all delivered assignments, and the number of different teachers who evaluated their delivered assignments.

(6)

DB M G

Exercise n. 3

Write the following query in SQL

A. Considering the conferences with at least 10 editions, for each edition of the conference show the name of the edition and the code of the author who presented the highest number of articles in the edition

AUTHOR(AuthorCode, Name, Surname, Department, University) ARTICLE(ArticleCode, Title, Topic)

AUTHORS_OF_ARTICLE(ArticleCode, AuthorCode)

EDITIONS_OF_CONFERENCE(Conference, Edition, EditionName, StartDate, EndDate, Editor)

AUTHOR_PRESENTS_ARTICLE(AuthorCode, Date, StartTime, EndTime, Room,ArticleCode, Conference, Edition) 3. The following relations are given (primary keys are underlined):

(7)

DB M G

Solution Exercise n. 3 SQL

SELECT EditionName, APA.AuthorCode

FROM Author_Presents_Article APA, Edition_Of_Conference EOC WHERE Conference IN (SELECT Conference

FROM Editions_Of_Conference EOC1 GROUP BY Conference

HAVING COUNT(*) >=10)

AND EOC.Edition=APA.Edition AND E.Conference=APA.Conference GROUP BY APA.AuthorCode, APA.Edition, APA.Conference, EOC.EditionName HAVING COUNT(*)=(SELECT MAX(TotPa)

FROM (SELECT AuthorCode, Edition, Conference, Count(*) AS TotPa)

FROM Author_Presents_Article AA

GROUP BY AuthorCode, Edition, Conference) AS TFA WHERE TFA.Edition=APA.Edition AND

TFA.Conference=APA.Conference

(8)

DB M G

Exercise n. 4

Write the following query in SQL

A. Show the code of the seminars for which at least one scheduled presentation was held by the speaker with the highest number of topics of expertise

SEMINAR(SCode, STitle, Topic, Duration) SPEAKER(S-SSN, SName, BirthDate)

SEMINAR-CALENDAR(SCode, Date, StartTime, S-SSN, Room) EXPERTISE(S-SSN, Topic)

4. The following relations are given (primary keys are underlined):

(9)

DB M G

Solution Exercise n. 4 SQL

SELECT DISTINCT Scode FROM Seminare_Calendar

WHERE S-SSN IN (SELECT S-SSN FROM Expertise GROUP BY S-SSN

HAVING COUNT(*)=(SELECT MAX(TotExp)

FROM (SELECT S-SSN, COUNT(?) AS TptExp FROM Expertise

GROUP BY S-SSN))) A. Show the code of the seminars for which at least one

scheduled presentation was held by the speaker with the

highest number of topics of expertise

(10)

DB M G

Exercise n. 5

Write the following query in SQL

A. For each teacher who has taught exclusively courses whose topic is databases, select the code of the teacher and, among her

courses, the code of the course for which the average number of students attending the course lectures is the highest.

TEACHER(TCode, TName, TSurname, Department, ResearchGroupName, ResearchArea)

COURSE(CCode, CName, EnrollingStudent#, TCode, Topic) CLASSROOM(RoomID, Floor#, VideoKit, Seat#)

LECTURE(RoomID, Date, StartHour, EndHour, CCode, AttendingStudent#) VideoKit ={yes, no}

5. The following relations are given (primary keys are underlined):

(11)

DB M G

Solution Exercise n. 5 SQL

SELECT C.Tcode, C.Ccode FROM Course C, Lecture L

WHERE C.Tcode NOT IN (SELECT Tcode FROM Course C2

WHERE Topic <> ‘Databases’) AND C.Ccode=L.Ccode

GROUP BY C.Tcode, C.Ccode

HAVING AVG(AttendingStudent#)=

=(SELECT MAX(AVGS)

FROM (SELECT AVG(AttendingStudent#) AS AVGS C1.Tcode

FROM Lecture L1, Course C1 WHERE L1.Ccode=C1.Ccode

GROUP BY C1.Ccode, C1.Tcode) AS TPA

WHERE TPA.Tcode=C.Tcode)

(12)

DB M G

Exercise n. 6

Write the following query in SQL

A. Show the identifier, surname and degree programme of the students who have never delivered an assignment after the scheduled expiration date, and who have delivered all the assignments due always getting the highest score.

STUDENT(StudentID, Name, Surname, DegreeProgramme)

ASSIGNMENT_TO_BE_DELIVERED(ACode, Title, Topic, ScheduledExpirationDate) TEACHER(TeacherID, Name, Surname, Department)

EVALUATION_OF_DELIVERED_ASSIGNMENT(StudentID, ACode,

TeacherID,DeliveryDate, EvaluationDate, Score) 6. The following relations are given (primary keys are underlined):

(13)

DB M G

Solution Exercise n. 6 SQL

SELECT S.StudentId, Surname, DegreeProgramme FROM EVAL_OF_DEL_ASSIGN EODA, STUDENT S WHERE EODA.StudentId=T.StudentId AND

S.StdudentId NOT IN (SELECT StudentId

FROM EODA1, ASSIGN A

WHERE EODA1.Acode=A.Acode AND

DeliveryDate>SchedeledExpDate) AND Score = (SELECT MAX (Score)

FROM EODA2

WHERE EODA2.Acode=EODA.Acode GROUP BY S.StudentId, Surname, DegreeProgramme

HAVING COUNT(*) = (SELECT (COUNT(*)

FROM ASSIGNMENT_TO_BE_DELIVERED)

(14)

DB M G

Solution Exercise n. 6 SQL V.2

SELECT S.StudentId, Surname, DegreeProgramme FROM EVAL_OF_DEL_ASSIGN EODA, STUDENT S WHERE EODA.StudentId=T.StudentId AND

S.StdudentId NOT IN (SELECT StudentId

FROM EODA1, ASSIGN A

WHERE EODA1.Acode=A.Acode AND

DeliveryDate>SchedeledExpDate) (A.Code, Score) IN (SELECT Acode, MAX(Score)

FROM EODA 2 GROUP BY Acode)

GROUP BY S.StudentId, Surname, DegreeProgramme HAVING COUNT(*) = (SELECT (COUNT(*)

FROM ASSIGNMENT_TO_BE_DELIVERED)

Riferimenti

Documenti correlati

The Reynolds number of the inlet flow rate is used to characterise the fluid-dynamic conditions of the system.. and is

The main technical arguments are based on the use of the three-spheres inequal- ity and the doubling inequality at the boundary as unique continuation tools that allow us to

10 Figure 3. A) Pore pressure distribution, flux vectors, and critical failure surface at time step 22 (16:30 on 16 November) for 15 m and 30 m-long models; B) rainfall intensity

From the point of view of the soft gluons, each collinear group is simply a bunch of particles strongly boosted in a certain direction. The boost is so strong that the soft gluons

Here, to make up for the relative sparseness of weather and hydrological data, or malfunctioning at the highest altitudes, we complemented ground data using series of remote sensing

In this context, a locally compact group is hyperbolic if it has a continuous proper cocompact isometric action on some proper geodesic hyperbolic metric space [14]; this

As an immediate corollary of Theorem 1.7 , we obtain the following sphere theorem, that should be compared with the classical Alexandrov Theorem for compact connected constant

The framework we dealt with generalizes the model considered in De Grauwe and Rovira Kaltwasser 2012 and permitted us to study the effect on the dynamics of an endogenous