8.2
Consider the RDF representation of information from the university schema as shown in Figure 8.3. Write the following queries in SPARQL.
Find the titles of all courses taken by any student named Zhang.
Find titles of all courses such that a student named Zhang takes a section of the course that is taught by an instructor named Srinivasan.
Find the attribute names and values of all attributes of the instructor named Srinivasan, without enumerating the attribute names in your query.
- Find the titles of all courses taken by any student named Zhang.
select ?title
where {
?student_id name "Zhang"
?student_id takes ?section_id
?section_id sec_course ?course_id
?course_id title ?title
}
- Find titles of all courses such that a student named Zhang takes a section of the course that is taught by an instructor named Srinivasan.
select ?title
where {
?student_id instance-of student
?student_id name "Zhang"
?instructor_id instance-of instructor
?instructor_id name "Srinivasan"
?student_id takes ?sec
?instructor_id teaches ?sec
?sec sec_course ?course_id
?course_id title ?title
}
- Find the attribute names and values of all attributes of the instructor named Srinivasan, without enumerating the attribute names in your query.
select ?attribute_name, ?attribute_value
where {
?instructor_id instance-of instructor
?instructor_id name "Srinivasan"
?instructor_id ?attribute_name ?attribute_value
}
Note that unlinke in SQL, the predicate in a triple pattern can be a variable, which can match any relationship or attribute name.