Find a Match
Where does Alice study? What goes in the blank?
Start here if you can follow the direction of an arrow. Review how to read an arrow.
Sections in this chapter
Leave a place for the answer
Read “Alice studies at U1” from the tail of the arrow to its tip. Alice is the subject. The relationship tells you what connects Alice to U1, the object. You can read the fact aloud as a short sentence, which gives you a way to check the direction and the relationship together.
Now imagine that you can remember Alice’s name and the words “studies at”. The name of the place is the part you want to look up. Say the sentence again and pause where that name would go. “Alice studies at …” leaves a space for exactly the information you want.
A recorded fact
- Alicestudies atU1
The shape of the question
- Alicestudies atBlank
The two known parts are Alice and studies at. They will act as checks when you look through the graph. If you find a fact that meets those checks, copy the value at the arrowhead into the blank.
Consider two facts on a noticeboard. One says “Alice studies at U1” and another says “Alice visited U2”. Both begin with Alice and name a university. The first can answer “Where does Alice study?” because its relationship says what the question asks. The second would help if you wanted to find out where Alice visited.
One more fact, “Ravi studies at U2”, uses the right relationship and has a university at the end. The subject is Ravi, so it answers a question about Ravi. Checking the subject as well as the relationship keeps his answer separate from Alice’s.
Start with the question. Work out which parts are known and which part you need to find. Then use the known parts to test each candidate fact and take the answer from the blank position.
A fact can be drawn in many places on a page. The check still uses the start, label and end of its arrow. You can use exactly the same check on facts written in three columns in a spreadsheet.
Try that spreadsheet version in your head. Read the subject column first and keep facts about Alice. Then read their relationship column and keep the ones that say “studies at”. You can read each answer from the object column of the remaining facts.
Give the blank a name
A SPARQL triple pattern is a template for a fact. It has the same three positions as the RDF facts from Unit 1. The template can include values you already know and blanks for the things you want to find. SPARQL gives each blank a name so you can refer to it in the rest of the query.
A name beginning with ? is a variable. Read ?university as “the value for the university blank”. Here it sits after ex:studiedAt, in the object position. Think of the line as the sentence “Alice studies at some value we would like to find”.
ex:Alice ex:studiedAt ?university .
| Position in the arrow | In the pattern | What to do |
|---|---|---|
| Start | ex:Alice | Require Alice |
| Relationship | ex:studiedAt | Require “studies at” |
| End | ?university | Take the value from the fact |
Look at how ex:Alice appears before the relationship. That is a fixed identifier in the data. The identifier says which Alice the question means. ex:studiedAt is the corresponding identifier for the relationship. The last term uses the question mark because the data will supply its value.
Choosing a clear variable name helps a reader predict its purpose. You could name this blank ?place and find the same destination if you used that name consistently. The matching graph facts and the fixed relationship determine what can fill it. A variable name by itself places no restriction on the kind of value returned.
When you read a new pattern, try marking every term you can say aloud immediately. Alice and “studies at” are both things you can name. The remaining term is the thing to look up. This quick check also helps you catch a pattern that answers an easier question than the one you meant to ask.
For example, writing ex:U1 in the last position would give three fixed parts. You could use that pattern to check whether the graph contains the specific fact about Alice studying at U1. For the question “Where does Alice study?”, the destination needs to be able to take any value from a suitable fact.
The words on the drawing are labels for humans. The code uses the exact identifiers written in the example data. When you read ex:studiedAt aloud as “studies at”, keep the same meaning and position even though you pronounce it differently.
See the full query
PREFIX ex: <https://example.org/>
SELECT ?university WHERE {
ex:Alice ex:studiedAt ?university .
}
PREFIX defines ex:, a short name for the examples’ web namespace. WHERE contains the pattern and SELECT names the variable to show in the result.
Make a match
Put the pattern beside a candidate fact. The graph contains an arrow from Alice to U1 labelled “studies at”. Compare its start and label with those in the pattern, then decide what goes at the end. Predict whether U1 will fill the blank before revealing the match.
?university |
|---|
| U1 |
Giving a variable a value in a match is called a binding. In this example, the binding pairs the variable ?university with the identifier for U1. One row records that pairing. Read the column heading first and the cell second, as though you were saying “the university for this match is U1”.
The pattern stays on the page so you can use it again. If another recorded study fact about Alice appeared, you would check it against the same pattern and give it its own row when it fitted. Imagine the variable as a space on a form that you can fill separately for each suitable fact.
You can check your own answer by reading the pattern with the blank filled. Putting U1 into the pattern gives “Alice studies at U1”. You should be able to point to that exact fact in the data. If you could only point to a sentence about Alice visiting U1, look at the relationship again.
Try keeping a finger on the fixed part while you scan the graph. Starting at Alice narrows the search to facts about Alice. Following the label “studies at” narrows it again. At the end, copy the value from the arrowhead. The order of these checks helps you explain how you found the binding.
You can explain the same match from the result back to the fact. Start with the row containing U1 and substitute that value in the template. The resulting sentence names the evidence you expect to find. That is a useful way to check a result another person has handed you.
Try one match
- SamreturnedMap
Read the arrow as a sentence. What value completes “Sam returned ?item”?
Get a hint
Sam and “returned” are known. Read to the arrowhead for the name of the item.
Check the binding
?item is Map. The start and label fit. The end supplies Map, so the full sentence is “Sam returned the map”.
Worked example: U1’s campus cities
Now ask which cities have a U1 campus. Begin with U1, look for “has a campus in”, and leave a blank for the city. The campus facts give us several candidates, so work through them one at a time using the same checks you tried with Alice.
ex:U1 ex:hasCampusIn ?city .
- U1has a campus inCityA
- U1has a campus inCityB
- U2has a campus inCityC
Check the first candidate
Start with the arrow ending at CityA. Its tail is U1 and the label is the campus relationship. Both meet the pattern’s requirements. The arrowhead gives CityA, so the first binding puts CityA into ?city.
Use the same checks on the next two
The next arrow begins at U1 and uses the same label. Its end is CityB, so a second binding goes in its own row. Both facts pass the same test. If you find one answer, keep looking for other facts that meet the pattern.
The third fact also mentions a campus. It begins at U2, so it fails the start check. CityC stays out of the result because the pattern is asking about U1. A table makes the check visible by keeping the reason beside each candidate.
| Fact in the graph | U1 at the start? | Campus relationship? | Value for ?city |
|---|---|---|---|
| U1 has a campus in CityA | Yes | Yes | CityA |
| U1 has a campus in CityB | Yes | Yes | CityB |
| U2 has a campus in CityC | No, U2 | Yes | No match |
In the query result, the candidate that failed disappears. The table above is a checklist for a reader, with a row for every candidate we inspected. The output of this pattern has only the successful bindings, one for CityA and one for CityB.
When you explain why only these cities appear, point to the two arrows with U1 at the start. That tells someone how to reproduce the result. Pointing out where CityC’s arrow starts can be just as helpful if someone expected a third result.
Imagine a new fact says that U1 is named after CityD. Its start fits the pattern and its label is different. Sorting the candidates by start makes the search easier. Checking the label confirms whether an arrow can actually answer the question about campuses.
A reader might say “CityA and CityB answer the question because both are cities”. You can make the explanation stronger by naming the specific evidence. Each has a recorded campus arrow that starts at U1. This identifies the two facts someone could look for when they check your answer.
Read the two checks before you copy the answer. One familiar name is enough to make an arrow look promising. Both fixed parts must fit to produce a match.
Move the blank
Think back to the question about Alice. Suppose instead that you know the university and want to find out who studies there. The relationship still describes where someone studies. Now the question begins with “Who?”, so the unknown part is the person at the tail of the arrow.
Where does Alice study?
- Alicestudies at?university
ex:Alice ex:studiedAt ?university .
Who studies at U1?
- ?studentstudies atU1
?student ex:studiedAt ex:U1 .
For “Who studies at U1?”, you can read candidate facts from their beginnings as usual. Check that the label says “studies at” and the end is U1. When those parts fit, take the name at the start. The recorded fact about Alice now supplies a different binding, ?student with Alice as its value.
Try saying what belongs in each fixed position before you look at the drawing. In the first question, you know the person and the relationship. In the second, you know the relationship and its destination. Notice how the two questions can use the very same fact and still return answers from different parts of it.
Here is a similar change in everyday language. “What did Sam return?” asks for the object after “returned”. “Who returned the map?” asks for the person before “returned”. A fact saying “Sam returned the map” works for both questions, and each question takes a different name from the same edge.
Check where to put the variable
For “Who returned the map?”, put the variable at the start. Require the label “returned” and Map at the end. The match puts Sam in the blank.
When a question starts “Where?”, its English word order may bring the missing part to the front. Use the order of the underlying fact when writing the pattern. “Where does Alice study?” becomes “Alice studies at ___”, which makes the position easy to see.
Choose a name you will recognise when it comes back as a column heading. ?student makes it easy to read a list of names, and ?university makes it easy to read a list of places. You will use this habit in the next unit, where more than one pattern needs to refer to the same named blank.
Explain what the result shows
It helps to say out loud how many bindings you found, even when you can already see the values. For the study question about Alice, the result has one row. The campus question about U1 gives two. We can also ask a campus question about an example university whose campus facts are absent from this data.
?city |
|---|
?university |
|---|
| U1 |
?city |
|---|
| CityA |
| CityB |
Notice the first table has a column heading and no data rows. The question was clear enough to name a result column. The graph supplied zero matching facts. You can report the result as “the graph has no recorded campus fact about U3”. This wording keeps your statement about the evidence you checked.
Sometimes an empty result is a clue to check your question again. If you accidentally asked about U3 when you meant U1, the subject check explains why the U1 facts were left out. If the identifier matches the intended university, the next step might be to ask where the campus information is recorded.
One or more matches also describe the graph you searched. New records can add a binding when they satisfy the same fixed checks. Suppose U1 opens a campus in CityD and that fact is recorded with the same campus relationship. The pattern you already wrote can be applied to the updated data.
Before you open the explanation, predict which universities can still supply a binding for ex:U1 ex:hasCampusIn ?city .. Then list the cities that would appear if the graph adds the new U1 campus.
Check the updated result
The list is CityA, CityB and CityD. Each comes from a fact with U1 at the start and “has a campus in” as its label. CityC still belongs to the candidate that starts at U2.
A result list can be read in any order here. What matters is which facts can supply a value for the named blank. Write your findings as a sentence about the data, with enough detail for someone else to check your reasoning.
If you kept your candidate checklist, you can explain an omission as precisely as an answer. For CityC, the failing position is the subject. If the label had been the part that failed, your explanation would point to the relationship instead. Keep those two checks separate during the practice below.
One fact gives you one set of bindings for this pattern. When the next unit asks for two facts that fit together, you will check whether the two patterns can agree about the thing they share.
Practice: fill the code blank
Need a reminder? Watch one fact fill the blank and look at what stays fixed.
Alice is asking you to write a pattern that can tell her where she studies. Read the code as a sentence, keeping Alice and “studies at” fixed. Choose the term for the one part she wants the graph to supply. Check your answer, then explain which recorded fact fills the blank.
ex:Alice ex:studiedAt ______ .
Get a hint for the blank
Say the two known parts aloud. You already have the name at the beginning of the arrow and the words on the label. The missing part names the place at the end. Look for an option whose first character marks a variable.
Check your reasoning
The term is ?university. ex:Alice fixes the student and ex:studiedAt fixes the relationship. The arrow from Alice to U1 uses that label and gives us U1 at the destination. You can check the result by reading the recorded fact “Alice studies at U1”.
Choosing ex:U1 would write U1 as part of the request. Its place in this example is in the result. Choosing ex:Alice would ask for a study fact with Alice at both ends. The question asks to discover the destination of the recorded edge.
Check a bus timetable
At a station information desk, a passenger asks, “Where does Route 7 stop?” Here are four facts from the transport graph. Work out where the variable belongs, then list the answers you would give. For each candidate that fails, name the part of the pattern it conflicts with.
- Route 7stops atStation
- Route 7stops atMuseum
- Route 9stops atStadium
- Route 7starts atRiverside
- Say the two things the passenger has already specified. Rewrite the question as a sentence with a blank that you can name
?stop. - Check the start and label of each arrow. Write down the place at the end of every arrow that passes both checks.
- Explain the two exclusions separately. Which candidate fails the check on the route? Which candidate fails the check on the relationship?
Get a hint for finding the stops
Begin with the words “Route 7 stops at …”. Ask which terms from that sentence should appear at the start of every match and on its arrow. Even if the same route appears on multiple records, each record expresses the relationship written on its own label.
Check the timetable answer
ex:Route7 ex:stopsAt ?stop .
Place ?stop at the end. Station and Museum are the two values. Each arrow starts at Route 7 and is labelled “stops at”. Write one binding for each place so that someone can point to the corresponding arrow.
| Destination | Reason |
|---|---|
| Station | Route 7 has a “stops at” arrow to Station. |
| Museum | Route 7 has a “stops at” arrow to Museum. |
| Stadium | The arrow starts at Route 9. The route check fails. |
| Riverside | The arrow is labelled “starts at”. The relationship check fails. |
Suppose you included Stadium. Check the arrow again at its tail and compare the route number with the question. If you included Riverside, read the two relationship names aloud and decide which one the passenger asked about. Each exclusion should have a reason that you can show in the data.
Check that your answer names both stops. When several facts fit, a single answer can leave out another supported value. In the timetable you can check the Station and Museum arrows separately. Both meet the same requirements and each supplies a different value.
If you are helping the passenger, you can answer “The records list Station and Museum as stops for Route 7”. The question about stops and the facts you searched are clear from that sentence.
Try the same facts with a different question
A second passenger asks “Which route stops at the Museum?” Reuse the timetable facts. Work out which two parts are now fixed, and move the variable to the place the new question asks about. Predict the value you will get and describe which exact arrow confirms it.
Check the new question
Use ?route ex:stopsAt ex:Museum .. The relationship and destination are fixed. The arrow from Route 7 to Museum passes both checks, so ?route is Route 7. Reading the fact aloud confirms which route name to tell the passenger.
Compare your two answers. The first question found places at the tips of suitable arrows. The second found a name at the tail of an arrow. Both looked for a “stops at” relationship, and each took the value from the position that was unknown.
If you could check the route and the relationship independently, you are ready to reuse a value from one pattern in another. In Unit 3, you will use one fact to find the university and then look for campuses for the very same university.
Ready to move on?
Say which parts of a pattern stay fixed, which value fills the blank, and why another fact is excluded.