SPARQL Answer Lab
Unit 3 of 620 min

Use the Same Name

Alice studies at U1. Which campus arrows can you follow from there?

Start here if you can find a match for one arrow. Review a single match.

Sections in this chapter
  1. Find the hand-off
  2. Use one blank twice
  3. Build the first complete row
  4. Read the evidence for each row
  5. Compare one name with two
  6. Look for the shared value at either end
  7. Practice: connect the two lines
  8. Apply it: Aya’s class

Find the hand-off

Suppose you are making a list of cities that have a campus of a university where Alice studies. The first step is to find a university linked to Alice by a study fact. The next step is to check which campus facts start from the university you just found.

Look for the edge labelled studies at leaving Alice. It ends at U1. Say the full fact aloud or read it back as a short sentence. “Alice studies at U1” tells you exactly which endpoint to take into the next step.

Step 1. Follow Alice’s study arrow to U1.
  • Alicestudies atU1

Now put a finger on U1 in the full map. You can continue along the arrows leaving U1 for CityA and CityB. There is another campus arrow ending at CityC, but its start is U2. To follow that arrow you would first need evidence that Alice studies at U2.

Step 2. Find campus arrows starting at U1. The U2 arrow is a separate route.

Think about how you would explain the answer to someone who cannot see the picture. “CityA is a campus city” leaves out the important part. “Alice studies at U1, and U1 has a campus in CityA” gives two facts and names their point of contact. You can use the same explanation for CityB.

Imagine the same request as two notes on a desk. The first says which university Alice attends. The second lists where each university has a campus. You put the notes together by looking up the university from the first note in the second. The place names in other rows can still describe real campuses.

The hand-off is useful because it tells us what to check before accepting a result. First check the relationship on each arrow. Then compare the university names at the ends where those arrows meet. A convincing answer needs both labels and a university that fits both facts.

Can you continue to CityC?

You found U1 by following Alice’s study arrow. The campus arrow to CityC starts at U2. Can it extend the path you just followed? Say which value you would need for the campus arrow to fit.

Get a hint

Compare the destination of the study arrow with the start of the campus arrow.

Check the hand-off

The study arrow gives U1. The campus arrow to CityC needs U2. Those names differ, so CityC cannot extend the U1 path. The arrows to CityA and CityB start at the U1 you found.

Carry the university forward. Each time you follow a campus arrow for this question, use the university reached by the study arrow. That step is what turns two facts into one answer about Alice.

Use one blank twice

In a query, give the place where the arrows meet a name. Here the first pattern has a blank at the end, called ?university. The ? marks a variable. Alice and the study relationship are fixed by the question. The value for the university comes from whichever fact matches that pattern.

The campus pattern has a blank at each end. Its start needs the university from the study pattern. Give that blank the same name, ?university. Give the destination a different name, ?city, because the campus city and the university each need their own value.

Read each code line alongside its arrow. The same labelled box connects the two lines.
ex:Alice ex:studiedAt ?university .
  • Alicestudies at?university
?university ex:hasCampusIn ?city .
  • ?universityhas a campus in?city

Read the first line in plain English. “Find a university where Alice studies.” Read the second as “Find a city where that university has a campus.” The phrase “that university” is the instruction to carry a value forward. The repeated variable name gives the query the same instruction.

Suppose the first pattern supplies U1. Writing U1 in the right-hand box on the first arrow also fills the left-hand box on the second arrow. The remaining choice is the city. The campus arrow must leave U1 because the same variable already has that value in this match.

Another way to think about it is to label two gaps on a printed form. You mark both gaps “university” because they refer to the same answer. You mark a different gap “city” because it may have a different answer. The useful information is where you repeat the label.

Even if you have never written code, you can check a pair of patterns by tracing their names. Find the first occurrence of a variable and circle any later occurrence with exactly the same spelling. Then ask whether the circled parts should actually refer to the same thing in the question.

Check which blank gets U1

The first line assigns U1 to ?university. The next line starts with ?university, so this row must use U1 there as well. The other blank, ?city, takes its value from the destination of a campus arrow.

One shared name means one value within each match. A different match can assign a different value to the same variable. The rule applies to those parts of the query that share a name while building one row.

Build the first complete row

The question asks for campus cities supported by two facts about the same university. We can build an answer one row at a time. Start with the match you already know from the diagram, Alice’s study arrow to U1. Keep the university assignment visible while you look for a campus arrow.

Step 1. Record the university

The first line matches “Alice studies at U1.” Write ?university = U1. At this point we have one value, but we still need evidence for a campus. Leaving the city blank in a working note lets us see the part of the question that remains open.

Step 2. Test the next arrow

Try “U1 has a campus in CityA.” The start of that arrow agrees with the value already written down. Its label agrees with the relation in the second line. Now it supplies the missing destination, so write ?city = CityA beside the university assignment.

Step 3. Save the values as a row

Put the two assignments together in one row, (U1, CityA). We can read both original facts back from it. The row describes a whole route from Alice through U1 to CityA. Keeping the university in the row makes the reason for including CityA available for inspection.

