You are on page 1of 7

/*s214861TANCAUPETREA (Databases6CFU)

HOMEWORK_2
1.Thefollowingrelationsaregiven(primarykeysare
underlined):
AUTHOR(AuthorCode,Name,Surname,Department,University)
ARTICLE(ArticleCode,Title,Topic)
AUTHORSOFARTICLE(ArticleCode,AuthorCode)
EDITIONSOFCONFERENCE(Conference,Edition,EditionName,
StartDate,EndDate,Editor)
AUTHORPRESENTSARTICLE(AuthorCode,Date,StartTime,EndTime,
Room,
ArticleCode,Conference,Edition)

WritethefollowingqueryinSQL
(a)Fortheauthorswhohaveexclusivelypresentedarticleswithtopic,show
the
codeoftheauthor,thesurnameoftheauthor,her/hisuniversity,andthe
totalnumberof
articlespresentedbytheauthorineacheditionofeveryconference.*/

/*authorwhohaveexlusivelypresentedarticleswithtopic'Data
Mining'=All(Theoneswhopresentedanythingelse)*/
(*)
(
SELECTAuthorCode,Conference,Edition,COUNT(*)AS
#PresentedArticles
FROMAUTHOR_PRESENTS_ARTICLE
GROUPBYAuthorCode,Conference,Edition
)ASSTATISTICS

SELECT
AuthorCode,Surname,University,#PresentedArticles,Edition,Conferen
ce
FROMAUTHOR,AUTHOR_PRESENTS_ARTICLEASAPA,(*)
WHEREAuthorCodeNOTIN(SELECTAuthorCode
FROMARTICLE,AUTHORS_OF_ARTICLE
WHERE
ARTICLE.ArticleCode=AUTHORS_OF_ARTICLE.ArticleCode
ANDARTICLE.Topic>'DataMining')
ANDAPA.AuthorCode=AUTHOR.AuthorCode
ANDAPA.Conference=STATISTICS.ConferenceAND
APA.Edition=STATISTICS.Edition
ANDAPA.AuthorCode=APA.AuthorCodeAND
AUTHOR.AuthorCode=STATISTICS.AuthorCode
2.Thefollowingrelationsaregiven(primarykeysare
underlined):
STUDENT(StudentID,Name,Surname,DegreeProgramme)
ASSIGNMENTTOBEDELIVERED(ACode,Title,Topic,
ScheduledExpirationDate)
TEACHER(TeacherId,Name,Surname,Department)
EVALUATIONOFDELIVEREDASSIGNMENT(StudentID,ACode,TeacherID,
DeliveryDate,EvaluationDate,Score)
WritethefollowingqueryinSQL
(a)Foreachstudentwhohasdeliveredatleast3assignmentswithscore
greaterthan4,show
thesurnameofthestudent,thetotalnumberofassignmentsdeliveredbythe
student,
theaveragescoreofalldeliveredassignments,andthenumberofdifferent
teacherswho
evaluatedtheirdeliveredassignments.
*/
/*
(*)eachstudentwhohasdeliveredatleast3assignmentswith
scoregreaterthan4
*/
SELECTStudentID
FROMEVALUATION_OF_DELIVERED_ASSIGNMENT
WHEREScore>'4'
GROUPBYStudentID
HAVINGCOUNT(*)>2
/*Inordertoshowthetotalnumberofdeliveredassignmentsfor
eachstudentthatsatisfies(*)
Icanuseafunctiontabletocollectstatistics*/
(**)
(
SELECTStudentID,COUNT(*)AS#DeliveredAssignements,AVG(Score)
AS#AverageDeliveredAssignements,
COUNT(DISTINCTTeacherID)AS#EvaluetadByTeachers

FROMEVALUATION_OF_DELIVERED_ASSIGNEMENT
GROUPBYStudentID
)ASSTATISTICS

Solution:
SELECT
STUDENT.StudentID,Surname,#DeliveredAssignements,#AverageDelivere
dAssignements,#EvaluetedByTeacher
FROMSTUDENT,(**)
WHERESTUDENT.StudentID=STATISTICS.StudentID
ANDSTUDENT.StudentIDIN(*)
3.Thefollowingrelationsaregiven(primarykeysare
underlined):
AUTHOR(AuthorCode,Name,Surname,Department,University)
ARTICLE(ArticleCode,Title,Topic)
AUTHORSOFARTICLE(ArticleCode,AuthorCode)
EDITIONSOFCONFERENCE(Conference,Edition,EditionName,
StartDate,EndDate,Editor)
AUTHORPRESENTSARTICLE(AuthorCode,Date,StartTime,EndTime,
Room,
ArticleCode,Conference,Edition)
WritethefollowingqueriesinSQL
(a)Consideringtheconferenceswithatleast10editions,foreacheditionof
theconference
showthenameoftheeditionandthecodeoftheauthorwhopresentedthe
highestnumber
ofarticlesintheedition.*/

(**)
(
SELECTAuthorCode,Conference,Edition,COUNT(DISTINCTArticleCode)
AS#PresentedArticles
FROMAUTHOR_PRESENTS_ARTICLE
GROUPBYAuthorCode,Conference,Edition
)ASSTATISTICS

Solution:

