Você está na página 1de 10

Q1.

Find all information about courses XQuery:


<result> { doc("university.xml")/university/courses } </result>

Q2. Find titles of all courses XQuery:


<result> { doc("university.xml")//course/title } </result>

Q3. Find all areas in which courses are offered XQuery: ( version a ) q3a.xq
<result> { doc("university.xml")//course/area } </result>

XQuery: ( version b ) q3b.xq


<result> { for $c in distinct-values(doc("university.xml")//course/area) return <area>{$c}</area> } </result>

Q4. Find all CS students XQuery: ( version a ) q4a.xq


<result> { for $s in doc("university.xml")//student where $s/major = "CS" return $s/sid } </result>

XQuery: ( version b ) q4b.xq


<result> { doc("university.xml")//student[major = "CS"]/sid } </result>

Q5. Find all CS or CE students XQuery: ( version a ) q5a.xq


<result> { for $s in doc("university.xml")//student where $s/major = "CS" or $s/major = "CE" return $s/sid } </result>

XQuery: ( version b ) q5b.xq


<result> { doc("university.xml")//student[major = "CS" or major = "CE"]/sid } </result>

Q6. Find all CS students who live in Edinburg XQuery: ( version a ) q6a.xq
<result> { for $s in doc("university.xml")//student where $s/major = "CS" and $s/address = "Edinburg" return $s/sid } </result>

XQuery: ( version b ) q6b.xq


<result> { doc("university.xml")//student[major = "CS"]/sid[../address = "Edinburg"] } </result>

Q7. Find all students whose majors are undeclared XQuery: ( version a ) q7a.xq
<result> { for $s in doc("university.xml")//student where not($s/major) return $s/sid } </result>

XQuery: ( version b ) q7b.xq


<result> { doc("university.xml")//student[not(major)]/sid } </result>

Q8. Find titles of all graduate courses (cid is 6000 or higher) in the DB area XQuery: ( version a ) q8a.xq
<result> { for $c in doc("university.xml")//courses/course where $c/area = "DB" and $c/cid >= 6000 return $c/title } </result>

XQuery: ( version b ) q8b.xq


<result> { doc("university.xml")//courses/course[area = "DB" and cid >= 6000]/title } </result>

Q9. Find all course identifiers and show (rename) them as course numbers XQuery:
<result> { for $c in doc("university.xml")//courses/course return <CourseNumber>{data($c/cid)}</CourseNumber> } </result>

Q10. Find all course identifiers with their respective course areas and show (rename) them as course numbers and groups XQuery:
<result> { for $c in doc("university.xml")//courses/course let $cn := <CourseNumber>{data($c/cid)}</CourseNumber> let $cg := <CourseGroup>{data($c/area)}</CourseGroup> let $out := ($cn,$cg) return <row>{$out}</row> } </result>

Q11. Find all possible pairs (combinations) of students and professors XQuery:
<result> { for $s in doc("university.xml")//students/student, $p in doc("university.xml")//professors/professor return <row>{($s/sid,$p/pid)}</row> } </result>

Q12. Find all possible pairs (combinations) of CS and CE students XQuery:


<result> { for $sCS in doc("university.xml")//students/student[major = "CS"], $sCE in doc("university.xml")//students/student[major = "CE"] let $cssid := <cssid>{data($sCS/sid)}</cssid> let $cesid := <cesid>{data($sCE/sid)}</cesid> return <row>{($cssid,$cesid)}</row> } </result>

Q13. Find identifiers of all courses that the student with name Nathan took XQuery:
<result> { for $s in doc("university.xml")//students/student[name = "Nathan"], $t in doc("university.xml")//transcripts/record where $s/sid = $t/sid return $t/cid } </result>

Q14. Find titles of all courses that the student with name Nathan took XQuery:
<result> { for $s in doc("university.xml")//students/student[name = "Nathan"], $t in doc("university.xml")//transcripts/record, $c in doc("university.xml")//courses/course where $s/sid = $t/sid and $t/cid = $c/cid return $c/title } </result>

Q15. Find course titles and professor names for courses that the student with name Nathan took XQuery:
<result> { for $s in doc("university.xml")//students/student[name = "Nathan"], $t in doc("university.xml")//transcripts/record, $c in doc("university.xml")//courses/course, $p in doc("university.xml")//professors/professor where $s/sid = $t/sid and $t/cid = $c/cid and $t/pid = $p/pid return <row>{($c/title, $p/name)}</row> } </result>

Q16. Find names of students who took a course with professor Artem XQuery: ( version a ) q16a.xq
<result> { for $s in doc("university.xml")//students/student, $t in doc("university.xml")//transcripts/record, $p in doc("university.xml")//professors/professor where $s/sid = $t/sid and $t/pid = $p/pid and $p/name = "Artem" return $s/name } </result>

XQuery: ( version b ) q16b.xq


<result> { for $x in distinct-values( for $s in doc("university.xml")//students/student, $t in doc("university.xml")//transcripts/record, $p in doc("university.xml")//professors/professor where $s/sid = $t/sid and $t/pid = $p/pid and $p/name = "Artem" return $s/name ) return <name>{$x}</name> } </result>

Q20. Find all CS and all IT students XQuery: ( version a ) q20a.xq


<result> { doc("university.xml")//student[major = "CS"]/sid | doc("university.xml")//student[major = "IT"]/sid } </result>

Q21. Find names of professors who taught INTRO or DB courses XQuery: ( version a ) q21a.xq
<result> { (for $p in doc("university.xml")//professors/professor, $t in doc("university.xml")//transcripts/record, $c in doc("university.xml")//courses/course where $p/pid = $t/pid and $t/cid = $c/cid and $c/area = "INTRO" return $p/name) union (for $p in doc("university.xml")//professors/professor, $t in doc("university.xml")//transcripts/record, $c in doc("university.xml")//courses/course where $p/pid = $t/pid and $t/cid = $c/cid and $c/area = "DB" return $p/name) } </result>

Result:
<result> <name>Artem</name> <name>John</name> <name>Christine</name> </result>

XQuery: ( version b ) q21b.xq


<result> { for $p in doc("university.xml")//professors/professor, $t in doc("university.xml")//transcripts/record, $c in doc("university.xml")//courses/course where $p/pid = $t/pid and $t/cid = $c/cid and ($c/area = "INTRO" or $c/area = "DB") return $p/name } </result>

Q22. Find names of professors who taught in 2009 or 2010 XQuery:


<result> { (for $p in doc("university.xml")//professors/professor, $t in doc("university.xml")//transcripts/record where $p/pid = $t/pid and $t/year = "2009" return $p/name) union (for $p in doc("university.xml")//professors/professor, $t in doc("university.xml")//transcripts/record where $p/pid = $t/pid and $t/year = "2010" return $p/name) } </result>

Q23. Find names of professors who taught both INTRO and DB courses XQuery:
<result> { (for $p in doc("university.xml")//professors/professor, $t in doc("university.xml")//transcripts/record, $c in doc("university.xml")//courses/course where $p/pid = $t/pid and $t/cid = $c/cid and $c/area = "INTRO" return $p/name) intersect (for $p in doc("university.xml")//professors/professor, $t in doc("university.xml")//transcripts/record, $c in doc("university.xml")//courses/course where $p/pid = $t/pid and $t/cid = $c/cid and $c/area = "DB" return $p/name) } </result>

Q24. Find names of professors who taught in both 2009 and 2010 XQuery:
<result> { (for $p in doc("university.xml")//professors/professor, $t in doc("university.xml")//transcripts/record where $p/pid = $t/pid and $t/year = "2009" return $p/name) intersect (for $p in doc("university.xml")//professors/professor, $t in doc("university.xml")//transcripts/record where $p/pid = $t/pid and $t/year = "2010" return $p/name) } </result>

Q28. Find courses that were not offered in Fall 2009 XQuery:
<result> { let $c := (doc("university.xml")//courses/course) except (for $t in doc("university.xml")//transcripts/record, $c in doc("university.xml")//courses/course where $t/cid = $c/cid and $t/year = "2009" return $c) return $c/cid } </result>

Q29. Find professors who taught all DB courses XQuery:


<result> { for $p in doc("university.xml")//professors/professor let $courses := doc("university.xml")//courses/course[area = "DB"], (: all DB courses :) $transcripts := doc("university.xml")//transcripts/record[pid = $p/pid] (: all record for a particular professor :) where every $c in $courses satisfies some $t in $transcripts satisfies $t/cid = $c/cid return $p/pid } </result>

Q30. Find names of students who took every course in the INTRO area XQuery:
<result> { for $s in doc("university.xml")//students/student let $courses := doc("university.xml")//courses/course[area = "INTRO"], $transcripts := doc("university.xml")//transcripts/record[sid = $s/sid] where every $c in $courses satisfies some $t in $transcripts satisfies $t/cid = $c/cid return $s/name } </result>

Q32. Find professors who taught at least two (>=2) different courses XQuery: ( version a ) q32a.xq
<result> { for $t1 in doc("university.xml")//transcripts/record, $t2 in doc("university.xml")//transcripts/record where $t1/pid = $t2/pid and $t1/cid != $t2/cid return $t1/pid } </result>

Q35. Find the total number of courses offered in Fall 2009 XQuery: ( version a ) q35a.xq
<result> { let $crs := doc("university.xml")//transcripts/record[semester = "Fall" and year = "2009"] return count($crs) } </result>

XQuery: ( version b ) q35b.xq


<result> { count(doc("university.xml")//transcripts/record[semester = "Fall" and year = "2009"]) } </result>

Q37. Find the total number of credits completed by student with id 101 XQuery:
<result> { sum( for $r in doc("university.xml")//transcripts/record[sid = "101"], $c in doc("university.xml")//courses/course where $r/cid = $c/cid return $c/credits ) } </result>

Você também pode gostar