One complete match. U1 is the destination of step 1 and the start of step 2.
  • Alice1. studies atU1
  • U12. has a campus inCityA
Assignments in the row supported by these two facts
?university?city
U1CityA

Giving a variable a value is called a binding. Here, binding the university to U1 lets you check all the matching campus arrows. Binding the city to CityA finishes the row for that particular route. You can make another row when you find another campus arrow that also starts at U1.

This is a join. Each part has its own matches, and we combine assignments that agree on shared variables. The study and campus facts both assign U1 to the university in this example. Try reading CityA’s campus arrow by itself and notice what the first line adds: evidence that U1 is linked to Alice.

The steps are a way to follow the meaning of the query. They also help you check an answer by replaying the two facts. An engine can look for the campus edges first and then check which university Alice studies at. It still has to find an assignment that fits both patterns.

Now suggest a second row for the same question. Keep the first assignment and see whether another arrow leaves U1 with the right label. CityB provides a different value for the destination. The next section shows how to check each row, including a candidate whose city looks plausible until you trace its edge.

Read the evidence for each row

Every row has to pass the same check. Put its values back into the two patterns, turning both into concrete statements. Then look for those statements in the data. You are checking the values together. A value in one column can be real while the proposed combination has no supporting path.

Two complete matches for the shared university
UniversityCityCampus evidence
U1CityAU1 has a campus in CityA.
U1CityBU1 has a campus in CityB.

Start with the first row. Substituting U1 into the study pattern gives Alice’s study fact. Substituting U1 and CityA into the campus pattern gives the campus fact for CityA. With the second row, the study fact stays the same and the campus fact changes to the one for CityB.

Both rows can use U1 because the same study fact can support more than one route. It can be paired with the campus arrow to CityA or with the campus arrow to CityB. Each finished pair of facts supplies a different city, so the table contains a row for each match.

Rejected candidate, separate from the result table
CandidateWhere the check fails
U1, CityCThe campus fact for CityC starts at U2. This row needs one starting at U1.

Now try the rejected candidate. The study fact still works when the row says U1. The campus pattern would need “U1 has a campus in CityC.” The recorded CityC arrow has the right label and destination, but its start is U2. That is the exact place where the check fails.

We can imagine a separate version of the data with an extra study fact, “Alice studies at U2.” In that version, a different row could use U2 in both patterns and reach CityC. The shared name would still do its job. The new row would have a study fact that supplies U2.

This also explains why a repeated university in the table is useful. You can point to where the two rows agree and where they differ. Later, when you count, you will need to decide whether your question asks about two routes or the one university those routes have in common.

In the practice, the Show row buttons highlight the facts for one selected row. Try CityA and CityB in turn. The highlighted study fact gives the same starting evidence in both cases. Check which campus arrow changes. The status message also names the values used by the chosen row.

Compare one name with two

Consider a version of the campus pattern whose first term is ?otherUniversity. The study pattern still supplies U1 for ?university. The second line can use its own university value, since it now has a separate variable. Look at the single name change in these two versions.

Shared university. Both lines use ?university.
ex:Alice ex:studiedAt ?university .
?university ex:hasCampusIn ?city .
Separate universities. Line 2 uses ?otherUniversity.
ex:Alice ex:studiedAt ?university .
?otherUniversity ex:hasCampusIn ?city .

With the separate name, the campus line can match all three campus facts in this graph. Two start at U1, so they can still lead to the answers CityA and CityB. The third starts at U2, and the query can combine it with Alice’s study fact because the university assignments use different names.

Complete rows with two separate university variables
UniversityOther universityCity
U1U1CityA
U1U1CityB
U1U2CityC, extra row

Read the extra row as two sentences. Alice studies at U1. U2 has a campus in CityC. Both sentences are in the graph, yet together they do not answer the question about Alice’s university. Keeping both university columns makes the reason visible. The two patterns made no shared decision about that university.

Notice that the first two rows still use U1 in both columns. Two variable names can happen to take the same value. They allow different values too, which is why the third row is possible. A shared name would require the two university positions to agree before the row could enter the result.

Names can be changed consistently. If you replace both occurrences of ?university with ?school, one variable still appears in both patterns. The values in the matching rows are the same, with the university column labelled ?school. Replacing only one occurrence changes the constraint itself.

Try a quick comparison on paper. Underline the end of the study pattern in each version, then underline the start of its campus pattern. Compare the two names you underlined. This is a small way to inspect the question the query is really asking before reading any result table.

Check a rename of both occurrences

The two rows still give CityA and CityB. Each line uses the same ?school value in a row, just as they previously used the same ?university value.

Check the name where the facts meet. A sensible sounding name can still be a separate blank. The spelling across the two patterns is what determines whether a value must be shared.

Look for the shared value at either end

So far one arrow ended at the university and the other started from it. The shared value can also be at the same end of both arrows. Imagine asking which organiser runs an event that Arun plans to attend. One arrow starts at Arun and reaches the event. A second arrow starts at the organiser and reaches the event.