SELECT
EOC.EditionName,EOC.Edition,STATISTICS.AuthorCode,#PresentedArtic
les
FROMEDITIONS_OF_CONFERENCEASEOC,AUTHOR_PRESENTS_ARTICLEAS
APA,(**)
WHEREEOC.Conference=STATISTICS.ConferenceAND
EOC.Edition=STATISTICS.Edition
ANDAPA.AuthorCode=STATISTICS.AuthorCode
ANDAPA.Conference=EOC.ConferenceANDAPA.Edition=EOC.Edition
ANDEOC.ConferenceIN(SELECTConferenceFROM
EDITION_OF_CONFERENCEGROUPBYConferenceHAVINGCOUNT(*)>9)
/*
4.Thefollowingrelationsaregiven(primarykeysare
underlined):
SEMINAR(SCode,STitle,Topic,Duration)
SPEAKER(SSSN,SName,BirthDate)
SEMINARCALENDAR(SCode,Date,StartTime,SSSN,Room)
EXPERTISE(SSSN,Topic)
WritethefollowingqueryinSQL
(a)Showthecodeoftheseminarsforwhichatleastone
scheduledpresentationwasheldby
thespeakerwiththehighestnumberoftopicsofexpertise.
/*

/*Speakerwiththehighestnumberoftopicsofexpertise*/

SELECTSSSN,COUNT(*)AS#Topics
FROMEXPERTISE
GROUPBYSSSN

/*Toextractthecodeoftheseminarsforwichatleastone
scheduledpresentationwasheldbythespeakerwiththehighest
numberoftopicsofexpertise,IdeploySEMINARCALENDARTABLE*/

Solution:

SELECTSC.SCode
FROMSEMINARCALENDARASSC,(SELECTSSSN,COUNT(*)AS#Topics
FROMEXPERTISEGROUPBYSSSN)ASSTAT
WHERESC.SSSN=(SELECTSSSNFROMSTATWHERESTAT.#Topics=
(SELECTMAX(#TOPICS)FROMSTAT))
/*
5.Thefollowingrelationsaregiven(primarykeysareunderlined):
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}
WritethefollowingqueryinSQL
(a)Foreachteacherwhohastaughtexclusivelycourseswhosetopicis
databases,selectthe
codeoftheteacherand,amonghercourses,thecodeofthecourseforwhich
theaverage
numberofstudentsattendingthecourselecturesisthehighest.
*/
(*)
SELECTTCode
FROMCOURSE
WHERETOPIC>'databases'

/*foreachcoursefindtheaveragenumberofattendingcourse*/
(**)

(SELECTTCode,CCode,AVG(AttendingStudent#)AS#AverageAttends
FROMLECTURE,COURSE
WHERECOURSE.CCode=LECTURE.CCode
GROUPBYCCode,TCode)asSTATS

Solution:

SELECTTEACHER.Tcode,COURSE.CCode
FROMTEACHER,(**),COURSE
WHERETEACHER.TCodeNOTIN (SELECTTCode
FROM COURSE
WHERETOPIC>'Databases')

ANDCOURSE.TCode=TEACHER.TCode
ANDCOURSE.CCodeIN(SELECTCCodeFROMSTATSWHERE
#AverageAttends=(SELECTMAX(#AverageAttends)
FROMSTATSASS2
WHERE
STATS.TCode=S1.TCode)
ANDSTATS.TCode=COURSE.TCode)
/*sameprofasinexternal
query*andtheninthesecondlevelofnestingIconsider
onlythetupleswiththesameTCodeasintheexternalquery*/
/*
6.Thefollowingrelationsaregiven(primarykeysare
underlined):
STUDENT(StudentID,Name,Surname,DegreeProgramme)
ASSIGNMENTTOBEDELIVERED(ACode,Title,Topic,
ScheduledExpirationDate)
TEACHER(TeacherID,Name,Surname,Department)
EVALUATIONOFDELIVEREDASSIGNMENT(StudentID,ACode,TeacherID,
DeliveryDate,EvaluationDate,Score)

WritethefollowingqueryinSQL
(a)Showtheidentifier,surnameanddegreeprogrammeofthestudentswhohave
neverde
liveredanassignmentafterthescheduledexpirationdate,andwhohave
deliveredallthe
assignmentsduealwaysgettingthehighestscore.
*/

Solution:

SELECTStudentID,Surname,DegreeProgramme
FROMSTUDENT,(SELECTStudentID,COUNT(*)AS#EvaluatedMax
FROMEVALUATION_OF_DELIVERED_ASSIGNMENT
WHEREScore=(SELECTMAX(Score)FROM
EVALUATION_OF_DELIVERED_ASSIGNMENT)
GROUPBYStudentID)ASSTAT

WHEREStudentIDNOTIN(SELECTStudentID
FROMEVALUATION_OF_DELIVERED_ASSIGNEMENTAS
EODA,ASSIGNEMENT_TO_BE_DELIVEREDASATBD
WHEREEODA.ACode=ATBD.ACodeAND
DeliveryDate>ScheduledExpirationDate)
ANDSTAT.StudentID=STUDENT.StudentIDAND
STAT.#EvaluatedMAX=(SELECTCOUNT(*)FROM
ASSIGNMENT_TO_BE_DELIVERED)

You might also like