Two arrows can meet at their destinations. The boxes labelled ?event must take the same value within a match.
  • Arunplans to attend?event
  • ?organiserruns?event

In that event question, the value to report comes from the start of the second arrow. If Arun plans to attend the Lantern Show and the Culture Club runs that show, the two facts meet at the Lantern Show. A fact about who runs the Book Fair would provide an organiser for a different event.

The drawing preserves the direction of each recorded relationship. You can find the organiser by inspecting arrows into the event, while still reading “Culture Club runs Lantern Show” in the direction shown. You can also compare both destination boxes directly. Both techniques ask whether the events agree.

Try reading the two arrows aloud before you write a name in either pattern. Listen for the object that appears in both statements. In the campus example, the university is the destination of the study arrow and the start of the campus arrow. In the event example, both arrows end at the shared event.

When the piece you want to report is a different object, give it its own variable. Here the organiser has its own blank because the organiser is the answer and the event is what links the two facts. A table can keep both values even if the person reading it only asked for the organiser.

Before you practise, try describing the small transfer you just made. The concrete relationships changed from study and campus to attendance and running an event. The rule remained the same: find one entity that both facts need to refer to. Once you can trace that entity, use the repeated name to express the link.

You can use this check when a question asks for a place, a person or something else. Figure out what the question asks you to report, then look for another value that has to be shared for the answer to hold. The two roles may be different even when both need a place in your results.

Choose the shared name by meaning. It belongs in the positions that must refer to the same thing. Read the arrow directions to locate those positions, then compare the variable names.

Connect the two lines

Need a reminder? See the two places that share a name before filling the blank.

In the second line, use the university found in the first. Fill in just the blank. After checking, try both trace buttons in the result table to connect each city to the two facts that support it.

Read the pattern from the first line to the next. Think about the blank that has already been filled before you look at the blank for the city. If you get a hint, use it to compare the spelling of the two university names and try again.

ex:Alice ex:studiedAt ?university .
ex:hasCampusIn ?city .

Explain one exclusion

Why doesn’t CityC appear in either row? Give the value supplied by the study fact, then say what value the campus arrow for CityC would require. Use the two university names in your explanation.

Check your explanation

CityC is a campus of U2. The study fact binds ?university to U1, so the campus line must also start at U1. U2 would make the second fact fit on its own, but it would break the agreement with the first fact.

If you mentioned just the destination, try pointing to the start of the CityC arrow. CityC is a possible value for ?city in isolation. The start of its supporting edge is what rules it out for Alice’s university.

Try a different name

Imagine the author of the query wants to use ?school. Decide what would happen in each of these edits. First, rename only the blank at the start of the campus line. Then start from the original query again and rename both occurrences. Predict the cities that would appear.

Check both edits

Renaming the campus blank alone gives it a value independent of ?university. It can use U1 or U2, so CityA, CityB and CityC appear. With both occurrences renamed to ?school, the two patterns still share a variable. That version gives CityA and CityB.

To see the difference, trace CityC against each version. The one-line rename gives U2 its own place in the row. After the two-line rename, that same place has to hold U1 because the first line binds ?school to U1.

Apply it: where does Aya’s class meet?

Aya enrolled in Class A. Studio 1 hosts Class A. Studio 2 hosts Class B. Use these records to answer this question: Which studio hosts the class Aya enrolled in?

There are three facts. Both studios host a class, and the class at the arrow’s destination tells you which studio fits.
  • Ayaenrolled inClass A
  • Studio 1hostsClass A
  • Studio 2hostsClass B
ex:Aya ex:enrolledIn ?class .
?studio ex:hosts ___ .
  1. Put a variable in the blank at the end of the second line. Say what it has to mean.
  2. Find the studio and name the two facts that support your answer.
  3. Predict what happens if the blank is ?otherClass instead. Which extra studio appears, and why?

Give your answers aloud or write down a row showing the studio and class. Put your reasoning next to your prediction before you open the worked answer.

Get a hint about the two arrows

Look at the destinations of the enrolment and hosting arrows. The question is about the studio where the enrolled class is hosted. Use the first line to decide what needs to be at the end of the second line.

Show the worked answer

Use ?class at the end of the second line. The first line gives Class A. The two facts that fit together are Aya enrolled in Class A and Studio 1 hosts Class A. The matching studio is Studio 1.

With ?otherClass, the first line still gives Class A, but the second line can use Class B as well. Studio 2 appears because it hosts Class B. Its edge satisfies the hosting pattern on its own.

What each version matches
Second line ends withStudios returnedTest on the class
?classStudio 1The class must be Class A.
?otherClassStudio 1 and Studio 2Class A and Class B can each satisfy line 2.

Check your explanation by naming both ends of the evidence. In this question, the answer is a studio at the start of a hosting arrow. The shared value sits at the end of both arrows. If your explanation put the studio in both lines, return to Looking for a shared value at either end.

In the next unit you will keep track of what a results table counts. Keep one thought from this lesson: a row preserves the values that made its facts fit together.

Ready to move on?

Point to the value both lines share. Use it to explain one matching row and why a different university’s campus is excluded.